laurent
laurent

Reputation: 90863

Getting a compiler error when not waiting for a promise?

Currently TypeScript allows me to do something like this:

async function getSomething():Promise<Something> {
    // ...
}

Then later:

const myObject = getSomething();

Then later when I try to use myObject it gives strange errors.

What I'm wondering is - is there any way to make TypeScript display an error when I get promise without await? I couldn't find any flag for this but maybe I missed it.

Upvotes: 0

Views: 81

Answers (1)

Richard Haddad
Richard Haddad

Reputation: 1014

Simply type your object:

const myObject: Something = getSomething();  // error, it returns Promise<Something>

const myObject: Something = await getSomething();  // works, it returns Something

Upvotes: 0

Related Questions