Skip to content

Clarify canvas ctx.globalCompositeOperation type #34673

Closed
@Joncom

Description

@Joncom

TypeScript Version: 3.6.4

Search Terms:

globalCompositeOperation, TypeScript, union

Code:

The valid strings for ctx.globalCompositeOperation can be viewed here: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation

For example, this is valid:

ctx.globalCompositeOperation = 'source-in';

However, this is invalid:

ctx.globalCompositeOperation = 'foo';

And yet TypeScript thinks both are valid, because TypeScript simply defines globalCompositeOperation as string:

TypeScript/lib/lib.dom.d.ts

Lines 3376 to 3379 in 29becf0

interface CanvasCompositing {
globalAlpha: number;
globalCompositeOperation: string;
}

The following union would provide a better development experience:

 'source-over' | 'source-in' | 'source-out' | 'source-atop' | 'destination-over' | 'destination-in' | 'destination-out' | 'destination-atop' | 'lighter' | 'copy' | 'xor' | 'multiply' | 'screen' | 'overlay' | 'darken' | 'lighten' | 'color-dodge' | 'color-burn' | 'hard-light' | 'soft-light' | 'difference' | 'exclusion' | 'hue' | 'saturation' | 'color' | 'luminosity'

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions