1、never 表示不存在状态,无法表示预期的状态
type A1 = string & number // type A = never 推论为never类型
function fn(): never { // 可以使用void,但是never更合理(虽然没有返回值,但是此函数执行就报错)
throw new Error('执行就报错')
}
function fn1(): never { // never更合理(虽然没有返回值,但是此函数里面是死循环)
while (true) {
// 死循环
}
}
2、忽略never(太低级,会被忽略)
type Aa = 'a' | 'b' | 'c' | 'd'
function fn2(val: Aa) {
switch (val) {
case 'a':
break
case 'b':
break
case 'c':
break
case 'd': // 新加的 'd',不然兜底会提示报错
break
default:
// never一般用在兜底逻辑中
const error: never = val
break
}
}