-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Void type narrowing stops working in callbacks #8541
Comments
Statically there is nothing telling the compiler that the callback is executed immediately. Thus let foo: string | void
let cbr: (cb: (err: Error) => any) => any
if (foo) {
cbr(function () { // compiler doesn't know when the callback is executed.
foo.substr(0, 10)
})
}
foo = undefined |
I'm not sure if it's related to #8353, it seems closer to #8472. They're somewhat like two sides of the same thing:
let x: number | string = "hi";
x; // 'x' is analyzed as 'string'
func();
x; // 'x' is analyzed as 'number'
function func() {
x = 1;
}
let x: number | string = "hi";
// 'x' is analyzed as 'string' for execution of this particular call,
// so this may compile:
func();
function func() {
// However, in general there is no way to know whether the type of 'x' would be
// 'number' or 'string' here for an arbitrary caller context, so it technically
// may still error during compilation, unless perhaps more complex analysis is done:
return x.charCodeAt(0);
} |
Thanks for the issues, they are all almost the same issues, but the solutions are probably much different. I understand the other issues stated are a bit more complicated - one requires understanding the call locations and another is around mutating variables outside the scope. I understand that Anyway, if it sounds similar enough I'm happy for it to be closed - as long as the issue is being tracked somewhere 😄 |
this is a duplicate of #7662. you can see more discussion in #7719 (comment) |
Actually, @mhegazy, I'd like to re-open this issue. I don't think it's entirely correct. For example, if it's a |
All the referenced issues mention specifically const x: number | number[] = []
function cb () {
x.push(1)
} It should work. The |
Const should work with latest in master. |
Thanks. Is there a particular option to make it work? I just checked out master and am stilling getting:
|
Sorry about that, should have been more specific. it only narrows in function expressions and lambdas.. so this should work: const x: number | number[] = []
var cb = function () {
x.push(1)
} now that i think about it, i do not see why this should not work for function declarations as well. |
created #8976 to track that. |
Thanks @mhegazy |
Seems like this would have been reported, but I had trouble finding a duplicate issue. Feel free to close if one does exist.
TypeScript Version:
nightly (1.9.0-dev.20160217)
Code
Expected behavior: No error
Actual behavior:
Edit: Only visible on nightly - notice that in nightly
foo.substr
within theif (foo)
is valid, but not within the callback function which is also insideif (foo)
.The text was updated successfully, but these errors were encountered: