Skip to content

HTMLCanvasElement.getContext('2d') incorrectly returns union type in 1.5 beta #2984

Description

@IanYates

I've got a bunch of TypeScript code that calls getContext('2d') on HTML canvas elements.

Since the update to 1.5 beta this morning I now get a union type of CanvasRenderingContext2D and WebGLRenderingContext. That makes sense for the general call to getContext(...) since either could be returned.

However if I'm specifically calling getContext('2d'), I know I'm getting back a CanvasRenderingcontext2D.
I worked around it by creating this little interface in a file

interface HTMLCanvasElement {
    getContext(contextId: '2d'): CanvasRenderingContext2D;
}

(using syntax from section 3.8.2.4 - Specialized Signatures in the TypeScript language reference)

Could we get this added to lib.d.ts perhaps?

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptFixedA PR has been merged for this issue

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions