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_Methods_Promise_Decorator_Typing - Fatal编程技术网

使用TypeScript装饰程序和承诺进行正确的键入?

使用TypeScript装饰程序和承诺进行正确的键入?,typescript,methods,promise,decorator,typing,Typescript,Methods,Promise,Decorator,Typing,我编写了一个缓冲的装饰器,它通过返回一个承诺(在执行过程中)来按预期工作。然而,为了使tsc传输程序满意,我不得不使用any来转换修饰函数,然后(在下面的示例中)使用Promise 我怎样才能避免额外的演员?装饰者是否有可能修改装饰函数的签名?例如,我如何编写一个@buffered装饰器,它接受一个方法函数f(返回number),而装饰函数@buffered f返回一个Promise(而不仅仅是一个number): 但是p被识别为数字,而不是承诺: [ts] Property 'then' do

我编写了一个
缓冲的
装饰器,它通过返回一个
承诺
(在执行过程中)来按预期工作。然而,为了使
tsc
传输程序满意,我不得不使用
any
来转换修饰函数,然后(在下面的示例中)使用
Promise

我怎样才能避免额外的演员?装饰者是否有可能修改装饰函数的签名?例如,我如何编写一个
@buffered
装饰器,它接受一个方法函数
f
(返回
number
),而装饰函数
@buffered f
返回一个
Promise
(而不仅仅是一个
number
):

但是
p
被识别为
数字
,而不是
承诺

[ts] Property 'then' does not exist on type 'number'.
@buffered
装饰器的实现可在以下位置获得:


请注意,当我想要使用默认延迟
200ms
时,由于我希望去掉
@buffered
装饰器的括号,因此实现具有额外的复杂性。装饰器无法更改它们正在装饰的类的结构。这是一个设计限制。有一个建议,但它似乎不是一个优先事项(如果GitHub问题对您来说很重要,可能会投票表决)

如果我们想转换所有方法,我们可以使用映射类型,但是我们不能只对修饰的属性应用映射类型转换,所以在这种情况下这不是一个可行的解决方案

更好的选择是直接在类中使用corect返回类型。我们可以使用
async
对代码进行最小的更改:

class Class {
    @buffered(100) // i.e. 100ms delay
    // use async and return a promise instead of a number
    public async f1(t: Date): Promise<number> {
        return new Date().getTime() - t.getTime();
    }
    @buffered // i.e. defaults to 200ms delay
    public async f2(t: Date) { // no need to specify the return type will be inferred correctly to Promise<number>
        return new Date().getTime() - t.getTime();
    }
}

const p: Promise<number> = new Class().f1(new Date()); // works fine

p.then((res: number) => { console.debug(res); }); 
类{
@缓冲(100)//即100ms延迟
//使用async并返回承诺而不是数字
公共异步f1(t:日期):承诺{
返回新日期().getTime()-t.getTime();
}
@缓冲//即默认为200ms延迟
public async f2(t:Date){//无需指定返回类型,将正确推断出承诺
返回新日期().getTime()-t.getTime();
}
}
const p:Promise=new Class().f1(new Date());//很好
p、 然后((res:number)=>{console.debug(res);});

相关GitHub问题:@jcalz 10x链接:)
[ts] Type 'number' is not assignable to type 'Promise<number>'.
const p = new Class().f1(new Date());
p.then((res: number) => { console.debug(res); });
[ts] Property 'then' does not exist on type 'number'.
class Class {
    @buffered(100) // i.e. 100ms delay
    // use async and return a promise instead of a number
    public async f1(t: Date): Promise<number> {
        return new Date().getTime() - t.getTime();
    }
    @buffered // i.e. defaults to 200ms delay
    public async f2(t: Date) { // no need to specify the return type will be inferred correctly to Promise<number>
        return new Date().getTime() - t.getTime();
    }
}

const p: Promise<number> = new Class().f1(new Date()); // works fine

p.then((res: number) => { console.debug(res); });