Skip to content

Type narrowing of string literal seems to get loosened in callbacks #14748

Closed

Description

TypeScript Version: 2.2.1

Code

function myValueArg(arg: "value") { }

function myStringArg(arg: string) { }

namespace ns { // Not needed, but shows it's not scope related
    let str = "value";

    if (str === "value") {
        myValueArg(str);
        [].forEach(() => {
            myValueArg(str); // Argument of type 'string' is not assignable to parameter of type '"value"'.
        });
    } else {
        myStringArg(str);
        [].forEach(() => {
            myStringArg(str);
        });
    }
}

Expected behavior:
No warnings: the value (and type) of str has not changed.

Actual behavior:
Warning Argument of type 'string' is not assignable to parameter of type '"value"'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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