gpt4 book ai didi

typescript - 区分 typescript 类型中的装饰类方法

转载 作者:行者123 更新时间:2023-12-04 13:17:58 28 4
gpt4 key购买 nike

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

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( ) 方法?

最佳答案

据我所知,答案可能是“否”。装饰器目前不会改变类型,因此类型系统不会注意到装饰方法和未装饰方法之间的区别。人们已经为类装饰器(而不是像你正在使用的方法装饰器)要求这样的东西,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 type 仍然不知道哪些方法被修饰了,但是 DecoratedAPrototype做。这足以为您提供您正在寻找的行为(我使用了 Pick 所以省略的属性只是不知道存在并且没有明确地 never ...我猜这不是很重要)

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

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

Link to code

关于typescript - 区分 typescript 类型中的装饰类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58039676/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com