如何申报;“记录”;在typescript中使用部分特定键键入?

如何申报;“记录”;在typescript中使用部分特定键键入?,typescript,Typescript,现在,我明白了: 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 = {

现在,我明白了:

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
interface I_Platforms {
    pf1: T_PlatformInfo
    pf2: T_PlatformInfo
    pf3: T_PlatformInfo
}
const platforms: Partial<I_Platforms> = {} // it works
类型T\u平台=记录
const platforms:T_platforms={}//问题就在这里
如果我没有声明所有属性:

Type '{}' is missing the following properties from type 'Record<T_PlatformKey, T_PlatformInfo>': pf1, pf2, pf3 ts(2739)
类型“{}”缺少类型“Record”中的以下属性:pf1、pf2、pf3 ts(2739)
我试过其他类似的方法:

type T_platforms = Record<T_PlatformKey, T_PlatformInfo>

const platforms: T_Platforms = {} // here is the problem
interface I_Platforms {
    pf1: T_PlatformInfo
    pf2: T_PlatformInfo
    pf3: T_PlatformInfo
}
const platforms: Partial<I_Platforms> = {} // it works
接口I\u平台{
pf1:T_平台信息
pf2:T_平台信息
pf3:T_平台信息
}
const platforms:Partial={}//它可以工作
嗯,这很有效,但是。。。?不太聪明

(顺便说一句,请原谅我糟糕的英语,thx)

您可以使用映射类型(类似于
记录
实现)并指定每个键都是可选的:

type T_Platforms = { [Key in T_PlatformKey]?: T_PlatformInfo }

您计划如何使用
T_平台
?我怀疑
Record
不是你想要的。也许是地图?