Skip to content

NonNullable<ConditionalType<T>> can't be assigned to ConditionalType<T> with --strictNullChecks #30706

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

Closed
chlc3d opened this issue Apr 2, 2019 · 2 comments · Fixed by #37208
Assignees
Labels
Bug A bug in TypeScript Domain: Conditional Types The issue relates to conditional types

Comments

@chlc3d
Copy link

chlc3d commented Apr 2, 2019

TypeScript Version: [email protected]

Search Terms: Conditional NonNullable Type

Code

type ConditionalType<T> = T extends string ? string : number ;

function ConditionalOrUndefined<T>() : ConditionalType<T> | undefined {
    return 0 as any;
}

function JustConditional<T>() : ConditionalType<T> {
    return ConditionalOrUndefined<T>()!; // Type 'NonNullable<ConditionalType<T>>' is not assignable to type 'ConditionalType<T>'
}


// For comparison...
function genericOrUndefined<T>() : T | undefined {
    return 0 as any;
}

function JustGeneric<T>(): T {
    return genericOrUndefined<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>'

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

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Apr 9, 2019
@chlc3d
Copy link
Author

chlc3d commented Apr 28, 2019

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.

@weswigham
Copy link
Member

weswigham commented Mar 4, 2020

@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).

@weswigham weswigham added Bug A bug in TypeScript Domain: Conditional Types The issue relates to conditional types and removed Needs Investigation This issue needs a team member to investigate its status. labels Mar 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Conditional Types The issue relates to conditional types
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants