Open Topppy opened 2 years ago
ts 不允许 ‘不可能’的类型断言。比如
const x = "hello" as number;
> Conversion of type 'string' to type 'number' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
这种情况下的解决方法就是断言2次,先转化为any/unknown, 再断言为其他类型
const a = (expr as any) as T;
Type 创建后不可二次修改, Interface 始终可扩展编辑的
type U= A|B
如果使用的是A 跟B都有的属性,那么ts不会报错, 如果使用的是A或者 B 独有的属性,那么使用前需要先判断具体是哪一个类型,ts才不会报错。
function printId(id: number | string) {
if (typeof id === "string") {
// In this branch, id is of type 'string'
console.log(id.toUpperCase());
} else {
// Here, id is of type 'number'
console.log(id);
}
}
项目建议开启 strictNullChecks
如果你知道变量一定存在,则可以使用:非空断言操作符
function liveDangerously(x?: number | null) {
// No error
console.log(x!.toFixed());
}
https://www.typescriptlang.org/docs/handbook/2/everyday-types.html
字面量类型推断
数字和字符串的字面量会被推断为 number 和string类型,所以常见的报错:
的解决方法