-
Notifications
You must be signed in to change notification settings - Fork 12.8k
bang operator resets type inference #16945
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
How about a double bang?
|
Why would you create a custom type guard that included |
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: function isFoo(arg?:any):arg is Foo{
return true;
}
function logic(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'.
}
}
} |
I think there are some problems with how interface Foo {
foo;
}
declare function isFooMaybe(x: any): x is Foo | undefined;
declare var a: object | undefined;
if (isFooMaybe(a)) {
var b = a // (object & undefined) | (object & Foo)
} |
TypeScript Version: 2.3.4
Code
Expected behavior:
arg!
should resolve to foo if it's already inferred thatarg is Foo|undefined
Actual behavior:
arg!
is resolved toobject
The text was updated successfully, but these errors were encountered: