Skip to content
Advertisement

Typescript – Convert “object with promises as properties”-Type to “object with resolved promise values as properties”-Type

Is this possible in Typescript?

I want to turn e.g. something like this:

JavaScript

Into this:

JavaScript

I want to use it to resolve an object with promises as values, but still stay type safe. In this example, I need to convert IPromiseObject to an object with the same keys but the values resolved so that I can use it as return type.

JavaScript

Advertisement

Answer

You can map over the properties and use the Awaited utility type to extract the type inside the Promise.

JavaScript

Usage:

JavaScript

Playground

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement