错误TS1243:“async”修饰符不能与“abstract”修饰符一起使用

在我的项目中,我正在使用Typescript@4.0.3并且运行良好,但现在我将其版本更新为最新Typescript@4.1.3它给了我很多错误。我无法在文档中找到任何内容,也没有任何想法如何解决此问题。

这是我的代码:

abstract class SystemValidator {

    constructor() {}

    abstract async validate(addr:Addr):Promise<[boolean, Addr[], SystemValidationErrors]>

}

这给了我错误:

错误 TS1243:“async”修饰符不能与“abstract”修饰符一起使用。

任何想法来解决这个问题?我应该aync从这里移除吗??

回答

是的,您应该删除async.

您不应该强制使用async实现它的类。还有其他方法可以返回 a Promise,而不仅仅是async.

编辑:

因为对于某些人来说不清楚为什么async不重要。这里有几种返回承诺的方法:

async function iAmAsync(): Promise<boolean>{
    return false;
}

function iAmNotAsync(): Promise<boolean>{
 return new Promise(resolve => resolve(false));
}

function iAmAlsoNotAsync(): Promise<boolean>{
 return new Observable().pipe(first()).toPromise();
}

iAmAsync().then();
iAmNotAsync().then();

游乐场链接

  • Of course it does. It converts any return to a Promise and informs the user of the function a promise is guaranteed to be returned. That's JavaScript 101. The async modifier only has to do with the return and has nothing to do with anything else. If a developer wants to enforce it just for code readability and not getting forced to return new Promise() which is actually transparent and automatic with async he should be allowed to.
  • He *should* force the class that implements it to return whatever he needs to be returned. The fact is TypeScript is limited on that one. What should happen is TypeScript must allow the async modifier with abstract if that's what the developer intends.

以上是错误TS1243:“async”修饰符不能与“abstract”修饰符一起使用的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>