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
declareconstf: <P>(fn: (props: P)=>void,init?: P,)=>P;interfaceProps{req: string;opt?: string;}constprops=f((p: Props)=>'',{req: ""},);props.opt// Error in TS 5.6, not in earlier versions
π Actual behavior
P is inferred as {req: string}, so accessing the optional property causes a TypeScript error.
π Expected behavior
P should be inferred as Props, as it is in TS 5.5 (and earlier). If you drop the init parameter from the call to f, then P is also inferred to be Props 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.
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.
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 every-ts bisect.
Though I guess looking at #59764 (comment) a little more closely, one difference is that in that case, the widened type of defaultT isn't assignable to Foo (at least with excess property checking). Whereas in my example the type of initis assignable to Props, so that seems like a more natural type to infer.
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
Activity
Andarist commentedon Sep 26, 2024
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.
RyanCavanaugh commentedon Sep 26, 2024
I agree with Mateusz BurzyΕski (@Andarist) that I was correct in those comments π
danvk commentedon Sep 26, 2024
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
every-ts bisect.danvk commentedon Sep 27, 2024
Though I guess looking at #59764 (comment) a little more closely, one difference is that in that case, the widened type of
defaultTisn't assignable toFoo(at least with excess property checking). Whereas in my example the type ofinitis assignable toProps, so that seems like a more natural type to infer.I can see that there's no perfect answer in Ryan's case, but it seems there is here.
Andarist commentedon Sep 27, 2024
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