-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MethodDecorator gets TS2322/TS2315 #17936
Comments
For the first one, it is caused by stricter generic checks (#16368). It seems you have to define a Or just enable the For the second one, there is indeed no generic on (lib.d.ts) declare type MethodDecorator = <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void; |
|
So that it'll accept all kinds of NOTE: With
I'm not familiar with that |
There is no requirements that decorators return one of the predefined decorator types in I would strongly advise against enabling declare const routeManager: {
regeisterRoute: (config: {
constructor?: Function,
function?: Function,
method: string,
path: string
}) => void;
};
export function route(method: string, path: string) {
return <T extends object,
K extends keyof T,
R extends Promise<any>>
(target: T, name: keyof T, descriptor: TypedPropertyDescriptor<(...args: any[]) => R>) => {
routeManager.regeisterRoute({
constructor: target.constructor,
function: descriptor.value,
method,
path
});
};
}
class A {
@route('GET', 'api/values') m() {
return Promise.resolve(['a', 'b']);
}
} Not only will this pass the type checker since it is a valid decorator type, but the decorator will validate that the decorated method actually returns a Promise for a resource (just an example). Trying to exactly match the predefined decorator types would preclude this very useful typechecking behavior. |
Well, it works. Just like what @ikatyang says to define a |
@Norgerman Ah, I missed that comment. Sorry for being redundant. |
@Norgerman Could you please share your way of implementing |
In case somebody needs an exact code, his is how I solved this issue in my project: interface MethodDecoratorCustom<Instance extends object = object> {
(
target: Instance | Constructor<Instance>,
key: PropertyKey,
descriptor: TypedPropertyDescriptor<Method<Instance | Constructor<Instance>>>,
): void | TypedPropertyDescriptor<Method<Instance | Constructor<Instance>>>;
} (See definitions of |
TypeScript Version: 2.4.2
Code
Expected behavior:
No error
Actual behavior:
The text was updated successfully, but these errors were encountered: