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

304
Views
How to write this list in Typescript? it is currently in Javascript, It is list of cars,

How to write this list in Typescript? I know the code works in TypeScript, but I want the list to have declarative types. it is currently in Javascript, it is list of cars :

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

I know have tried const car: { type: string, model: string, year: number } =

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

0

Typically, you can define a type or interface for specific objects and specify the individual types for each attribute. You can then define your list as an array of the specific interface or type you defined.

Example:

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

This is also a valid in typescript. If you want in types then you can write it like this

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

OR you can declare an type and use that

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

In one of our strictest repos at work we declare explicit types on array containers, which would look like this, if you're interested in seeing it organised professionally:

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!