Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

TypeScript Object Types


TypeScript has a specific syntax for typing objects.

Read more about objects in our JavaScript Objects chapter.


Example

const car: { type: string, model: string, year: number } = {
  type: "Toyota",
  model: "Corolla",
  year: 2009
};
Try it Yourself »

Object types like this can also be written separately, and even be reused, look at interfaces for more details.


Type Inference

TypeScript can infer the types of properties based on their values.

Example

const car = {
  type: "Toyota",
};
car.type = "Ford"; // no error
car.type = 2; // Error: Type 'number' is not assignable to type 'string'.
Try it Yourself »

Optional Properties

Optional properties are properties that don't have to be defined in the object definition.

Example without an optional property

const car: { type: string, mileage: number } = { // Error: Property 'mileage' is missing in type '{ type: string; }' but required in type '{ type: string; mileage: number; }'.
  type: "Toyota",
};
car.mileage = 2000;

Example with an optional property

const car: { type: string, mileage?: number } = { // no error
  type: "Toyota"
};
car.mileage = 2000;
Try it Yourself »

w3schools CERTIFIED . 2022

Get Certified!

Complete the TypeScript modules, do the exercises, take the exam and become w3schools certified!!

$45 ENROLL

Index Signatures

Index signatures can be used for objects without a defined list of properties.

Example

const nameAgeMap: { [index: string]: number } = {};
nameAgeMap.Jack = 25; // no error
nameAgeMap.Mark = "Fifty"; // Error: Type 'string' is not assignable to type 'number'.
Try it Yourself »

Index signatures like this one can also be expressed with utility types like Record<string, number>.

Learn more about utility types like this in our TypeScript Utility Types chapter.


TypeScript Exercises

Test Yourself With Exercises

Exercise:

Add the correct types for the object below:

const car: { type: , model: , year:  } = {
  type: "Toyota",
  model: "Corolla",
  year: 2009
};

        

Start the Exercise


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.