区分打字稿类型中的修饰 class 方法

Distinguishing decorated class methods in a typescript type

我想创建一个泛型类型,它只从 class 定义中选择修饰方法。

function test(ctor: any, methodName: any) {}

class A {
    @test
    public x() {}

    public y() {}
}

type DecoratedOnly<T> = {
    [P in keyof T]: T[P] extends /* Magic Happens */ ? T[P] : never;
};

let a: DecoratedOnly<A> = {} as any;
a.x(); // ok
a.y(); // never!

是否可以推断 class 的修饰方法,因此 DecoratedOnly 泛型保持修饰的 x() 方法不变,并忽略未修饰的 y() 方法?

据我所知答案可能是"no"。装饰器目前不会改变类型,因此类型系统不会注意到装饰方法和未装饰方法之间的区别。人们已经为 class 装饰器(而不是像您正在使用的方法装饰器)要求这样的东西,here...但这是一个有争议的问题。有些人强烈认为装饰器应该不被类型系统观察到,而其他人则强烈反对。在 JavaScript 中的装饰器最终确定之前,TypeScript 的维护者不太可能对它们的工作方式进行任何更改,因此我不希望这里有任何立即的解决方案。


但是,如果我们备份并尝试提出一个与应用这些装饰器具有相同效果的解决方案,同时跟踪文件系统中发生的事情,会怎样?

为了得到一些具体的工作,我要让 test() 做一些事情:

function test(
  target: any,
  propertyKey: string,
  descriptor: PropertyDescriptor
) {
  console.log(
    "decorated test on target",
    target,
    "propertyKey",
    propertyKey,
    "descriptor",
    descriptor
  );
}

当你像这样制作 A 时:

class A {
  @test
  public x() {}

  public y() {}
}

您得到以下日志:decorated test on target Object { … } propertyKey x descriptor Object { value: x(), writable: true, enumerable: false, configurable: true }


由于我们无法检测何时应用装饰器,如果我们根本不使用 @test 装饰风格,而是调用 [=67 上的实际 test 函数会怎样=] 描述符,装饰器被编译成什么方法?如果我们创建自己的 apply-instance-method-decorator 函数,我们可以让该函数同时进行装饰 跟踪在类型系统中装饰了哪些方法。像这样:

function decorateInstanceMethods<T, K extends Extract<keyof T, string>>(
  ctor: new (...args: any) => T,
  decorator: (
    target: any,
    propertyKey: string,
    descriptor: PropertyDescriptor
  ) => void,
  ...methodsToDecorate: K[]
): T & { decoratedMethods: K[] } {
  methodsToDecorate.forEach(m =>
    decorator(
      ctor.prototype,
      m,
      Object.getOwnPropertyDescriptor(ctor.prototype, m)!
    )
  );
  return Object.assign(ctor.prototype, {
    decoratedMethods: methodsToDecorate
  });
}

该函数可能隐藏在某处的库中。下面是制作 A 并用 test:

装饰它的方法
class A {
  public x() {}
  public y() {}
}

const DecoratedAPrototype = decorateInstanceMethods(A, test, "x");

最终记录与之前相同的内容:decorated test on target Object { … } propertyKey x descriptor Object { value: x(), writable: true, enumerable: false, configurable: true }

但是现在DecoratedAPrototypeA.prototype加上decoratedMethods属性,类型是Array<"x">,所以你可以这样做:

type DecoratedOnly<
  T extends {
    decoratedMethods: (keyof T)[];
  }
> = Pick<T, T["decoratedMethods"][number]>;

const a: DecoratedOnly<typeof DecoratedAPrototype> = new A();
a.x(); // okay
a.y(); // error, property "y" does not exist on DecoratedOnly<typeof DecoratedAPrototype>

您可以看到 A 类型仍然不知道哪些方法被装饰了,但是 DecoratedAPrototype 知道。这足以给你你正在寻找的行为(我使用 Pick 所以省略的属性只是不知道存在并且没有明确地 never ......它不是非常重要我猜)

这对你有用吗?是的,它比仅仅使用装饰器要复杂一些,但它是我能得到的最接近你想要的东西。

无论如何,希望对您有所帮助。祝你好运!

Link to code