Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
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:

interface IPromiseObject {
    promiseA: Promise<number>;
    promiseB: Promise<string>;
}

Into this:

interface IResolvedPromiseObject {
    promiseA: number;
    promiseB: string;
}

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.

export async function resolveAllObject<IPromiseObject>( promiseObject: IPromiseObject ): Promise<???> {
    const resolvedPromiseObject = {};
    for ( const key in promiseObject ) {
        const value = await promiseObject[ key ];        
        Object.assign( resolvedPromiseObject, { [ key ]: value } );
    }
    return resolvedPromiseObject;
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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

type AwaitedObject<T> = {
  [K in keyof T]: Awaited<T[K]>
}

Usage:

interface IPromiseObject {
  promiseA: Promise<number>;
  promiseB: Promise<string>;
}

type AwaitedObject<T> = {
  [K in keyof T]: Awaited<T[K]>
}

type IResolvedPromiseObject = AwaitedObject<IPromiseObject>
// type IResolvedPromiseObject = {
//    promiseA: number;
//    promiseB: string;
// }

Playground

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda