,
, !
, , Typescript . . , , , , .
, (), .
Typescript 1.6,
// ( ). , , (.hasOwnProperty ..), . .
function isConfigGetMsg(msg: BaseMessage): msg is ConfigGetMsg {
return msg.name === MsgTypes.CONFIG_GET_MSG;
}
if (isConfigGetMsg(msg)){
console.log(msg.key);
}
, . , , . , .
, . , , - :
interface One {
a: number
}
interface Two extends One {
b: string
}
function isAssignableTo<T>(potentialObj:Object, target:T) : potentialObj is T {
return Object.keys(target).every(key => potentialObj.hasOwnProperty(key))
}
let a: Object = {
a: 4,
b: 'hello'
};
let b: Two = undefined;
if(isAssignableTo(a, b)) {
b = a;
}
isAssignableTo. , , , , , . . , - , , , . , .
, , "" - , . , , , /. , , typescript ., tsconfig.
, api. , . isAssignableTo . , , , , .
, , . , . , ;).