Exploring the creation of utility decorators like memoize
and rateLimiter
, I aim to maximize type safety while minimizing unnecessary boilerplate code.
Can decorators maintain full type safety without explicitly defining generics?
type GET_FUNCTION_SIGNATURE<
T extends TypedPropertyDescriptor<any>
> = T extends TypedPropertyDescriptor<infer U> ? U : never;
interface ITestDecoratorOptions<DECORATED_FUNCTION_ARGUMENTS_TYPE, DECORATED_FUNCTION_RETURN_TYPE> {
getKeyFromArgs: (args: DECORATED_FUNCTION_ARGUMENTS_TYPE) => string;
getDefaultValue: (args: DECORATED_FUNCTION_ARGUMENTS_TYPE) => DECORATED_FUNCTION_RETURN_TYPE;
}
const testDecorator = <TYPED_PROPERTY_DESCRIPTOR extends TypedPropertyDescriptor<any>>(
options: ITestDecoratorOptions<
Parameters<GET_FUNCTION_SIGNATURE<TYPED_PROPERTY_DESCRIPTOR>>,
ReturnType<GET_FUNCTION_SIGNATURE<TYPED_PROPERTY_DESCRIPTOR>>
>
) => {
return (
target: Object,
key: string,
descriptor = Object.getOwnPropertyDescriptor(target, key) as PropertyDescriptor
): TYPED_PROPERTY_DESCRIPTOR => {
return null as any;
};
};
class Test {
// \/ Is it possible to remove that generic and keep full type safety here?
@testDecorator<TypedPropertyDescriptor<(a: number, b: string) => boolean>>({
getKeyFromArgs: args => {
// number string
return args[0].toString() + args[1]; // full type checking
},
getDefaultValue: args => {
// full type checking: on args(number, string) and return type(boolean)
if (args[0] === 1) {
return true;
}
return false;
}
})
public someMethod(a: number, b: string): boolean {
return true;
}
}