Typescript Decorator 函数泛型类型
Typescript Decorator function generic typing
我正在创建一个 Express rest API 并且也在考虑创建我自己的装饰器函数。我现在正在为控制器 class.
创建装饰器
但是每当我想将这个装饰器添加到控制器时,我都会收到以下错误:
Unable to resolve signature of class decorator when called as an expression. Type 'Constructable' is not assignable to type 'typeof ZoneController'. Property 'getZones' is missing in type 'RestController' but required in type 'ZoneController'.
这个错误不会在 stackblitz 中弹出(它应该在 ZoneController 的装饰器中弹出),但是对于代码,请参见 Stackblitz
这是有道理的,因为 RestController 是 ZoneConroller 的父级 class,所以它不知道任何方法。为了解决这个问题,我想向控制器装饰器添加一个通用类型:
type Constructable<T> = new (...args: any[]) => T
export function Controller<T extends RestController>(restCtor: Constructable<T>): Constructable<T> {
return class extends restCtor {
router = null
}
}
但这会导致以下错误:
Class '(Anonymous class)' incorrectly extends base class 'T'. '(Anonymous class)' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'RestController'
我不知道该如何解决这个错误。错误和代码见下文Stackblitz。
我找到了一些关于此错误的文档 (here and here),但我不清楚应该如何更改代码来解决此错误。
我应该如何更改我的代码才能解决此问题?
我正在使用打字稿 v4.0.2
和以下 TSConfig:
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"module": "esnext",
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2018",
"dom"
]
}
}
可以使用接口编写 class 的定义:
interface SomeClass {
new(...args: any[]): SomeClass
}
遵循相同的模式,我认为您需要更改 Constructable
的定义:
type Constructable<T> = new (...args: any[]) => Constructable<T>
我正在创建一个 Express rest API 并且也在考虑创建我自己的装饰器函数。我现在正在为控制器 class.
创建装饰器但是每当我想将这个装饰器添加到控制器时,我都会收到以下错误:
Unable to resolve signature of class decorator when called as an expression. Type 'Constructable' is not assignable to type 'typeof ZoneController'. Property 'getZones' is missing in type 'RestController' but required in type 'ZoneController'.
这个错误不会在 stackblitz 中弹出(它应该在 ZoneController 的装饰器中弹出),但是对于代码,请参见 Stackblitz
这是有道理的,因为 RestController 是 ZoneConroller 的父级 class,所以它不知道任何方法。为了解决这个问题,我想向控制器装饰器添加一个通用类型:
type Constructable<T> = new (...args: any[]) => T
export function Controller<T extends RestController>(restCtor: Constructable<T>): Constructable<T> {
return class extends restCtor {
router = null
}
}
但这会导致以下错误:
Class '(Anonymous class)' incorrectly extends base class 'T'. '(Anonymous class)' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'RestController'
我不知道该如何解决这个错误。错误和代码见下文Stackblitz。 我找到了一些关于此错误的文档 (here and here),但我不清楚应该如何更改代码来解决此错误。
我应该如何更改我的代码才能解决此问题?
我正在使用打字稿 v4.0.2
和以下 TSConfig:
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"module": "esnext",
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2018",
"dom"
]
}
}
可以使用接口编写 class 的定义:
interface SomeClass {
new(...args: any[]): SomeClass
}
遵循相同的模式,我认为您需要更改 Constructable
的定义:
type Constructable<T> = new (...args: any[]) => Constructable<T>