在这种情况下它不允许 instanceof - 为什么?
public assign(color: string | ColorProperty | RgbProperty | RgbColor): void {
super.assign(color);
if (color instanceof ColorProperty) {
ps - 我爱工会!!!
最佳答案
看来instanceof
关键字必须具有类型 any
包含在联合中以便在函数中使用它。
我的猜测是编译器需要处理所有类型保护都返回 false 的情况 - 因此类型被推断为 any
.
function assign(_color: any | string | ColorProperty | RbgProperty) {
if (_color instanceof ColorProperty) {
}
// else may not be a string | ColorProperty | RbgProperty
}
关于typescript1.4 - 为什么我不能在联合上执行 instanceof?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28975200/