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
For some reason type guards to not work if an interface has only optional properties.
interfaceA{foo : number;}functionisA(object: any): object is A{returntrue;}leta: A|boolean;if(isA(a)){a.foo;// compiles}interfaceB{foo? : number;}functionisB(object: any): object is B{returntrue;}letb: B|boolean;if(isB(b)){b.foo;// does not compile because type inference says its `B | boolean`}
The text was updated successfully, but these errors were encountered:
For a type guard, it will look through all the union type constituents (B and boolean in the example), and will filter out types that are not assignable to the target type. since B has only optional properties, booelan is assignable to, and it is not filtered out from the union. looks like a good candidate for #3842.
For some reason type guards to not work if an interface has only optional properties.
The text was updated successfully, but these errors were encountered: