Add more strict types and better inference#11
Conversation
|
Hi @Kalin8900, thank you for your contribution! I am currently a bit swamped with work, but I'll definitely have look at it the next days. |
|
Hi @Kalin8900, So I am wondering if it is sufficient for your use case to have the "type-safety" on the // make the Aspects context generic with the default AspectContext
export interface Aspect<T extends AspectContext = AspectContext> {
execute(ctx: T): any;
}
export interface AspectContext {
target: any;
methodName: string;
functionParams: any[];
returnValue: any;
error: any;
};
// ...
// specify a custom context with a number as return type
interface SpecificAspectContext extends AspectContext {
target: any;
methodName: string;
functionParams: any[];
returnValue: number;
error: any;
}
class SpecificAspect implements Aspect {
execute(ctx: SpecificAspectContext) {
executeHasBeenCalled = true;
ctx.returnValue = 2; // works
ctx.returnValue = "2"; // error
}
}
class YetAnotherSampleClass {
@UseAspect(Advice.After, SpecificAspect)
public doIt(): number {
return 1;
}
}This way you can pretend to work with specific types in the Aspects |
|
Hello @engelmi! The main thing I would like to achieve is having the type check on the decorator level. I don't see a point of adding it only on the Aspect level, because you can have unexpected behaviors then. Maybe there is a way to combine the decorator level check if specifying custom context? |
Hello everyone.
I would like to propose an extension to the current solution to introduce better type inference and type safety.
Please let me know what you think. If you find it useful, I will update the documentation.