'How to declare a type whose key and value of an object are associated with enum in TS?
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?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
