Typescript中的类型谓词
function isSet(variable: any): variable is Set<any> {
return variable instanceof Set;
}function isSet(variable: any): boolean {
return variable instanceof Set;
}interface Cat {
name: string;
meow(): void;
}
interface Dog {
name: string;
bark(): void;
}
function isCat(animal: Cat | Dog): animal is Cat {
return 'meow' in animal;
}
function performSound(animal: Cat | Dog) {
if (isCat(animal)) {
// 在这个块中,TypeScript 知道 animal 是 Cat 类型
animal.meow();
} else {
// 在这个块中,TypeScript 知道 animal 是 Dog 类型
animal.bark();
}
}

PreviousSimultaneously Pushing a Repository to Two Different Remotes: GitHub and GitLabNextMac Sonoma 无法安装 Charles 的解决办法
Last updated