You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature Request / Proposal: constructor type guard #23274
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:
constel:HTMLElement=document.createElement('canvas');if(el.constructor===HTMLCanvasElement){//inside of this scope, el is parsed as an instance of HTMLCanvasElementel.getContext('2d');}
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.
constel: HTMLDivElement|HTMLCanvasElement=document.createElement(tagName);if(el.constructor===HTMLCanvasElement){//inside of this scope, el is parsed as an instance of HTMLCanvasElementel.getContext('2d');}
Activity
[-]Feature Request / Proposal: constructor ty[/-][+]Feature Request / Proposal: constructor type guard[/+]mhegazy commentedon Apr 10, 2018
related to #3841
RyanCavanaugh commentedon Apr 17, 2018
Accepting PRs for a new kind of type guard (IOW one that specifically looks for this syntactic pattern). Note that the
falsearm here can't perform any narrowing of a union due toconstructorchecks not identifying subclasses.Kingwl commentedon Apr 22, 2018
i’d like to work on this 😄
austincummings commentedon Jul 2, 2019
Is this issue being worked? If not I'd like to try and add this.
bruce-forte commentedon May 21, 2020
Hey. Why was this closed? :(
Kingwl commentedon May 21, 2020
Because #32774 is merged.
ikonst commentedon Jun 24, 2024
The snippet in the issue doesn't work. Nathan Shively-Sanders (@sandersn), can we reopen?
The only thing that works with #32774 is