我想要创建一个泛型类型,它只从类定义中选择修饰的方法。
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()方法?
发布于 2019-09-21 20:43:10
据我所知,答案可能是“不”。装饰师目前不改变类型,所以类型系统不会注意到装饰方法和未修饰方法之间的区别。对于类装饰师(与您使用的方法装饰师不同),这里.但这是个有争议的问题。有些人非常强烈地认为,装饰者不应该被类型系统所观察到,而另一些人则同样强烈地感到不同。在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函数,那又是什么方法呢?如果我们建立自己的应用实例-方法-装饰函数,我们可以使该函数同时进行装饰,并跟踪哪些方法在类型系统中被装饰。就像这样:
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 }。
但是现在,DecoratedAPrototype是A.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.我想这并不是非常重要)
这对你有用吗?是的,这比仅仅使用装饰要复杂一些,但这是我能得到的最接近你想要的东西。
不管怎样,希望这能帮上忙。祝好运!
https://stackoverflow.com/questions/58039676
复制相似问题