-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Comments
related to #3841 |
Accepting PRs for a new kind of type guard (IOW one that specifically looks for this syntactic pattern). Note that the |
i’d like to work on this 😄 |
Is this issue being worked? If not I'd like to try and add this. |
Hey. Why was this closed? :( |
Because #32774 is merged. |
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');
} |
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:
The text was updated successfully, but these errors were encountered: