microsoft / microsoft/TypeScript
HTMLCanvasElement.getContext() wrong typings when used with attributes.
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
TypeScript Version: 2.4.1
Code
declare const canvas: HTMLCanvasElement;
//correct typing
const gl1 = canvas.getContext('webgl', { preserveDrawingBuffer: true, depth: false });
// wrong typing for gl2
const attributes = { preserveDrawingBuffer: true, depth: false };
const gl2 = canvas.getContext('webgl', attributes);
// If we copy HTMLCanvasElement.getContext() signatures it works
function getContext2(contextId: '2d', contextAttributes?: Canvas2DContextAttributes): CanvasRenderingContext2D | null;
function getContext2(contextId: 'webgl' | 'experimental-webgl', contextAttributes?: WebGLContextAttributes): WebGLRenderingContext | null;
function getContext2(contextId: string, contextAttributes?: {}): CanvasRenderingContext2D | WebGLRenderingContext | null {
return null;
}
// correct typing
const gl3 = getContext2('webgl', attributes);
You can copy/paste that code on the playground to see it live (couldn't share: 'Text buffer too large to share').
Expected behavior:
Typing of all gl var should be WebGLRenderingContext.
Actual behavior:
Typing for gl2 is Canvas2DContextAttributes | WebGLRenderingContext.
I wanted to try to resolve this by changing the typings but then I found out that if we copy the exact same signature it works as expected.
So the typing indeed seem correct, dunno what's going on.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con gli overload di HTMLCanvasElement.getContext() e riproduci il problema in TypeScript Playground usando il codice riportato nella segnalazione. Analizza perché una variabile oggetto produca un'unione mentre gli attributi inline funzionano, quindi verifica che gl2 venga inferito come WebGLRenderingContext senza compromettere l'overload 2D.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100