Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 限制方法修饰符的使用_Typescript_Generics_Decorator - Fatal编程技术网

Typescript 限制方法修饰符的使用

Typescript 限制方法修饰符的使用,typescript,generics,decorator,Typescript,Generics,Decorator,我有一个方法的装饰器,我希望它只用于异步方法。这是一个用法示例: class A { @deco() // This should work. public async works() { } @deco() // This should fail. public fails() { } } 我试着这样定义装饰师: export function deco() { return <T extends {[K in keyof T]: () => Promise&

我有一个方法的装饰器,我希望它只用于异步方法。这是一个用法示例:

class A {
  @deco() // This should work.
  public async works() { }
  @deco() // This should fail.
  public fails() { }
}
我试着这样定义装饰师:

export function deco() {
  return <T extends {[K in keyof T]: () => Promise<any>}, 
          K extends string>
         (target: T, 
          propertyKey: K, 
          descriptor: PropertyDescriptor) => {
    // Decorator code here.
  };
}
这两者都不是:

export function deco() {
  return <T, 
          K extends keyof T>
         (target: T & {[propertyKey]: () => Promise<any>}, 
          propertyKey: K, 
          descriptor: PropertyDescriptor) => {
    // Decorator code here.
  };
}
导出函数deco(){ 返回 (目标:T&{[propertyKey]:()=>Promise}, 房地产商:K, 描述符:PropertyDescriptor)=>{ //这里是装饰代码。 }; }
有什么想法吗?

您应该使用方法装饰器,而不是属性装饰器:

declare type MethodDecorator = <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
declare-type-MethodDecorator=(目标:对象,属性key:string |符号,描述符:TypedPropertyDescriptor)=>TypedPropertyDescriptor | void;
可以添加到任何返回承诺的方法中的装饰器是:

function TypeRestrictedMethodDecorator(
    target: Object, // The prototype of the class
    propertyKey: string, // The name of the method
    descriptor: TypedPropertyDescriptor<(... p:any[]) => Promise<any>>
    ) {
    console.log("TypeRestrictedMethodDecorator called on: ", target, propertyKey, descriptor);
}

class TypeRestrictedMethodDecoratorExample {
    @TypeRestrictedMethodDecorator
    method(num: number): Promise<number> {
        return new Promise((res, rej)=> res(10));
    }

    @TypeRestrictedMethodDecorator // Error
    method2(num: number): number {
        return 10;
    }
}
函数类型受限方法编辑器(
target:Object,//类的原型
propertyKey:string,//方法的名称

描述符:TypedPropertyDescriptor承诺

到底是什么问题?使用PropertyDescriptor而不是TypedPropertyDescriptor?
PropertyDescriptor
不提供基于目标类型的限制方法,
MethodDecorator
是泛型方法,泛型类型是目标的类型。通常自您正在装饰一个方法,您应该使用a
MethodDecorator
,顾名思义
function TypeRestrictedMethodDecorator(
    target: Object, // The prototype of the class
    propertyKey: string, // The name of the method
    descriptor: TypedPropertyDescriptor<(... p:any[]) => Promise<any>>
    ) {
    console.log("TypeRestrictedMethodDecorator called on: ", target, propertyKey, descriptor);
}

class TypeRestrictedMethodDecoratorExample {
    @TypeRestrictedMethodDecorator
    method(num: number): Promise<number> {
        return new Promise((res, rej)=> res(10));
    }

    @TypeRestrictedMethodDecorator // Error
    method2(num: number): number {
        return 10;
    }
}