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
typeConditionalType<T>=Textendsstring ? string : number;functionConditionalOrUndefined<T>() : ConditionalType<T>|undefined{return0asany;}functionJustConditional<T>() : ConditionalType<T>{returnConditionalOrUndefined<T>()!;// Type 'NonNullable<ConditionalType<T>>' is not assignable to type 'ConditionalType<T>'}// For comparison...functiongenericOrUndefined<T>() : T|undefined{return0asany;}functionJustGeneric<T>(): T{returngenericOrUndefined<T>()!;// no error}
Expected behavior:
JustConditional compiles without errors, like JustGeneric.
Actual behavior:
JustConditional produces error Type 'NonNullable<ConditionalType<T>>' is not assignable to type 'ConditionalType<T>'
I took a look at this out of curiosity -- it seems like the issue is caused by how conditional types are compared for relationships in structuredTypeRelatedTo. (around line 13022-13053 in src/compiler/checker.ts)
If only the source type is conditional (as in the "for comparison" case in my example code) the compiler tries to determine if the constraint of the source type is related to the target type.
If both types are conditional (as in the other case in my example), then we only consider the types related if all four of the type parameters are related in a specific way, so a "nested" conditional type like this won't necessarily work.
If I remove the "else" on line 13038 so that the "both types are conditional" behavior falls back to the less specific behavior, my problem goes away and the tests still pass, but I haven't investigated whether this naive solution might cause other issues -- perhaps it would be better to widen the acceptable relationships in the earlier branch.
Are you interested in a pull request for this? If so I can add tests and put one together.
@trstamper sorry for the huuuuge delay, but I stumbled back onto this while looking into something else, and ended up opening #37208 - you were half right, allowing conditionals to be related by constraint was just what was missing; however we did still have to omit the distributive constraint case - since it maps the check type back to its constraint (should it have one), it ends up mucking up variance calculation (which wasn't something we did for conditionals back when you first opened this, as it happens).
TypeScript Version: [email protected]
Search Terms: Conditional NonNullable Type
Code
Expected behavior:
JustConditional compiles without errors, like JustGeneric.
Actual behavior:
JustConditional produces error
Type 'NonNullable<ConditionalType<T>>' is not assignable to type 'ConditionalType<T>'
Playground Link:
https://typescript-play.js.org/?target=2#code/C4TwDgpgBAwg9gOwCYEtgsQQwDYBVwQA8uAfFALxS5QQAewEyAzlE8AE4oIDmUA-Kw5deALigIArgFsARhHZQA3AChlAMwkIAxukSxEqXQhwB5dgFVkENVwhJiJABQBKKGPjI0GY3gIOoAD5QmkjWtkhQAN7KULFQ7BDAEuwIUAAMUJgsmAggKgC+qhraRlAAUhJsHobeOA4ubvqeRjj4kP7RcfGJyanVXljYZpahNgh29c4AhIpQAPRzVARQAOQAcohrEtjYmDLYRP0tvu2kJCtQKCwIcMCZTEwo3Mb70MBwUKCQq0e1J0SkFbKQqqBZQABicAUWjgUjAmE4TEQADpUUVNDpvFBuIx5CgtMMrGMJqQGmJqEEQmFxhFOnEEkkUul7plcgV0SUsRU2ABxXGcLSTclRGL0npMnHjAWE0bhSYzeaLG40djsKHAoA
Related Issues:
It looks like NonNullable has had similar issues with other complex types:
#23849
#27456
The text was updated successfully, but these errors were encountered: