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

时间:2019-09-21 11:08:00

标签: typescript typescript-typings typescript-generics

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

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!

是否可以推断出某个类的修饰方法,因此DecoratedOnly泛型类型将修饰的x()方法保持原样,而省略未修饰的y()方法?

1 个答案:

答案 0 :(得分:2)

据我所知,答案可能是“否”。装饰器当前不更改类型,因此类型系统不会注意到修饰方法和未修饰方法之间的差异。人们要求类修饰符(而不是像您正在使用的方法修饰符)这样的东西,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装饰样式,而是在属性描述符上调用实际的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