-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
2.0: Type guards not working inside callbacks #7719
Comments
The new behavior is correct, since TS cannot know when the callback will execute. Consider: var x: {} = "a";
if (typeof x === "string") {
setTimeout(() => console.log(x.charAt(0)), 0);
}
x = 5; It is wrong to assume The fix is to bind it to a scoped local: var x: {} = "a";
if (typeof x === "string") {
let y = x;
[1, 2].map(i => y.charAt(i));
setTimeout(() => console.log(y.charAt(0)), 0);
} |
Also instead of scoped locals, we can use const. This works just fine: function f(x: {}) {
const y = x;
if (typeof y === 'string') {
y.charAt(0);
[1, 2].map(i => y.charAt(i));
}
} Having to rename function parameters to use I even think that |
This appears to have started with commit dad0564
The text was updated successfully, but these errors were encountered: