microsoft / microsoft/TypeScript
Opaque types for WebGL
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
WebGL types are included in the default lib.d.ts. However, most types are defined as empty interfaces, which means that the compiler doesn't catch many WebGL errors.
Consider the following code:
function foo(gl: WebGLRenderingContext, fbo: WebGLFramebuffer) {
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo); // Correct call
gl.bindFramebuffer(gl.FRAMEBUFFER, "abc"); // This should be a compile error!
fbo = gl.createFramebuffer(); // Correct call
fbo = new WebGLFramebuffer; // This should be a compile error!
}
- The second parameter of
gl.bindFramebuffershould be aWebGLFramebufferobject, but passing astringdoes not emit a compile error or warning. - The type
WebGLFramebuffercan only be constructed viaWebGLRenderingContext.createFramebuffer, calling new throws an error at runtime
Ideally, Typescript would know that WebGLFramebuffer, even though it has no public properties, cannot be constructed or converted to any other type.
I assume that this is not easy to specify with structural typing, but having such opaque types could be useful for other libraries as well (e.g., for returning handles).
If that is not possible, can we find a workaround that at least helps catching the above mentioned bugs?
For reference, here's how the above types are currently defined in lib.d.ts:
interface WebGLFramebuffer extends WebGLObject {
}
declare var WebGLFramebuffer: {
prototype: WebGLFramebuffer;
new(): WebGLFramebuffer;
}
interface WebGLRenderingContext {
bindFramebuffer(target: number, framebuffer: WebGLFramebuffer): void;
// + many other methods
}
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
先从 lib.d.ts 中显示的 WebGLFramebuffer 和 WebGLRenderingContext 定义开始,然后跟踪编译器如何处理它们的结构类型和构造函数声明。只有当 TypeScript 具有一种已达成共识的机制或 workaround,能够拒绝无效的 bindFramebuffer 参数和直接构造,并且该行为由适当的编译器测试覆盖时,这个 issue 才算完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 25/100