: , , , , interface type. JavaScript instanceof , , TypeScript .
, .
, , , TypeScript , guard, true ( - ""). " arg is T), :
interface A {
foo: string;
}
interface B {
bar: number;
}
function isA(obj: any): obj is A {
return obj.foo !== undefined
}
function isB(obj: any): obj is B {
return obj.bar !== undefined
}
function func(obj: any) {
if (isA(obj)) {
obj.foo;
}
else if (isB(obj)) {
obj.bar;
}
}
type-guard, , true false. , , , ( ), , . , , , .