Search code examples
typescriptasynchronousabstract-class

error TS1243: 'async' modifier cannot be used with 'abstract' modifier


In my project, I was using Typescript@4.0.3 and it was working fine, but now I updated its version to latest Typescript@4.1.3 and it is giving me a lot of errors. I am unable to find anything in documentations and not getting any Idea how to resolve this issue.

here is my code:

abstract class SystemValidator {

    constructor() {}

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

}

This is giving me error:

error TS1243: 'async' modifier cannot be used with 'abstract' modifier.

Any idea to resolve this issue?? Should I remove aync from here??


Solution

  • Yes you should remove async.

    You should not force to use async to the class that implements it. There are other ways to return a Promise, not just async.

    Edit:

    Since it is not clear for some people why the async is not important. Here a couple of ways to return a promise:

    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();
    
    

    Playground Link