Skip to content

Type of variable is narrowed immediately after definition #44824

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

Closed
DanielSWolf opened this issue Jun 30, 2021 · 5 comments
Closed

Type of variable is narrowed immediately after definition #44824

DanielSWolf opened this issue Jun 30, 2021 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@DanielSWolf
Copy link

Bug Report

πŸ”Ž Search Terms

let, control flow, null, closure, lambda, arrow function

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

function execute(callback: () => void) {
  callback();
}

let value: string | null = null;

// At this point, immediately after its definition, the type of `value` has been narrowed to `null`.

execute(() => { value = 'test'; });

// Predictably, the type of `value` is still `null` here.

πŸ™ Actual behavior

Right after the line let value: string | null = null;, TypeScript narrows the type of value to null.

πŸ™‚ Expected behavior

Right after the line let value: string | null = null;, I would have expected the type of value to be string | null, as declared.

I understand that TypeScript performs narrowing when some operation might effectively change the type of a variable. But right after the definition of a variable, I'd like TypeScript to just trust my judgement. It's completely ignoring the type I explicitly specified, resulting in wrong typing.

@MartinJohns
Copy link
Contributor

TypeScript narrowing it to null is intentional.
TypeScript not recognizing the assignment is a duplicate of #9998.

This is working as intended.

You can workaround this by using null as string | null.

@DanielSWolf
Copy link
Author

I'm aware of the limitations regarding functions, but the null-narrowing behavior is new to me. @MartinJohns Can you point me to an issue containing the rationale?

@MartinJohns
Copy link
Contributor

MartinJohns commented Jun 30, 2021

Honestly, I'm too lazy to look it up right now. :-D

But the rationale is simple. Just take this small example:

let value: number | null = 123;
value.toFixed()

Should this code compile? According to the type annotation it can be either number or null, but null does not have a toFixed function. But TypeScript can see that you always assign a number to it, so calling the function should work, right? That's the behavior you see. Assignment narrows the type of the variable to the assigned type.

And TypeScript treats variable initializations just as assignments. So it's essentially:

let value: number | null;
value = 123;

@jcalz
Copy link
Contributor

jcalz commented Jun 30, 2021

Looks like a duplicate of #8513.

This is working as intended. Unions have been narrowed via control flow analysis since #8010.

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Jun 30, 2021
@DanielSWolf
Copy link
Author

@jcalz Thanks for pointing me at the right issues. This is indeed a duplicate of #8513. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants