Skip to content
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

Feature Request / Proposal: constructor type guard #23274

Closed
sheley1998 opened this issue Apr 9, 2018 · 7 comments · Fixed by #32774
Closed

Feature Request / Proposal: constructor type guard #23274

sheley1998 opened this issue Apr 9, 2018 · 7 comments · Fixed by #32774
Labels
Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this Suggestion An idea for TypeScript
Milestone

Comments

@sheley1998
Copy link

sheley1998 commented Apr 9, 2018

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');
}
@sheley1998 sheley1998 changed the title Feature Request / Proposal: constructor ty Feature Request / Proposal: constructor type guard Apr 9, 2018
@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Apr 9, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Apr 10, 2018

related to #3841

@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". and removed In Discussion Not yet reached consensus labels Apr 17, 2018
@RyanCavanaugh
Copy link
Member

Accepting PRs for a new kind of type guard (IOW one that specifically looks for this syntactic pattern). Note that the false arm here can't perform any narrowing of a union due to constructor checks not identifying subclasses.

@Kingwl
Copy link
Contributor

Kingwl commented Apr 22, 2018

i’d like to work on this 😄

@austincummings
Copy link
Contributor

Is this issue being worked? If not I'd like to try and add this.

@adam-arold
Copy link

Hey. Why was this closed? :(

@Kingwl
Copy link
Contributor

Kingwl commented May 21, 2020

Because #32774 is merged.

@ikonst
Copy link

ikonst commented Jun 24, 2024

The snippet in the issue doesn't work. @sandersn, can we reopen?

The only thing that works with #32774 is

const el: HTMLDivElement | HTMLCanvasElement = document.createElement(tagName);
if (el.constructor === HTMLCanvasElement) {
  //inside of this scope, el is parsed as an instance of HTMLCanvasElement
  el.getContext('2d');
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
8 participants