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 .
You'll need a little more magic to do this. First a type that describes the type of the arguments object:
Then we'll map strings to their actual types (i.e. "string" to
string
):And finally we map over the arguments and get the type for each:
Where
IsOptional
is defined as:This is because if it is required, it gives us
never
, andType | never
is alwaysType
. However if it is not required (optional), it gives usundefined
, resulting in the desiredType | undefined
Here is a playground showcasing this.