Skip to content

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

Closed
@chlc3d

Description

@chlc3d

TypeScript Version: typescript@3.4.0-dev.20190330

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

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: Conditional TypesThe issue relates to conditional types

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions