Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
@chlc3d

Description

@chlc3d
Issue body actions

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

Reactions are currently unavailable

Metadata

Metadata

Assignees

Labels

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

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.