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 of outer variable inside functions is not affected by narrowing in outer scope #60046

Closed
u130b8 opened this issue Sep 24, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@u130b8
Copy link

u130b8 commented Sep 24, 2024

πŸ”Ž Search Terms

type inside function, scope variable, type narrowing

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about this

⏯ Playground Link

https://www.typescriptlang.org/play/?target=99&ts=5.7.0-dev.20240924#code/DYUwLgBAtghgngIxAZTAJwJYDsDmAuCAZ3WxwB8sBXYYCAXgipoG4AoWRFE3eiAIgAefNu3hJUmXGwD00iAD0A-K1YBjAPZZiEAGYBGXgAoAlPQB8EAN6sIt6GK6ScbOxFkLlAXxU7KWVWAYmroATCZWNnYc4tzOkbbuSqyeQA

πŸ’» Code

let maybeString: string | null = null;
maybeString = "x";

maybeString;
// ^? let maybeString: string

const f1 = () => {
    maybeString;
    // ^? let maybeString: string | null
}

function f2() {
    maybeString;
    // ^? let maybeString: string | null
}

πŸ™ Actual behavior

The type of maybeString in both functions is string | null .

πŸ™‚ Expected behavior

The type of maybeString in both functions should be string as it was narrowed before these functions were defined.

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

Duplicate of #9998.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 24, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 27, 2024
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