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
本文探讨了TypeScript中的typeof操作符,作为类型保护的一种方式,它能返回七种基本类型的字符串常量,包括'string'、'number'、'bigint'、'boolean'、'symbol'、'undefined'和'object',以及'function'。通过了解这个操作符,开发者可以更好地进行类型检查和窄化。
594

被折叠的 条评论
为什么被折叠?



