Skip to content

null/undefined are not acting as discriminators with object types #24479

@DanielRosenwasser

Description

@DanielRosenwasser

Found after discussing this DefinitelyTyped issue: DefinitelyTyped/DefinitelyTyped#25731 (comment)

export interface Errored {
    error: Error
    value: null
}

export interface Succeeded<Value> {
    error: null
    value: Value
}

type Result<T> = Succeeded<T> | Errored;

declare function getVal<T>(x :T): Result<T>

let x = getVal("hello");

if (x.error === null) {
//  vvvvvvv ERROR
    x.value.toUpperCase();
} 

Expected: No error
Actual: x.value is possibly null.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions