Closed
Description
This is a follow-up to microsoft/TypeScript#52831.
I would like to request that OffscreenCanvas.getContext
behaves the same way as HTMLCanvasElement.getContext
in that the latter returns different concrete context types depending on what context ID is passed in as an argument and the former just returns an umbrella context type that needs to be disambiguated first.
HTMLCanvasElement.getContext
:
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
// `context` is `CanvasRenderingContext2D`
// lib.dom.d.ts:
getContext(contextId: "2d", options?: CanvasRenderingContext2DSettings): CanvasRenderingContext2D | null;
getContext(contextId: "bitmaprenderer", options?: ImageBitmapRenderingContextSettings): ImageBitmapRenderingContext | null;
getContext(contextId: "webgl", options?: WebGLContextAttributes): WebGLRenderingContext | null;
getContext(contextId: "webgl2", options?: WebGLContextAttributes): WebGL2RenderingContext | null;
getContext(contextId: string, options?: any): RenderingContext | null;
OffscreenCanvas.getContext
:
const canvas = new OffscreenCanvas(16, 16);
const context = canvas.getContext('2d');
// `context` is `OffscreenRenderingContext`
// lib.dom.d.ts:
getContext(contextId: OffscreenRenderingContextId, options?: any): OffscreenRenderingContext | null;
I believe it would be beneficial to add overloads with context ID literals which return specific context types.
Also, maybe it would make sense to introduce a context type string literal enum for the normal canvas context types, not just a string?
Documentation Link
Metadata
Metadata
Assignees
Labels
No labels