Typescript装饰函数泛型类型

Typescript装饰函数泛型类型,typescript,generics,decorator,Typescript,Generics,Decorator,我正在创建一个Express rest API,同时也在考虑创建自己的装饰函数。我现在正在为控制器类创建一个装饰器 但每当我想将此装饰器添加到控制器时,就会出现以下错误: Unable to resolve signature of class decorator when called as an expression. Type 'Constructable' is not assignable to type 'typeof ZoneController'. Property 'getZo

我正在创建一个Express rest API,同时也在考虑创建自己的装饰函数。我现在正在为控制器类创建一个装饰器

但每当我想将此装饰器添加到控制器时,就会出现以下错误:

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'.
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中弹出(它应该在ZoneControl上的decorator处弹出),但有关代码,请参阅

这是有意义的,因为RestController是ZoneColler的父类,所以它不知道任何方法。为了解决这个问题,我想向控制器装饰器添加一个通用类型:

type Constructable<T> = new (...args: any[]) => T

export function Controller<T extends RestController>(restCtor: Constructable<T>): Constructable<T> {
  return class extends restCtor {
    router = null
  }
}
我不知道该如何解决这个错误。有关错误和代码,请参见以下内容。 我已经找到了一些关于此错误的文档(和),但我不清楚如何更改代码以解决此错误

我应该如何更改代码以解决此问题? 我正在使用typescript
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"
    ]
  }
}

可以使用接口编写类的定义:

interface SomeClass {
  new(...args: any[]): SomeClass
}
按照相同的模式,我认为您需要更改
Constructable
的定义:

type Constructable<T> = new (...args: any[]) => Constructable<T>
type Constructable=new(…args:any[])=>Constructable

这确实解决了问题,但也导致了一个新错误:
类型为“typeof ZoneControl”的参数不可分配给类型为“Constructable”的参数。类型为“ZoneControl”的参数不可分配给类型为“Constructable”。键入“ZoneControl”与签名“new(…args:any[]):Constructable”不匹配。
该错误将在
ZoneControl
类上方的
@Controller
装饰器上引发。同样,在stackblitz中不可见