我想在函数中确定参数是否属于某个类。这是不可行的想法:
function assertClass<CLASS>(v: any, theClass: CLASS, message: string): v is typeof theClass{
let is = v instanceof CLASS ; // err 'CLASS' only refers to a type, but is being used as a value here
return assert(is, message);
}
我想做什么:
let animal: Cat | Dog;
animal= somethingThatCanReturnCatOrDog();
assertClass<Cat>(animal, 'should be a Cat');
我用typeof尝试了几种组合,但是更加困惑了!