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
{{ message }}
Repository navigation
Type guard not working in returned function #12616
functiongetGetter(){letdata={}data['bar']=1if(hasBar(data)){return()=>data.bar// Property 'bar' does not exist on type '{}'.}}functionhasBar(thing): thing is {bar: number}{returntypeofthing.bar=='number'}
Expected behavior:
Compile successfully.
Actual behavior:
index.ts(6,21): error TS2339: Property 'bar' does not exist on type '{}'.
Your example works if you use const to declare data. Type guards for let variables are not in effect in nested functions because the variable might be reassigned. See #8849.
Activity
ahejlsberg commentedon Dec 3, 2016
Your example works if you use
constto declaredata. Type guards forletvariables are not in effect in nested functions because the variable might be reassigned. See #8849.andraaspar commentedon Dec 3, 2016
I see. You are right.