-1

I'm making a project in TypeScript (Mainly to learn more about TypeScript) that uses the wikipedia npm package and this error came up and I can't figure out what's the problem. The issue is that I have an async function and it returns a json (A wikipedia summary). The problem is that when I assign it to a variable, it shows an error at res:

Type 'Promise<wikiSummary>' is missing the following properties from type 'wikiSummary': type, title, displaytitle, namespace, and 15 more.

Code:

async function getSummary(searchword: string) {
    try {
        const response: wikiSummary = await wiki.summary(searchword);
        return response;
    } catch (error) {
        console.log(error);
    }
}

const res: wikiSummary = getSummary(search); // error here

I have looked it up to see if someone had the same problem and it wasn't really what I looked for. I also can just remove the type annotation but I want to know why this is happening for my own knowledge.

Recleun
  • 41
  • 6
  • 1
    `const res: wikiSummary = await getSummary(search)` – super Dec 05 '22 at 13:40
  • @super It worked, but why? There's already an await inside the function, mind to clarify? – Recleun Dec 05 '22 at 13:45
  • 1
    `getSummary` is async. So it returns a promise. In order to get the value you need to await it. Just like `wiki.summary`. – super Dec 05 '22 at 13:56
  • @super After I compiled it, I ran it and it had an error: `await is only valid in async functions and the top level bodies of modules`. This is a node error not a TypeScript error. The await is in a top level body, but I think it's not a module, so how do I fix that? – Recleun Dec 05 '22 at 14:01
  • You transpile and run it with an appropriate tsconfig and version of node. Or you use `.then` instead. – super Dec 05 '22 at 14:08
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Jared Smith Dec 05 '22 at 17:09
  • @Recleun all asynchronous functions implicitly return a Promise. This is well-documented [on mdn](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function), here on SO, etc. – Jared Smith Dec 05 '22 at 17:10
  • @JaredSmith Yes, I understood that now. – Recleun Dec 05 '22 at 17:50

1 Answers1

0

I have found a couple of solutions for this. First one is to add await, by modifying tsconfig.json, and the second solution is by making an async function. I also could use .then().

First solution

I could use a await right before calling getSummary, and for it to work I had to modify the tsconfig.json file:

// tsconfig.json
{
    "compilerOptions": {
        "module": "esnext"
        "target": "ES2017"
    }
}

Second solution

I could make an async function and call getSummary inside of it with await. I don't think it's the best solution but it got the job done.

async function getSummary(searchword) {
    try {
        const response: wikiSummary = await wiki.summary(searchword);
        return response;
    }
    catch (error) {
        console.error(error);
    }
}

(async () => {
    const res = await getSummary(search);
    // rest of code here
})();
Recleun
  • 41
  • 6