I have a function below:
buildHandler({
accepts: {
userName: { type: 'string', required: true },
age: { type: 'number' }
},
handler: ({ args }) =>
{
// code
}
})
Or:
buildHandler({
accepts: [
{ arg: 'userName', type: 'string', required: true },
{ arg: 'age', type: 'number' },
],
handler: ({ args }) =>
{
// code
}
})
I want the type of the param “args” like
type Args = {
userName: string,
age?: number
}
I tried doing this:
type ReformatType<T> =
T extends 'string' ? string :
T extends 'number' ? number :
T extends 'boolean' ? boolean : T
type Schema<T extends Record<string, { type: any, required?: boolean }>> =
{ [key in keyof T]: T[keyof T]['required'] extends true ? ReformatType<T[keyof T]['type']> : (ReformatType<T[keyof T]['type']> | undefined) }
But the results aren’t what I expected.
type Args = {
userName: string | number | undefined;
age: string | number | undefined;
}
New contributor
Xuân Minh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.