-
Notifications
You must be signed in to change notification settings - Fork 13k
Open
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptThe issue relates to the different libraries shipped with TypeScriptHelp WantedYou can do thisYou can do this
Milestone
Description
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.bindFramebuffer
should be aWebGLFramebuffer
object, but passing astring
does not emit a compile error or warning. - The type
WebGLFramebuffer
can 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
}
tp, lll000111, zakjan, davl3232, xionluhnis and 2 more
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptThe issue relates to the different libraries shipped with TypeScriptHelp WantedYou can do thisYou can do this