Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

300
Views
¿Cómo escribir esta lista en Typescript? actualmente está en Javascript, es una lista de autos,

¿Cómo escribir esta lista en Typescript? Sé que el código funciona en TypeScript, pero quiero que la lista tenga tipos declarativos. actualmente está en Javascript, es una lista de autos:

 const list_of_cars = [{ type: "Toyota", model: "Corolla", year: 2009 }, type: "Ford", model: "Mustang", year: 1969 }];

Sé que probé const car: { type: string, model: string, year: number } =

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Normalmente, puede definir un tipo o interfaz para objetos específicos y especificar los tipos individuales para cada atributo. Luego puede definir su lista como una matriz de la interfaz o tipo específico que definió.

Ejemplo:

 interface Car { type: string model: string year: number } const listOfCars: Car[] = [{ type: "Toyota", model: "Corolla", year: 2009 }, type: "Ford", model: "Mustang", year: 1969 }];
about 4 years ago · Juan Pablo Isaza Report

0

Esto también es válido en mecanografiado. Si quieres en tipos, puedes escribirlo así

 const list_of_cars:{type:string, model:string,year:number}[] = [{ type: "Toyota", model: "Corolla", year: 2009 },{ type: "Ford", model: "Mustang", year: 1969 }];

O puede declarar un tipo y usar eso

 type car={type:string, model:string,year:number} const list_of_cars : car[] = [{ type: "Toyota", model: "Corolla", year: 2009 },{ type: "Ford", model: "Mustang", year: 1969 }];
about 4 years ago · Juan Pablo Isaza Report

0

En uno de nuestros repositorios más estrictos en el trabajo, declaramos tipos explícitos en contenedores de matriz, que se verían así, si está interesado en verlo organizado profesionalmente:

 interface CarType { type: string model: string year: number } type CarsType = CarType[] const list_of_cars: CarsType = [ { type: 'Toyota', model: 'Corolla', year: 2009 }, { type: 'Ford', model: 'Mustang', year: 1969 }, ]

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!