TypeScript 中 is 关键字
本文为个人学习摘要笔记。
原文地址:TypeScript 中的 is
TypeScript 里有类型保护机制。要定义一个类型保护,我们只要简单地定义一个函数,它的返回值是一个类型谓词:
function isString(test: any): test is string {
return typeof test === 'string'
}
上述写法与写一个返回值为 boolean 值函数的区别在哪里呢?
function isString(test: any): boolean {
return typeof test