-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Inferred type changes from 5.5 to 5.6 #60077
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
Comments
I think this boils down to the same issue and I would refer to Ryan's comments there: #59764 (comment) and #59764 (comment) Those are just heuristics and there are no perfect answers to which type should be preferred. Some cases perform better with one candidate picked and some cases perform better with the other one picked. I think that especially the case presented by Ryan in the first linked comment sways this way more in favor of the covariant candidate. It's way more likely that the object's shape available at runtime will have the properties coming from a covariant argument position than those coming from a contravariant parameter. |
I agree with @Andarist that I was correct in those comments π |
OK, I can see I'm outgunned here. π FWIW, I do think it's a bit surprising that this wasn't mentioned in the release notes given that it can cause confusing changes when you upgrade β I only found the PR via |
Though I guess looking at #59764 (comment) a little more closely, one difference is that in that case, the widened type of I can see that there's no perfect answer in Ryan's case, but it seems there is here. |
That is a difference that I implicitly talked about here. Honestly, I don't know how to think about this in the TS's structural type system since there are tradeoffs to both approaches |
π Search Terms
π Version & Regression Information
every-ts
)β― Playground Link
https://www.typescriptlang.org/play/?ts=5.6.2#code/CYUwxgNghgTiAEYD2A7AzgF3gMwFzwB4AFAPgAoAoeHFfMgBxiXrXyIEp4BeE+ANyQBLYABoq8QSkEYA-GzGce8IgG4KFSRhAxsUMAiJMW8AN7i4AR3yYYkgOZrqzWdYy2UDigF91ydFkZmNG4cSmoGNiM0RV4AclixahN4S3wAIjT4LwU1CkCWADpnCgB6EvgAURgmGAkUeAAVAGV4AFYCgDYReBQkLEl4EFgIQW1+bTRBVDQKIA
π» Code
π Actual behavior
P
is inferred as{req: string}
, so accessing the optional property causes a TypeScript error.π Expected behavior
P
should be inferred asProps
, as it is in TS 5.5 (and earlier). If you drop theinit
parameter from the call tof
, thenP
is also inferred to beProps
in TS 5.6.Additional information about the issue
This seems related to #59764 but I'm not sure it's the same, so I figured I'd file an issue. I tried the same code on the playground for #59709 and it does not fix this issue.
cc @Andarist for whether TS 5.6 is right and I'm wrong π
The text was updated successfully, but these errors were encountered: