【发布时间】:2015-05-24 05:08:06
【问题描述】:
是否可以创建一个 typeguard 或其他实现相同目的的东西来检查变量是否是 typescript union 中的特定接口类型?
interface Foo { a:string }
interface Bar { b:string }
(function() {
function doStuff(thing: Foo | Bar) {
if(typeof thing === 'Foo') {
console.log('Foo');
}
else if (typeof thing === 'Bar') {
console.log('Bar');
}
else {
console.log('unknown');
}
}
var thing: Foo = {a:'a'};
doStuff(thing);
})();
【问题讨论】: