嵌套对象中基于 属性 的 Typescript 联合

Typescript Unions based on property inside nested object

我正在尝试基于对象中的嵌套 属性 创建联合类型。请参阅以下示例:

type Foo = {
    abilities: {
        canManage: boolean
    }
}

type Bar = {
    abilities: {
        canManage: boolean
    }
    extraProp: number
}

type Condition1 = {
    abilities: {
        canManage: true
    }
} & Bar

type Condition2 = {
    abilities: {
        canManage: false
    }
} & Foo

type TotalData = Condition1 | Condition2

const data: TotalData = {
    abilities: {
        canManage: false, // if canManage is false, TS should complain when I add the `extraProp` key
    },
    extraProp: 5
}

我遇到的问题是打字稿忽略了我设置的条件。如果 canMange 值为 true,我只对允许某些属性感兴趣。这在嵌套时似乎不起作用。但是,如果我没有嵌套而只是像这样的东西,那就没问题了:

type Foo = {
     canManage: boolean
}

type Bar = {
    canManage: boolean
    extraProp: number
}

type Condition1 = {
    canManage: true
} & Bar

type Condition2 = {
    canManage: false
} & Foo
]
type TotalData = Condition1 | Condition2

const data: TotalData = {

canManage: false,
extraProp: 5 // now typescript complains that this property shouldn't be here because canManage is false
}

尝试根据嵌套对象中的 属性 设置联合时,我该如何解决这个问题?

编译器不理解"nested discriminated unions"的概念。如果联合的成员共享一个公共的 "discriminant" 属性,则类型是可区​​分的联合。判别式 属性 通常是 singleton/literal 类型,如 true"hello"123 甚至 nullundefined。不过,您 不能 使用另一个被判别联合作为判别式本身。如果可以的话,那就太好了,因为那时受歧视的联合可以按照您正在做的方式从嵌套属性向上传播。 microsoft/TypeScript#18758 上有人建议允许这样做,但我没有看到那里有任何动静。

就目前而言,类型 TotalData 不是 discriminated 联合。这只是一个工会。这意味着编译器不会尝试将类型 TotalData 的值视为 独占 Condition1Condition2。因此,如果您编写测试 data.abilities.canManage 的代码并期望编译器理解其含义,您可能 运行 会遇到问题:

function hmm(x: TotalData) {
    if (x.abilities.canManage) {
        x.extraProp.toFixed(); // error!
    //  ~~~~~~~~~~~ <--- possibly undefined?!
    } 
}

如果你想这样做,你可能会发现自己需要写成 user-defined type guard functions

function isCondition1(x: TotalData): x is Condition1 {
    return x.abilities.canManage;
}

function hmm(x: TotalData) {
    if (isCondition1(x)) {
        x.extraProp.toFixed(); // okay!
    } 
}

您在此处 运行 遇到的具体问题,其中 data 被视为有效 TotalDataexcess property checking is performed. Object types in TypeScript are "open"/"extendable", not "closed"/"exact 的方式有关。允许在不违反类型的情况下添加类型定义中未提及的额外属性。因此编译器不能完全禁止多余的属性;相反,它使用试探法来尝试找出这些属性何时是错误的以及何时是故意的。规则used 主要是:如果你正在创建一个全新的对象字面量,并且它的任何属性在使用它的类型中没有被提及,就会有一个错误。否则不会有。

如果 TotalData 是一个有区别的联合,你会在 data 上得到你期望的错误,因为 data.abilities.canManage 会导致编译器从 [=] 缩小 data 19=] 到 Condition2 其中没有提到 extraProp。但事实并非如此,因此 data 仍然是 TotalData,而 提到了 extraProp

microsoft/TypeScript#20863 中有人提议,对非歧视性工会进行更严格的过度 属性 检查。我非常同意;混合和匹配来自不同联合成员的属性似乎不是常见的用例,因此警告可能会有所帮助。但同样,这是一个长期存在的问题,我没有看到任何进展。


您可以为此做的一件事是更明确地说明您要防止的过多属性。 {a: string} 类型的值可以有 string 类型的 b 属性,但 {a: string, b?: never} 类型的值不能。因此,后一种类型将阻止 b 类型的属性,而无需依赖编译器的启发式方法进行过度 属性 检查。

你的情况:

type Foo = {
    abilities: {
        canManage: boolean
    };
    extraProp?: never
}

将与您最初的 Foo 定义非常相似,但现在您会收到此错误:

const data: TotalData = { // error!
// -> ~~~~
// Type '{ abilities: { canManage: false; }; extraProp: number; }'
// is not assignable to type 'TotalData'.
    abilities: {
        canManage: false,
    },
    extraProp: 5
}

编译器无法再将 dataCondition1Condition2 协调,因此它会抱怨。


好的,希望对你有帮助;祝你好运!

Playground link to code