microsoft / microsoft/TypeScript
Opaque types for WebGL
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
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
}
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit den in lib.d.ts gezeigten Definitionen von WebGLFramebuffer und WebGLRenderingContext und verfolge dann, wie der Compiler ihre strukturellen Typen und Konstruktordeklarationen verarbeitet. Das Issue ist erst abgeschlossen, wenn TypeScript über einen vereinbarten Mechanismus oder Workaround verfügt, der das ungültige bindFramebuffer-Argument und die direkte Konstruktion ablehnt, und das Verhalten durch geeignete Compiler-Tests abgedeckt ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 25/100