经营范围 网站建设,做百科需要参考的网站,巩义公司做网站,给别人做的网站要复杂做安全扫描概念 在语句的块级作用域【if语句内或条目运算符表达式内】缩小变量类型的一种类型推断的行为。 类型守卫可以帮助我们在块级作用域中获得更为需要的精确变量类型#xff0c;从而减少不必要的类型断言。
类型判断#xff1a;typeof实例判断#xff1a;instanceof字面量相等…概念 在语句的块级作用域【if语句内或条目运算符表达式内】缩小变量类型的一种类型推断的行为。 类型守卫可以帮助我们在块级作用域中获得更为需要的精确变量类型从而减少不必要的类型断言。
类型判断typeof实例判断instanceof字面量相等判断, , !, ! 类型判断typeof
function test(own: string | boolean | number) {if (typeof own string) {// 这里own的类型限制为string} else if (typeof own number) {// 这里 own 的类型限制为 number} else {// 这里的类型限制为 boolean}
}很好理解吧就像JS的typeof一样。但是两者还是有区别的。 typeof 类型保护只支持两种形式 typeof value [typename]typeof value ! [typename] 这里typename 必须是 number string boolean 或 symbol。 但是 TypeScript 并不会阻止你与其它字符串比较只是语言不会把那些表达式识别为类型保护。 转成JS几乎无区别。
虽然在类型判断里ts的typeof和js的typeof看起来没有区别。但是。ts的typeof用法与js的typeof用法存在蛮大的差异。具体可以看这篇推文。 实例判断instanceof 对typeof的补充
js里也有instanceof。instance就是实例的意思建议先看看JS里instanceof和typeof的区别。 在 TypeScript 中使用 instanceof 操作符可以检查一个对象是否是某个类的实例。当我们在使用类时可以通过 instanceof 检查来确认一个实例是否符合该类的定义 class User {public nickname: string | undefinedpublic group: number | undefined
}class Log {public count: number 10public keyword: string | undefined
}function typeGuard(arg: User | Log) {if (arg instanceof User) {arg.count 15 // Error, User 类型无此属性}if (arg instanceof Log) {arg.count 15 // OK}
}为什么用instanceof呢因为typeof有局限性引用类型比如数组正则等无法精确识别是哪一个种型instanceof能够识别变量比如实例对象是否属于这个类。
但是注意interface接口是不能用instanceof去检查。 接口的 instanceof 检查需要类型谓词。
interface Foo {...}
interface Bar {...}function test(input: Foo | Bar) {if (input instanceof Foo) {// 这里 input 的类型「收紧」为 Foo} else {// 这里 input 的类型「收紧」为 Bar}
}
上面是错误的写法正确的方法可以看这篇推文 in关键字
interface one {name: string;speak: string;
}
interface two {age: number;see: string;
}
先写两个接口one、two不懂接口的可以看我这篇文章然后将这两个接口进行联合声明一种混合类型。
type customType one | two;
使用in来判断属性是否在传递的参数中然后分别作输出。类似于在JS中我们根据特定的属性判断这个对象是我们要找的哪个对象。
function test(value: one | two) {
// function test(value: type) {if(name in value) {// 因为有name 这里收紧为value 对象为 oneconsole.log(value.speak);}if(see in value) {// 因为有see 这里收紧为value 对象为 twoconsole.log(value.see);}
}缺点用 in 关键字缩小数据类型至少有一个独特的属性作为判别标准否则不能用 in 关键字