Skip to content

lib.dom.d.ts OffscreenCanvas.getContext() not returning type based on context ID #52831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
TomasHubelbauer opened this issue Feb 17, 2023 · 2 comments
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this
Milestone

Comments

@TomasHubelbauer
Copy link

lib Update Request

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.

Configuration Check

My compilation target is ESNext and my lib is ["DOM", "ESNext"].

Missing / Incorrect Definition

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

@TomasHubelbauer
Copy link
Author

OffscreenCanvas is also missing convertToBlob. I am happy to contribute these changes in one PR, just give me a greenlight.

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript labels Feb 17, 2023
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Feb 17, 2023
@TomasHubelbauer
Copy link
Author

I did not read the issue template carefully enough, it seems the right way to handle this is not to send a PR here but instead open one in https://github.com/microsoft/TypeScript-DOM-lib-generator so I did that:

microsoft/TypeScript-DOM-lib-generator#1501

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants