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
typeFoo={foo:string;};functionisFoo(arg?:any):arg is Foo|undefined{returntrue;}functionlogic(arg?:object){if(isFoo(arg)){arg.foo;//Error:(..., 13) TS2532:Object is possibly 'undefined'. arg!.foo;//Error:(..., 18) TS2339:Property 'foo' does not exist on type 'object'.}}
Expected behavior: arg! should resolve to foo if it's already inferred that arg is Foo|undefined Actual behavior: arg! is resolved to object
It isn't a realistic example, but it's a clear and simple reproduction of inference information that gets lost by using the bang operator. The real-life scenario is a lot messier.
consider this example then:
functionisFoo(arg?:any):arg is Foo{returntrue;}functionlogic(arg?:object,b?:boolean){if(isFoo(arg)){arg=b? undefined : arg;if(!b){arg.foo;//Error:(..., 13) TS2532:Object is possibly 'undefined'. arg!.foo;//Error:(..., 18) TS2339:Property 'foo' does not exist on type 'object'.}}}
Activity
DeDuckProject commentedon Jul 5, 2017
How about a double bang?
[-]band operator resets type inference[/-][+]bang operator resets type inference[/+]kitsonk commentedon Jul 5, 2017
Why would you create a custom type guard that included
undefined? That doesn't sound very realistic.amir-arad commentedon Jul 5, 2017
It isn't a realistic example, but it's a clear and simple reproduction of inference information that gets lost by using the bang operator. The real-life scenario is a lot messier.
consider this example then:
DanielRosenwasser commentedon Jul 6, 2017
I think there are some problems with how
objectgets narrowed in general. For example:9 remaining items
RyanCavanaugh commentedon Oct 28, 2025
This works now