Multivit4min icon

Untitled

Multivit4min | PRO | 03/01/20 02:01:37 PM UTC | 0 ⭐ | 14312 👁️ | Never ⏰ | []
ECMAScript |

910 B

|

None

|

0 👍

/

0 👎

//global.d.ts
type ConfigMeta = StringConfig | NumberConfig
 
declare interface Config<T> {
  name: string
  value: T
}
 
interface StringConfig extends Config<String> {
  type: "string"
}
 
interface NumberConfig extends Config<Number> {
  type: "number"
}
 
interface MetaData {
  name: string,
  config: ConfigMeta[]
}
 
declare function registerPlugin(meta: MetaData, callback: (config: Record<string, any>) => void): void
 
 
 
//script.ts
registerPlugin({
  name: "some plugin",
  config: [{
    type: "string",
    name: "foo",
    value: "asdf"
  }, {
    type: "number",
    name: "bar",
    value: 123
  }]
}, (config) => {
  //the issue here, config is declared as Record<string, any>
  //however is it possible to extract type information from above config input
  //and get the correct key and associated types directly assigned?
  console.log(config) //gives { foo: "asdf", bar: 123 }
})

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎