10

I'm having trouble with the syntax for an async (arrow) function as a type/interface attribute. I've already done a bit of research but didn't found much except this https://github.com/Microsoft/TypeScript/issues/22035 It's not 100% accurate to my case but better than nothing. And still doesn't work for me...

Here is about the code I have in the declaration (simplified) :

export type MyType = {
    att1: string;
    funct: async (param1: string, param2: string) => Promise<string[]>;
};

And where I use it (example, not my actual code) :

import { MyType } from "./MyType";

const myObject: MyType = {
    att1: "First attribute";
    funct: async (param1: string, param2: string): Promise<string[]> => {
        // Code below is an example, in my case it's a database querying
        // But anyway, it's something async
        return new Promise((resolve, reject): void => {
            setTimeout((): void => {
                resolve(["string1", "string2"]);
            }, 5000);
        });
    };
};

The TypeScript transpiler says it cannot find the name "async". How do I tell the function "funct" in MyType is async? I need to use await in it...

1 Answer 1

27

The answer is simple, you don't tell the compiler the function is async on the interface. async is an implementation detail, it allows you to use await inside the body of the function. The user of the interface does not care how you implement your function only what it returns and what parameters need to be passed in.

What you really want is to tell the compiler that the function does not return string[] but rather a Promise<string[]>. This means that the caller can't access the result directly but needs to either use then on the returned promise or use await to get the result.

export type MyType = {
    att1: string;
    funct:  (param1: string, param2: string) => Promise<string[]>;
};
Sign up to request clarification or add additional context in comments.

5 Comments

The return type in the type definition is something I forgot to change... I just didn't know you don't have to add the async keyword in the interface. Any way, thank you very much.
What if I want to force the function to be async. I explicitly want the user of the interface to make the method an async one?
@MikeMajara no way to force this. From a consumer standpoint there really is no way to tell the difference between an async method and a method returning a promise.
how can I create a class that implements that interface and having the async method?
@BrunoNegrãoZica eg.: class MyClass implements MyType { funct (param1: string, param2: string) {.return Promise ((res, rej) => { // something async here leading to res(someArrayOfStrings) or rej(error) }. }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.