TypeScript - 描述一个可变长度的数组,但至少有一个特定的强制条目

TypeScript - Describing an array of variable length but with at least one specific mandatory entry

我想知道这是否可行

type Product = {
    name: string
    rules: [
        ProductRule<'forSale', boolean>,
        ...Array<ProductRule<ProductRuleKey, any>>
    ]
}

我在 rules 类型 Product 键中说的是一个数组,必须有一个名为 forSale 的规则,但可以有任意数量的任何其他规则键。

或者当然我只是将需要的一个从数组中移出,但不太优雅。

按照罗伯托的说法,这正是您的做法。