Interface [key in StringEnum] 当不是 StringEnum 的所有元素都作为键存在时?

Interface [key in StringEnum] when not all elements of StringEnum will exist as keys?

考虑以下代表商店的示例数据,其中包含待处理和已完成的采购订单 ID 列表:

{
    "shop-45": {
        "FULFILLED": [55, 70]
    },
    "shop-46: {
        "PENDING": [59, 54]
        "FULFILLED": [100, 101]
    }
}

如果商店既没有待定订单也没有已完成订单,则根本不会出现在列表中。

我尝试使用以下内容来表示:

type Status = "PENDING" | "FULFILLED";

interface ShopList {
    [shop: string]: {
        [status in Status]: number[];
    }
}

不出所料,当我没有 PENDINGFULFILLED 作为商店的子属性时,tsc 会抱怨。

我通过将 属性 设为可选 ([status in Status]?: number[]) 来平息错误,但我认为这不是我真正想要做的,因为商店永远不会有零个子属性.

另一个异想天开的暗中尝试 [status in Partial<Status>]: number[]; 也有类似的抱怨。

这是我唯一的选择,根本不用担心吗?

这是一个普通的 MCVE;真正的要复杂得多,层次也更多。这就是(总是?)使用泛型而不是重复每个可能的键名的动机:枚举被用作其他对象中的字段值。

TypeScript/issues 参考讨论相似的情况:7374 | 19211 | 14934 | 5683.

如果您想阻止商店在没有 PENDINGFULFILLED 的情况下不会出现的情况,您需要对您的类型更加明确一些。例如,您可以执行以下操作:

type ShopWithPending = {
    PENDING: number[];
}

type ShopWithFulfilled = {
    FULFILLED: number[];
}

type ShopStatus = ShopWithPending | ShopWithFulfilled | (ShopWithPending & ShopWithFulfilled);

interface ShopList {
    [shop: string]: ShopStatus
}

然而,这确实使得从列表中拉出 shops 变得困难,因为 Typescript 会将 ShopStatus 类型视为不保证具有任何属性的东西,因此不允许您尊重 PENDINGFULFILLED.

为了找回该能力,您需要 Shop 类型上的其他内容,以允许 Typescript 将类型推断缩小到特定类型的版本。