Suppose I define a generic mapped type that associates keys with functions.
type SomeMappedType<T, R> = {
[K in keyof T]: (...args: any[]) => R
}
This is then passed to my consuming function through an options type where it's optional.
type Options<T = any, R = any, P extends SomeMappedType<T, R> = SomeMappedType<T, R>> = {
prop1?: P
}
I use this value and return the output type. In the code, if prop1
is undefined, it doesn't affect the output. However, the Output
type returns a non-empty mapped type even in this case.
const test2: {
[x: string]: any;
}
The output type looks like this:
type Output<T, R, P extends SomeMappedType<T, R>> = ResolveType<{ [K in keyof P]: ReturnType<P[K]> }>;
Is there a way for the default SomeMappedType<T, R>
to convey that it essentially contains no keys? Essentially looking for a mapped type equivalent of an empty array []
You can check out this TypeScript playground link to see the issue
Thank you for your assistance!
EDIT:
The solution provided by @spender works well for the original example, which may have been too straightforward. Here's an updated example that demonstrates 2 different optional properties with distinct types contributing to the Output