question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MethodDecorator gets TS2322/TS2315

See original GitHub issue

TypeScript Version: 2.4.2 Code

export function route(method: string, path: string): MethodDecorator {
    return (target: any, name: string, descriptor: TypedPropertyDescriptor<Function>) => {
        routeManager.regeisterRoute({
            constructor: target.constructor,
            function: descriptor.value,
            method,
            path
        });
    }
}
export function route(method: string, path: string): MethodDecorator<Function> {
    return (target: any, name: string, descriptor: TypedPropertyDescriptor<Function>) => {
        routeManager.regeisterRoute({
            constructor: target.constructor,
            function: descriptor.value,
            method,
            path
        });
    }
}

Expected behavior: No error Actual behavior:

error TS2322: Type '(target: any, name: string, descriptor: TypedPropertyDescriptor<Function>) => void' is not assignable to type 'MethodDecorate
or'.
  Types of parameters 'descriptor' and 'descriptor' are incompatible.
    Type 'TypedPropertyDescriptor<T>' is not assignable to type 'TypedPropertyDescriptor<Function>'.
      Type 'T' is not assignable to type 'Function'.
error TS2315: Type 'MethodDecorator' is not generic.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
ikatyangcommented, Aug 21, 2017

For the first one, it is caused by stricter generic checks (#16368).

It seems you have to define a CustomMethodDecorator first, since there is no generic on MethodDecorator, and its declaration requires a non-constraint generic.

Or just enable the --noStrictGenericChecks option.


For the second one, there is indeed no generic on MethodDecorator, the error message seems a little weird.

(lib.d.ts)

declare type MethodDecorator = <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
1reaction
parzhitskycommented, May 24, 2021

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 Method and Constructor, and a usage example.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript method decorator -- `this` with noImplicitThis enabled
apply(this, args) at the end -- but with noImplicitThis enabled in tsconfig.json , I get the following error: [eval].ts(1,224): error ...
Read more >
TypeScript - Method Decorator - LogicBig
A method decorator is applied to a method declaration. Method decorator without arguments. If a method decorator has no arguments e.g. ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found