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

Provide one of object types in 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,
}

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

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

一个选项不可用:目前 TypeScript 中没有确切的类型(unlike in Flow). There is TS issue #12936 "Exact Types" 现在仍然是开放的。

TS 的未解决问题列表中还有另一个 issue/proposal 确切地询问了您的问题:#14094 "Proposal: Allow exclusive unions using logical or (^) operator between types"

您可以使用这些解决方法:

选项#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
}

选项#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
}

郑重声明,在 Facebook 的类型系统中 Flow you can solve the problem by using a disjoint union (XOR) instead of just a union (OR) or by using exact object types or as for TS above setting the undesirable property to undefined. I made a Flow demo with your example (link)。在这种情况下,Flow 的代码与 TS 的代码相同。