Skip to content

Feature Request / Proposal: constructor type guard #23274

@sheley1998

Description

@sheley1998

In most cases, constructor comparison is faster than instanceof and typeof, thus using it as type guard can improve performance. The code below demonstrates the desired behavior:

const el:HTMLElement = document.createElement('canvas');

if (el.constructor === HTMLCanvasElement) {
    //inside of this scope, el is parsed as an instance of HTMLCanvasElement
    el.getContext('2d');
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Effort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".Help WantedYou can do thisSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions