Skip to content

Inconsistent reachability with different type annotations #37472

@kanwren

Description

@kanwren

TypeScript Version: Nightly

Search Terms: control flow analysis, reachability analysis, never, type annotation

Code

function impossible1(): never {
    throw new Error();
}

const impossible2: () => never = () => {
    throw new Error();
};

const impossible3 = (): never => {
    throw new Error();
}

function foo(): never {
    // impossible1 and impossible2 work here, but not impossible3
    impossible3();
}

// but all seem to have the same type
const x: typeof impossible2 = impossible1;
const y: typeof impossible3 = impossible2;
const z: typeof impossible1 = impossible3;

Expected behavior:

The three functions with the same type should all behave the same under control flow/reachability analysis.

Actual behavior:

Using impossible3, which removes the type annotation from the LHS, does not compile, while using impossible1 or impossible2 does.

Playground Link: Playground Link

Related Issues: N/A

Other issues exist related to reachability not working as expected, but removing the type annotation like this is a strange way to run into it.

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