I use TypeScript protection in a ternary statement in a loop and see a behavior that I don't understand.
My interfaces
interface INamed { name: string; } interface IOtherNamed extends INamed { otherName: string; }
Protection of my type
function isOther(obj: any): obj is IOtherNamed { ...
General usage example
var list: Array<{named: INamed}> = []; for(let item of list) { var other: IOtherNamed = ... }
Inside my for .. loop, I use my guard type to assign my current element or null to the IOtherNamed variable.
This does not work
// Compiler Error: INamed is not assignable to IOtherNamed for(let item of list) { var other: IOtherNamed = isOther(item.named) ? item.named : null; }
It does
for(let item of list) { var named: INamed = item.named; var other2: IOtherNamed = isOther(named) ? named : null; }
My questions
- Is it by design that one of them works and the other does not?
- If by design, what nuance here determines when it works or not? In particular, why does disabling my object of a new variable (without any type change) get rid of a compiler error?
source share