Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
How to use a loop to create an object with keys from a list in typescript without using `Partial`?

Overview

Im trying to create an initialise an object with known keys, however the initial empty object {} that I need breaks my typing.

The problem:

I have a list of value, that I want to use as keys, and I want to initialise an object with those keys. In python i would do something like:

myDict = {key: value for (key, value) in zip(myKeys, [[]]*len(myKeys))}

This should give the myDict the type Record<myKeys, string[]>

In TS, i dont seem to be able to have the same luck, as i need to initialise the object to an empty list before i can populate it with my keys/values. I would rather not need to call it a partial, then coerce it into the correct type.

what ive tried:

I have tried:


    const allPlanFeatures: Partial<Record<ExtensivePlanHandle, string[]>> = {};
    for (const planId of extensivePlanList) {
        const features = getFeaturesForPlan(planHandleToMonthly[planId], is4kEnabled, exportFlow);
        allPlanFeatures[planId] = features;
    }
    return allPlanFeatures as Record<ExtensivePlanHandle, string[]>;

and

 const allPlanFeatures: Partial<Record<ExtensivePlanHandle, string[]>> =
        extensivePlanList.reduce(
            (a, planHandle) => ({
                ...a,
                [planHandle]: getFeaturesForPlan(
                    planHandleToMonthly[planHandle],
                    is4kEnabled,
                    exportFlow
                ),
            }),
            {}
        );
    return allPlanFeatures as Record<ExtensivePlanHandle, string[]>;

These do end up with the desired effect at the end of the day, but I'd rather be able to not have this partially working middle

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can try this:

const allPlanFeatures: Record<ExtensivePlanHandle, string[]> = {} as Record<ExtensivePlanHandle, string[]>;

this is not perfect, but sometimes you need to tell typescript compiler what is right.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda