MethodDecorator gets TS2322/TS2315
See original GitHub issueTypeScript 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:
- Created 6 years ago
- Comments:9 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 onMethodDecorator
, 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)
In case somebody needs an exact code, his is how I solved this issue in my project:
(See definitions of
Method
andConstructor
, and a usage example.)