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

Better usage of never return types #20822

Closed
antialize opened this issue Dec 20, 2017 · 2 comments
Closed

Better usage of never return types #20822

antialize opened this issue Dec 20, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@antialize
Copy link

The control flow analysis with never return types could be improved.

Consider:

function test1_func() : never {
    throw Error("E");
}

function test1(value: "cat" | 5) {
    // test1_func never returns so there is no exit from the if code path, 
    // therefore we can assume that value is "cat" after the if.
    if (value == 5) test1_func();
    return value.length; 
}

Since test1_func never returns I would expect the type of value at the return statement to be narrowed to "cat".

Consider:

function test2_func()  {
    throw Error("E");
}

I would expect the return type to be deduced as never instead of void.

Finally with a bit better analysis an assert function could be implemented

function test3_func(value:true): void;
function test3_func(value:false): never;
function test3_func(value:boolean) {
    if (!value) throw Error("E");
}
function test3(value:number|null) {
    test3_func(value != null);
    return value + 5;
}

Here since the false value variant never returns, it can be deduced that value must be a number after the call to test3_func.

I though about implementing this my self, but I have no idea of where in the somewhat long cheker.ts.

@DanielRosenwasser
Copy link
Member

Looks related to #8655.

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants