Skip to content

More specific overloads for OffscreenCanvas.getContext #1480

Description

I have a project which previously used @types/offscreencanvas for OffscreenCanvas support. That library defines separate overloads for the different getContext types:

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/a6b57ed5f511b6d1c842b6acb4961c966d4afb00/types/offscreencanvas/index.d.ts#L46-L58

When pulling in the new changes via microsoft/TypeScript#51300 in TS 4.9.4, it seems like there's only one definition of getContext, so my calls to e.g. getContext("2d")?.clearRect(...) now fail to compile:

Property 'clearRect' does not exist on type 'CanvasRenderingContext2D | OffscreenRenderingContext'.
  Property 'clearRect' does not exist on type 'ImageBitmapRenderingContext'.ts(2339)

It would be nice to use the same approach of overloading based on the contextType parameter so that the proper types can be used without an as downcast.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions