Hi Friends 👋,
Welcome to Infinitbility ❤️!
This tutorial is part of the below series.
TypeScript define variable
- How to define type in typescript?
- How to define array in typescript?
- How to define object in typescript?
- How to define array of objects in typescript?
Let start Part 3: How to define object in typescript?
In this tutorial, we will create objects in typescript, but before creating objects we have to write their property data type.
Let’s define their property data type.
To define the type, TypeScript provides a type
keyword you can also use interface
, and some developers define a class for this.
But here, we used type
.
1type User = {2 id: int,3 name: string;4 isActive: boolean;5};
in the above code, we have to define object property data type, which we are going to use in creating objects.
1type User = {2 id: int,3 name: string;4 isActive: boolean;5};67const user: User = {8 id: 1,9 name: "infintbility",10 isActive: true11}
Okay, now we know when we want to create an object then first we have to create their type.
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.