Answer the question
In order to leave comments, you need to log in
How to make a decorator allowed only for methods of a given type?
there is a class
export interface IMyClass {
requiredMethod(): void;
}
class MyClass implements IMyClass {
@AppendHelix()
method1(): string {
return 'my-string';
}
@AppendHelix()
method2(): number {
return Number.EPSILON;
}
requiredMethod(): void { };
}
export function AppendHelix(): MethodDecorator {
return function AppendHelixDecorator<T>(
target: any,
propertyKey: string | symbol,
descriptor: TypedPropertyDescriptor<T>
): TypedPropertyDescriptor<T> {
const originMethod = target[propertyKey];
return {
value() {
const origResult = originMethod.apply(this);
return 'helix ' + origResult;
},
} as any as TypedPropertyDescriptor<T>;
};
}
@AppendHelix()
method2(): number {
return Number.EPSILON;
}
Answer the question
In order to leave comments, you need to log in
T is just the signature of the method, put what you need there:
export function AppendHelix() {
return function AppendHelixDecorator(
target: any,
propertyKey: string | symbol,
descriptor: TypedPropertyDescriptor<() =>string>
) {
const originMethod = target[propertyKey];
return {
value() {
const origResult = originMethod.apply(this);
return 'helix ' + origResult;
},
}
};
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question