Typescript comes with a few nice type arithmetic operations like this one:
let target: ReturnType<typeof AcceptTerms>;
In this example AcceptTerms
is a function that returns Operation<AcceptTermsParams, string>
.
I'm wondering if it is possible to extract the generic type parameters and apply them to another type of a similar shape.
What I'm trying to achieve in practice is to generate a Permission
object for all my Operation
s:
type Permission<I, O> {
name: string;
operationName: string;
// ...
}
type Operation<I, O> = {
name: string;
// ...
};
My problem is that I don't have instances of Operation
s that I can use, only functions that return Operation
s (hence the ReturnType<typeof AcceptTerms>
).
Right now this is how I create permissions for an Operation
:
const allowViewAvailableBasketsForAll: Permission<void, Basket[]> = {
name: "Allow view available baskets for all",
operationName: VIEW_AVAILABLE_BASKETS_NAME,
policies: [allowAllPolicy()],
};
But it would be nice to be able to do something like this:
export const withAllowAllFor = <??extract type params of operation somehow??>(name: string): Permission<I, O> => ({
name: `Allow ${name} for all`,
operationName: name,
policies: [allowAllPolicy()],
});
So instead of supplying I
and O
by hand:
<I, O, T extends Operation<I, O>>
Can I somehow derive them from a type of Operation<I, O>
?