Home > Net >  Mapping a dynamic object to typescript function params
Mapping a dynamic object to typescript function params

Time:04-28

I'm trying to make a list ( example below ) and map it to the parameters of a function. I imagine the arguments being an object with keys that match the names of the params, and the values being the options for each arg (whether it's required or more important, what type ).

interface Command<T> {        // Theese are the parts that I'm curious about, 
  args: T,                    // idk if its even possible, or if there's another solution.
  run(...args: T) {           // 
    
  }
}

const Arguments = {
  name: {
    type: "string",
    required: true,
  },
  duration: {
    type: "number",
    required: false,
  }
};

const cmd: Command<typeof Arguments> = {
  args: Arguments,
  run(name, duration) {
    console.log(`banned ${name} for ${duration ?? "forever"}`);
  }
}

If it is possible or you have any ideas of how to achieve this, please let me know as I've been struggling with this for days now

  • Related