Typescript的typeof是一种类型保护,但是它只能返回以下值:
- “string”
- “number”
- “bigint”
- “boolean”
- “symbol”
- “undefined”
- “object”
- “function”
例如:
const str:string = "abc";
typeof str === "string"; // True
const date:Date = new Date();
typeof date === "Date"; // False
参考资料:
TypeScript-Doc-Narrowing