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 guards don't work for interfaces with only optional properties #5842
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`}
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.
Activity
Use custom type guards for legend as well
mhegazy commentedon Dec 1, 2015
For a type guard, it will look through all the union type constituents (
Bandbooleanin the example), and will filter out types that are not assignable to the target type. sinceBhas only optional properties, booelan is assignable to, and it is not filtered out from the union. looks like a good candidate for #3842.a work around is to tag type B, see #4895