We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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", "type narrowing within loop"
https://www.typescriptlang.org/play?ts=5.2.2#code/FAFwngDgpgBAyiKEYF4YG9g2zAzoiAOQEMBbKALjxACcBLAOwHMscGoAPEBJKn5AD4wGAVwA2YgNzAAvsGABjAPYN81JABk6atAG0A5MX0AaGPoBGJswv0BdaWKggYACyjEAJvz4EYQ0RKowuJSwI7OEDRQAG7e8L7+IUEBocD4mtogAHQAZko0AKLECi4AFKXpRGRQAJSoAHwYrNjKqs7sAO5x-EGYOP3qVeTGzQPsXHEpo3KjdDkwpW6ePSirwRJ1fQPYS16+aJ380v0z-XMLkTH8m6P9l7EEWePc+8JQXQTHOKc49ytvHyQ0hkNXk4GgMEYOSgNCiHgAKpBYGhwVAlPNdvxgAB6bHYAB6AH5gEA
type Step = { stepName: string nextStep: Step | null; } const stepList = ['a', 'b', 'c']; let headStep: Step | null = null; let prevStep: Step | null = null; stepList.forEach((stepName) => { const newStep: Step = { stepName, nextStep: null } if (headStep === null) { headStep = newStep; } if (prevStep) { prevStep.nextStep = newStep; } prevStep = newStep; }) type inferredType = typeof headStep // ^?
type inferredType = typeof headStep // ^? type inferredType = null
type inferredType = typeof headStep // ^? type inferredType = Step | null
Whenever headStep is reassigned within the forEach loop, the Step gets filtered out from the original type (wrongly narrowing).
headStep
Step
The text was updated successfully, but these errors were encountered:
Effects of callbacks aren't modeled in CFA, so your example is equivalent to
type Step = { stepName: string nextStep: Step | null; } const stepList = ['a', 'b', 'c']; let headStep: Step | null = null; let prevStep: Step | null = null; type inferredType = typeof headStep // ^?
which is correct.
See #11498
Sorry, something went wrong.
Ohhh got it, thanks for the explanation @RyanCavanaugh! Just realised that I π the original issue at some point in the past. I'll close this one.
No branches or pull requests
π Search Terms
"type narrowing", "type narrowing within loop"
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?ts=5.2.2#code/FAFwngDgpgBAyiKEYF4YG9g2zAzoiAOQEMBbKALjxACcBLAOwHMscGoAPEBJKn5AD4wGAVwA2YgNzAAvsGABjAPYN81JABk6atAG0A5MX0AaGPoBGJswv0BdaWKggYACyjEAJvz4EYQ0RKowuJSwI7OEDRQAG7e8L7+IUEBocD4mtogAHQAZko0AKLECi4AFKXpRGRQAJSoAHwYrNjKqs7sAO5x-EGYOP3qVeTGzQPsXHEpo3KjdDkwpW6ePSirwRJ1fQPYS16+aJ380v0z-XMLkTH8m6P9l7EEWePc+8JQXQTHOKc49ytvHyQ0hkNXk4GgMEYOSgNCiHgAKpBYGhwVAlPNdvxgAB6bHYAB6AH5gEA
π» Code
π Actual behavior
π Expected behavior
Additional information about the issue
Whenever
headStep
is reassigned within the forEach loop, theStep
gets filtered out from the original type (wrongly narrowing).The text was updated successfully, but these errors were encountered: