Skip to content
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

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

Closed
DanielRosenwasser opened this issue May 13, 2016 · 1 comment

Comments

@DanielRosenwasser
Copy link
Member

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;
}
@mhegazy
Copy link
Contributor

mhegazy commented May 13, 2016

The issue here is that 1. the compiler does not know that these conditions are exhaustive, sometimes it involves multiple variables, and 2. we do not know that fail always throws.

we could add a syntactic form of defining that a function always throws, but do not think relaxing the check is the solution. there are valid cases, that you want the compiler to catch.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants