enum InputType {
Text = 'text',
Number = 'number'
}
type InputTypeMapping = {
[InputType.Text]: string,
[InputType.Number]: number
}
const inputConfig = {
full_name: {
label: 'Name',
type: InputType.Text
},
age: {
label: 'Age',
type: InputType.Number
}
}
基于上面的配置,我将一个表单渲染到浏览器,表单提交的预期输出对象应该是这样的:
{
full_name: '',
age: 0
}
我想根据上inputConfig
一个片段中的对象为预期输出创建对象类型。每个键 ( keyof inputConfig
) 应映射到适当的输入类型映射 ( typeof InputTypeMapping[inputConfig[key].type]
)。
我找不到创建这种类型的方法。预期的类型应该是这样的([key in keyof typeof memberInfoKeys]
有效,但typeof InputTypeMapping[inputConfig[key].type]
无效,只是为了给你一个我所期待的图片)
type FormOutput = {
[key in keyof typeof memberInfoKeys]: typeof InputTypeMapping[inputConfig[key].type]
}
有没有可能在 Typescript 中创建这种类型的方法?