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

Update Variable Type After a Type Narrowing #58913

Closed
6 tasks done
titouandk opened this issue Jun 18, 2024 · 2 comments
Closed
6 tasks done

Update Variable Type After a Type Narrowing #58913

titouandk opened this issue Jun 18, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@titouandk
Copy link

πŸ” Search Terms

narrowing, type, variable, redefinition

βœ… Viability Checklist

⭐ Suggestion

TypeScript should understand that the type of a variable has changed, following a type narrowing:

type-narrowing

Playground link:
https://www.typescriptlang.org/play/?#code/KYDwDg9gTgLgBAMwK4DsDGMCWEVzAQygGdgAVCMTNASQBMAKNYAG2YDV9mlgAuOVANYoIAdxQBKPiiQBbAEbAocAN4AoAJDNg8GBSp04AXjhNWHLsADcqjQHpbJ-ER0Q4zqJhQBzOJgR4IIiJMOS0NPzh6GABPMGAIf11KGlojQ2MAIml5RQzxFQ11JP1U42KUgDpdAGUYD296cWt1AF8bdQio2PjEvRS4AEJ0uAz3Ty88gvUigAsoUTgUYBE4AFEoeah6DOoUADdOTFTy31o85raNcoMyvroKojBmTBhtiryAbQAGAF1m69KcAAcrIFFAKgRiMBdq8AU12p0QTlwZgiED8ECondaOJ8mppjA5gslit1pt6AADXYHZ7HPqnEYAEmUAJaGQp8Na7Sg2iQUFwAOsbSAA

πŸ“ƒ Motivating Example

TypeScript does not understand that the type of a variable has changed following a type narrowing.

The type of a variable is announced as the type with which the variable has been declared - even if its type has been refined since via type narrowing.

πŸ’» Use Cases

What workarounds are you using in the meantime?

Creating a copy of the variable. TypeScript understands the type of the new variable.

@MartinJohns
Copy link
Contributor

Duplicate of #27706.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 18, 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 Jun 21, 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