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
interfaceNestedType<A,Bextendsstring|number>{a: A;b: B;}exporttypeComplexType<T>=TextendsNestedType<infer A, infer B>
? {items?: T[],someA?: A,someB?: B}
: never;functiongetItems<T>(complex: ComplexType<T>){complex={};// not assignable (1)returncomplex.items||[];}
Expected behavior:
I feel like I should not be able to call a function where a parameter resolves to never, and therefore (1) should not be able to fail, but maybe I am just fundamentally misunderstanding this aspect of the type system.
Additionally, I would expect the error message to expand on the incompatibility, instead of just stating it.
Actual behavior:
Assignment fails because of seemingly incompatible types. My IDE (IntelliJ IDEA) resolves this type to the following, which does not make any sense to me:
{items?: T[],someA?: A,someB?: B}&NestedType<A,B>// where does this come from?
The immediate reason why this fails (and why there is no elaboration of the error) is that there simply is no assignability relation for conditional types in higher order. See #26933. The second reason is that clearly ComplexType<T>can reduce to never, and when we check that the function body makes sense for possible instantiations of T, it’s sort of irrelevant whether or not you’re going to have trouble actually calling the function with that instantiation. After all, you can pass a never to a function that accepts a never, and just because this is a semantically fraught scenario doesn’t mean we can just pretend such an instantiation is not in the domain of the type parameter.
Additionally, I would expect the error message to expand on the incompatibility, instead of just stating it.
Well, the error would basically be calling out a limitation of the compiler that might change, not telling you an actual type-theoretical reason why assignability isn’t possible. Of the cases where we issue this error with no further details, some are only because the compiler doesn’t have the ability to reason about the relationship, but others would still be problematic code even if the compiler could reason about it. And we obviously can’t tell the difference between those cases; if we could, the former set would not be errors.
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.
TypeScript Version: v4.2.0-dev.20201123
Code
Expected behavior:
I feel like I should not be able to call a function where a parameter resolves to
never
, and therefore (1) should not be able to fail, but maybe I am just fundamentally misunderstanding this aspect of the type system.Additionally, I would expect the error message to expand on the incompatibility, instead of just stating it.
Actual behavior:
Assignment fails because of seemingly incompatible types. My IDE (IntelliJ IDEA) resolves this type to the following, which does not make any sense to me:
Playground Link:
https://www.typescriptlang.org/play?ts=4.2.0-dev.20201123#code/JYOwLgpgTgZghgYwgAgHIQM6QCYBUCeADhADwCCANMgELIQAekI2GyWUoA5sgD7IgBXALYAjaAD5kAbwBQyecjgAuZGQDcchSJXUNAXxkyGhAPZQwyMERQBhE0MIAbBgWIlckgLzJcdRhGZWdCwIPGsSUBhoVSpI6OpxTXkAfmlgSCEMZJVcAG0AXSoMewgybJi2Eupy6gMFZBUQCAA3aA0ZGAEQBDBgExBkTggwAEkMjHdxAAoEeycGFTsHZ3pXUg8ASmkk5FnlhmRvKT01ZAB6M-4TCzgMDGBOEDgRZ2QpgEYNnahhgSgBvbzegAOnSEEyvD4BX0MiAA
The text was updated successfully, but these errors were encountered: