-
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
[TS 5.4.0-beta] instaceof
narrowing impacted by prototype property
#57397
Comments
FYI - each nightly version has |
Thanks. That is what I used (the EDIT: Remembering that after looking up the commit I did switch the url to the date based param, but that can be less accurate. I agree the commit-range url is much better! thanks again! |
If you're curious to bisect something, |
Oh that's awesome. I've been manually bisecting by updating the playground url π |
Off topic-ish, but the issue template links to it; is there something we can do to make it more obvious? |
Maybe if I hadn't raised a few tickets in the past I would have been less hasty to fill in the form without really reading the extra info bits. That said, maybe also putting a link to it in a HTML comment in the text box would make it more visible. As harder to not read the inputs. |
Bisect confirmed #54753 |
I think this works kind of as expected right now. The break is related to this change: type P = (typeof MenuWidget)["prototype"]; // TS 5.3: any, TS 5.4: new () => Widget
const p = MenuWidget.prototype; // TS 5.3: any, TS 5.4: new () => Widget It boils down to the fact that a concrete That said, it has a very surprising effect on this given example. I think though that it just uncovers a weird implicit implementation detail. It is surprising that the official The "true" type IfAny<T, Y, N> = 0 extends 1 & T ? Y : N;
type ConstructorType = abstract new (...args: never) => any;
type Constructed<T extends ConstructorType> = T extends abstract new (
...args: never
) => infer R
? R
: any;
type TryConstruct<T> = T extends ConstructorType ? Constructed<T> : {};
type InstanceType2<T> = "prototype" extends keyof T
? IfAny<T["prototype"], TryConstruct<T>, T["prototype"]>
: TryConstruct<T>; |
Thank you so much for that analysis @Andarist! I can confirm back in the original project that the This change is only causing one error in a widget that is still using our 'legacy' class system that pre-dates ES2015 classes (thus all the manual class-like type generation), and we can work around this in our code until the widget gets migrated. Thanks again! |
This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
instanceof, narrowing, prototype
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?target=99&ts=5.4.0-dev.20231130#code/CYUwxgNghgTiAEkoGdnwOoEtgHMQBd4BvAKHnNGXxgHsBPACgEoAueANxuwG4SBfEiVBI4iGgDsqGbHnwBhCVRgBXMPhow2pcuXEgA7szZZcBXgKHhoosIsIBZEOOUnZc6Ki1kdew62LeOjqYyI7OADIh+GwARjQ0ECBQ4rxBfPAAZNKm+Kk6AA606vh0+SBsJWU0AGbZbnYqahrc8AD0rfAAPAC03fBwALY07JjiOPD4ABYh8FCc2GggMLQw-LwklQhhLjIE8AC88ACSkvjJYCAAKqUgnZs18NuuBO4oyAB868LWCLanj04djkKjcHk9dvIPGgstofAYjADnM98GtBO10RjMVjsTjcXj0YJEoQ-vgnNFEUDZPAAD7wZTiUDVUYgYC8b6wX52DjGCHrTC1BjseCjKjnEBgwHIpgBILkElkg4cFrtLq9eC+eBLFbC8TwACsADoACwGgAM3RiBCg-EEIAAHvkNIQiAIgA
π» Code
(reduced down from large repo as best I could)
New behavior
TypeScript
5.4.0-beta
appears to be consulting the.prototype
property of the type in a way that it wasn't before resulting in theinstanceof
narrowing to narrow to the base class.Previous behavior
The code in
5.3.3
was expecting theinstanceof
to narrow to the return type of the function being consulted.Additional information about the issue
Note: this is more of a change report than a bug report, as I don't think this was announced in the 5.4.0-beta breaking changes. I suspect the change is an improvement and is catching a potential issue that was previously unsafe.
The text was updated successfully, but these errors were encountered: