如何声明一种类型的对象的键和价值与TS中的枚举相关联?
如何在打字稿中声明对象的类型,其密钥是枚举的枚举成员,其值是一个函数,并且此函数的参数与密钥有关?
Details are as follows:
enum Type {
'select'='select',
'upload'='upload'
}
type selectProps = {
options: any[],
defaultCheckedValue: string
}
type uploadProps = {
url:string[],
request:()=>Promise<any>
}
// The otherConfig parameters of the select and upload functions are extended keys based on otherConfig
interface otherComonConfig{
displayDependOnKeys:Record<string,any>
}
// How to define the correspondence between key and value
type RenderFunc = {
[key in Type]:<T,U extends otherComonConfig,R>(prop:string,label?:string,options?:T,otherConfig?:U) => R
}
let map:RenderFunc = {
[Type.select]:(prop,label,options,otherConfig) =>{
return {
prop,
label,
key:"select",
options,
otherConfig
}
},
[Type.upload](prop,label){
return {
prop:"handleContent",
key:'upload'
};
}
}
// I hope when i call this methods the options auto map selectOptions,
// At the same time I can also customize the otherConfig type
// How can i achieve?
map.select('32','dwdw',);
I've tried many ways, but it always prompts the wrong type,Can anyone help me?
How to declare the type of an object in typescript, its key is an enumeration member of enum, its value is a function, and the parameters of this function are related to the key?
Details are as follows:
enum Type {
'select'='select',
'upload'='upload'
}
type selectProps = {
options: any[],
defaultCheckedValue: string
}
type uploadProps = {
url:string[],
request:()=>Promise<any>
}
// The otherConfig parameters of the select and upload functions are extended keys based on otherConfig
interface otherComonConfig{
displayDependOnKeys:Record<string,any>
}
// How to define the correspondence between key and value
type RenderFunc = {
[key in Type]:<T,U extends otherComonConfig,R>(prop:string,label?:string,options?:T,otherConfig?:U) => R
}
let map:RenderFunc = {
[Type.select]:(prop,label,options,otherConfig) =>{
return {
prop,
label,
key:"select",
options,
otherConfig
}
},
[Type.upload](prop,label){
return {
prop:"handleContent",
key:'upload'
};
}
}
// I hope when i call this methods the options auto map selectOptions,
// At the same time I can also customize the otherConfig type
// How can i achieve?
map.select('32','dwdw',);
I've tried many ways, but it always prompts the wrong type,Can anyone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论