如何创建具有索引签名和不同类型的固定属性的 TypeScript 接口?

k0p*_*kus 5 json interface typescript index-signature

从旧的 api 我得到一个 JSON 响应,如下所示:

const someObject = {
    "general": {
        "2000": 50,
        "4000": 100,
        "8000": 200,
    },
    "foo": [
        0,
        1,
        2,
    ],
    "bar": [
        5,
        7,
    ],
    "baz": [
        8,
        9,
    ],
};
Run Code Online (Sandbox Code Playgroud)

请记住,除“general”之外的所有索引都是动态的,可能不在响应中,我无法为每个属性键入但必须使用索引签名。

我想通过 typescript@2.9.2 来实现:

interface ISomeObject {
    general: {
        [index: string]: number;
    };

    [index: string]?: number[];
}
Run Code Online (Sandbox Code Playgroud)

因为general总是会在响应,但其他指标可能会或可能不会在那里。

我面临的问题:

  • 我不能做[index: string]?: number[]可选的,因为它会抱怨这里使用数字作为值。
  • [index: string]: number[]将覆盖的定义,general: number因此 tsc 会抱怨:

    Property 'general' of type '{ [index: string]: number; }' is not assignable to string index type 'number[]'.`
    
    Run Code Online (Sandbox Code Playgroud)

我什至可以使用 TypeScript 接口输入这种格式吗?

Fen*_*ton 5

这是TypeScript Dictarray 概念的变体。

欺骗修复是告诉 TypeScript 一切都很好,你知道你在做什么:

interface ISomeObject {
    [index: string]: number[];
    // @ts-ignore: I'm creating a Dictarray!
    general: {
        [index: string]: number;
    };
}
Run Code Online (Sandbox Code Playgroud)

编译器将正确推断返回类型,在本例中为数字:

let x: ISomeObject;

const a = x.general['idx'];
const b = x['idx'];
Run Code Online (Sandbox Code Playgroud)

链接的文章有更多信息,但这是您特定情况下的要点。

  • 最好使用 `let x = { general: {}} as ISomeObject;` - 当你开始使用它时,联合的限制是不断地填充到两种更窄的类型之一。我更喜欢早节而不是晚节:) (3认同)