I'm trying to infer a functions paramers based on a predefined object. So:
function doTheThing((data) => {}, { data: { type: 'string', optional: true, default: 'hello' })
Using this type:
type InferParams<T extends Params | undefined> = {
[K in keyof T]: T[K]['optional'] extends true
? T[K]['default'] extends undefined
? InferParamType<T[K]> | undefined
: InferParamType<T[K]>
: InferParamType<T[K]>;
};
But I'm having trouble with the 'optional' and 'default' properties. If I remove that default clause it works great. If optional is true the param can be the type or undefined. But as soon as I add the default clause it is never optional, always just the type.
How do I get the type to correctly branch if there is no default?