typescript - 区分联合类型
问题描述
为什么我不能仅通过成员 x 是字符串而不是数字的信息来区分该联合?为什么我必须使用文字类型?
type A = { x: string ; y: string }
type B = { x: number ; y: number }
type C = A | B
function fn(p: C) {
if (typeof p.x === 'string') {
// Typescript is unable to infer p as A
// Typescript infer p.y as (string | number), why not just string ?
}
// Typescript is capable of inferring p.x as number - good
// But cannot infer p.y as number, why ?
}
对我来说,这是不可能用像 {x:string,y:number} 这样的类型调用函数的,那么为什么打字稿假设这是可能的呢?
解决方案
不是直接解释为什么 Typescript 编译器无法通过 区分联合类型typeof
,但手册说:
有以下三种成分:
- 具有共同的单例类型属性的类型——判别式。
- 一个类型别名,它接受这些类型的联合——联合。
- 在公共财产上键入警卫。
因此,为了缩小正确的类型,需要一个单例类型属性。我会使用自定义类型保护:
type A = { x: string ; y: string }
type B = { x: number ; y: number }
type C = A | B
function isA(obj: C): obj is A {
return typeof obj.x === 'string';
}
function isB(obj: C): obj is B {
return typeof obj.x === 'number';
}
function fn(p: C) {
if (isA(p)) {
// ...
}
if (isB(p)) {
// ...
}
}
推荐阅读
- php - 一个月后用户自动登录 php, mysql
- javascript - Chrome 扩展程序:X 分钟后在同一选项卡上打开链接
- html - bootstrap 3 增加文本背景颜色大小并在每个选项卡上切换
- sql-server - 如何使用 SSIS 中的平面文件目标在不覆盖文件夹的情况下获取不同的目标文件
- reactjs - Webpack 在电子关闭之前无法编译
- java - 放心 - 将 json 路径与另一个路径进行比较
- java - Java Streams列表以映射出现索引作为键
- c# - 多应用网络的最佳方法?
- python - IBM Cloud Functions - 如何使用和上传自己的库?
- c++ - 标准 Windows SDK 文件中的 C++ 编译错误