Skip to content

Consider loosening strict null checks & implicit return checking in the presence of the 'nothing' type #8602

Closed
@DanielRosenwasser

Description

@DanielRosenwasser

From a discussion with @wycats.

Currently, even if a type is narrowed down to nothing, we still consider control flow errors in those branches even though you might just be handling error cases:

// Error: Not all code paths return a value.
function foo(x: string | number) {
    if (typeof x === "string") {
        return true;
    }
    else if (typeof x === "number") {
        return false;
    }
    fail("Unexhaustive!");
}

function fail(message: string) {
    throw message;
}

Similarly, if you add a type annotation, you'll get an error.

// Error: Function lacks ending return statement but return type lacks 'undefined'
function foo(x: string | number): boolean {
    if (typeof x === "string") {
        return true;
    }
    else if (typeof x === "number") {
        return false;
    }
    fail("Unexhaustive!");
}

function fail(message: string) {
    throw message;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions