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

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

Closed
Rycochet opened this issue Mar 20, 2017 · 4 comments · Fixed by #56908
Closed

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

Rycochet opened this issue Mar 20, 2017 · 4 comments · Fixed by #56908
Labels
Duplicate An existing issue was already created

Comments

@Rycochet
Copy link

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"'.

@gcnew
Copy link
Contributor

gcnew commented Mar 21, 2017

Use const. let bindings are reset because the function might be invoked asynchronously and a new value could be set to the let variable in the mean time.

@Rycochet
Copy link
Author

That sort of makes sense (though this is just a sample, the real code I noticed it on is most definitely not a constant). Wouldn't it be better to annotate things that are definitely not asynchronous to prevent this spurious warning?

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label May 24, 2017
@ghost
Copy link

ghost commented Nov 2, 2017

Duplicate of #9998, also #11498.

@ghost ghost added Duplicate An existing issue was already created and removed Needs Investigation This issue needs a team member to investigate its status. labels Nov 2, 2017
@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 Jun 21, 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

Successfully merging a pull request may close this issue.

4 participants