-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Flow control resets guarded variables in sub function scope #8367
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
Technically, this dupes the first half of #8270 but since that was closed as the second issue the user in that thread mentioned was fixed (and he got a workaround for this issue), this issue may be appropriate. @ahejlsberg 's prior comment on this:
|
Thanks. Since this is "clean" of the other issue, maybe it makes sense to keep this open, and for now I will feedback here. From a feedback perspective it was surprising to me that the guard didn't hold (and a breaking change in behaviour). While it is true that the callback can occur at any point, in the above use case, any conservatism is "wasted". Also, technically input could be anything by the time the return function gets called, including not being type function convertBar(input: Foo | Bar): () => string {
let foo: () => string;
if isFoo(input) {
foo = input.foo;
}
return foo ? function () {
return foo();
} : input.bar;
} To me that just seems awkward and intent a whole lot less clear. What I have done for now (which it always pains me to strong arm the compiler): function convertBar(input: Foo | Bar): () => string {
return isFoo(input) ? function () {
return (<Foo> input).foo(); // <-- I am sure I made a angel weep for doing this
} : input.bar;
} |
this is already covered in #7662. |
TypeScript Version:
nightly (1.9.0-dev.20160429)
Code
Expected behavior:
Receive no errors (as per 1.8)
Actual behavior:
Receive the error
Property 'foo' does not exist on type 'Foo | Bar'.
The text was updated successfully, but these errors were encountered: