You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
letfoo: string|undefined;if(!foo)foo='hello';foo.length;// oknewPromise(resolve=>resolve(foo.length));// ^ should be string, but inferred as string | undefined
Yep, see #9998 - we cutoff control flow analysis on function boundaries (mostly), so foo only has it's declared type within the Promise callback. If you capture the narrowed value with a const before the callback is made, that'll capture the narrowed value.
we cutoff control flow analysis on function boundaries (mostly)
Which makes perfect sense, because in general the compiler can't know when a function will be called--or even how many times. In the specific case of Promise we know the function will be called only once, and immediately--but this isn't an assumption the compiler can make in the general case.
Activity
GongT commentedon May 6, 2019
No, that is not ok: foo.length -> [Object is possibly 'undefined']
lll000111 commentedon May 6, 2019
初绎 (@GongT) You mean the 3rd line? There is no error in the playground, with all optional checks enabled.
j-oliveras commentedon May 6, 2019
Most likely, related to #9998.
weswigham commentedon May 8, 2019
Yep, see #9998 - we cutoff control flow analysis on function boundaries (mostly), so
fooonly has it's declared type within the Promise callback. If you capture the narrowed value with aconstbefore the callback is made, that'll capture the narrowed value.fatcerberus commentedon May 9, 2019
Which makes perfect sense, because in general the compiler can't know when a function will be called--or even how many times. In the specific case of
Promisewe know the function will be called only once, and immediately--but this isn't an assumption the compiler can make in the general case.dislido commentedon May 9, 2019
ok,It seems that it is the correct solution