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

92
Vistas
reference to partial object in typescript?

I have an object and it's typed

interface formDataType {
  tesla: {
    made: string
  },
  ford: {
    made: string
  }
}
const formData: formDataType = {
    "tesla": {
      "made":"2021"
    },
    "ford": {
      "made": "2022"
    }
  };

then I've a function where its argument is one of [string]:{made: string}, how can I use back formDataType? is there something like objectOf formDataType?

demo https://codesandbox.io/s/react-typescript-forked-cwv9p

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

0

You can create a type to use in your FormDataType, then reuse that type in your function:

TS Playground

type VehicleDetails = { made: string };

type FormDataType = {
  ford: VehicleDetails;
  tesla: VehicleDetails;
};

// The following is the same type, written using the Record utility:
// type FormDataType = Record<'ford' | 'tesla', VehicleDetails>;

const formData: FormDataType = {
  tesla: {made: '2021'},
  ford: {made: '2022'},
};

function doSomethingWithVehicleDetails (details: VehicleDetails) {
  // console.log(details);
}


Update in response to your comment:

You can also derive a union of all the types of the values of the keys in your FormData type (and if they're all the same type, the union will effectively be that one type):

TS Playground

type FormDataType = {
  ford: { made: string };
  tesla: { made: string };
};

type VehicleDetails = FormDataType[keyof FormDataType];

function doSomethingWithVehicleDetails (details: VehicleDetails) {
  // console.log(details);
}

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