在打字稿中提供一种对象类型

lor*_*ort 5 types object-literal typescript

如果我输入以下内容:

interface A {
    x: number
}
interface B {
    y: number
}

type Z = A | B;

// here it allows me to create a variable of type Z with both members of type A and B.
let z: Z = {
    x: 5,
    y: 6,
}
Run Code Online (Sandbox Code Playgroud)

我无法确保 Z 类型的对象确实包含 A 的所有成员但不包含 B 的成员(或相反)。使用 TypeScript 有可能吗?经过大量研究,我倾向于“不”的答案,但我不确定。

Mör*_*rre 5

默认情况下,联合将所有提供的选项合并为一个选项,但有两个选项供您选择。你想要的在 TypeScript 中是不可用的,但在 TS 项目的列表中有类似的问题(状态:此时“打开”)以及两个很好的解决方法。

一种选择不可用:此时 TypeScript 中没有确切的类型(与 Flow 不同)。有 TS 问题#12936“Exact Types” ,现在仍然是开放的。

在 TS 的未解决问题列表中还有另一个问题/提案,它确切地询问您的问题是什么:#14094“提案:允许在类型之间使用逻辑或 (^) 运算符进行排他联合”

您可以使用以下解决方法:

选项1

在 TypeScript 和 Flow 中,您可以使用标记类型来创建 XOR 联合而不是 OR 联合。

interface A {
    kind: 'A',
    x: number
}
interface B {
    kind: 'B',
    y: number
}

type Z = A | B;

// here it DOES NOT allow to create a variable of type Z with both members of type A and B.
let z: Z = {
    kind: 'A',
    x: 5
    // y: 6 will produce an error
}
Run Code Online (Sandbox Code Playgroud)

选项#2

第二个选项是设置所有类型的所有属性,但将那些不应该存在的属性设置为undefined

interface A {
    x: number,
    y?: undefined
}
interface B {
    x?: undefined,
    y: number
}

type Z = A | B;

let z: Z = {
    y: 5
    // y: 6 will produce an error
}
Run Code Online (Sandbox Code Playgroud)

作为记录,在 Facebook 的类型系统Flow 中,您可以通过使用不相交联合 (XOR) 而不是联合 (OR) 或使用精确的对象类型或对于上面的 TS 将不需要的属性设置为 来解决问题undefined我用你的例子(链接)做了一个 Flow 演示。在这种情况下,Flow 的代码与 TS 的代码相同。