-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type validations do not carry over when assigned to a const #48891
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
Comments
It's working as intended. See #44730.
|
@MartinJohns Thanks for pointing out the PR! Exactly the issue I'm facing. In my code sample above, could you help me understand why there are errors in D and E when the expression is assigned to a |
Because of this part:
|
Ah I see! Got it. Thank you very much! For reference, making type Obj = Readonly<{
test1?: string
test2: string | null
test3: string | false
}> |
Bug Report
🔎 Search Terms
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
TypeScript can tell when the value of a variable is not falsy inside of an if block where the validation is directly used as the condition. But it fails to carry it over when the outcome of the validation is assigned to a
const
and that variable is used as the condition of the if statement instead.🙂 Expected behavior
As long as the outcome of the validation is assigned to a
const
, TypeScript should deduce that the value is not falsy.The text was updated successfully, but these errors were encountered: