Advertisement
Multivit4min

Untitled

Mar 1st, 2020
834
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //global.d.ts
  2. type ConfigMeta = StringConfig | NumberConfig
  3.  
  4. declare interface Config<T> {
  5.   name: string
  6.   value: T
  7. }
  8.  
  9. interface StringConfig extends Config<String> {
  10.   type: "string"
  11. }
  12.  
  13. interface NumberConfig extends Config<Number> {
  14.   type: "number"
  15. }
  16.  
  17. interface MetaData {
  18.   name: string,
  19.   config: ConfigMeta[]
  20. }
  21.  
  22. declare function registerPlugin(meta: MetaData, callback: (config: Record<string, any>) => void): void
  23.  
  24.  
  25.  
  26. //script.ts
  27. registerPlugin({
  28.   name: "some plugin",
  29.   config: [{
  30.     type: "string",
  31.     name: "foo",
  32.     value: "asdf"
  33.   }, {
  34.     type: "number",
  35.     name: "bar",
  36.     value: 123
  37.   }]
  38. }, (config) => {
  39.   //the issue here, config is declared as Record<string, any>
  40.   //however is it possible to extract type information from above config input
  41.   //and get the correct key and associated types directly assigned?
  42.   console.log(config) //gives { foo: "asdf", bar: 123 }
  43. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement