'pf2'

问题描述 投票:0回答:1

现在,我有了这个问题,我想这样声明一个 "Record "类型。

type T_PlatformKey= 'pf1' | 'pf2' | 'pf3'
type T_PlatformInfo = {
    key: T_PlatformKey
    name: string
    [k: string]: any
}

我想声明一个像这样的 "记录 "类型。

type T_platforms = Record<T_PlatformKey, T_PlatformInfo>

const platforms: T_Platforms = {} // here is the problem

如果我不声明所有的属性,

Type '{}' is missing the following properties from type 'Record<T_PlatformKey, T_PlatformInfo>': pf1, pf2, pf3 ts(2739)

我试过其他的方法,像这样:

interface I_Platforms {
    pf1: T_PlatformInfo
    pf2: T_PlatformInfo
    pf3: T_PlatformInfo
}
const platforms: Partial<I_Platforms> = {} // it works

呃,它可以工作,但是... ? 不是很聪明。

(另外,原谅我糟糕的英语,thx)

typescript
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.