Contents

What is Type?

Type

Type is a definition of a type of data. It is used to define the structure of the objects in typescript. It is collection of data types. It is denotade with 'type' keyword for creating new. It does not support the use of object.
                  
                  // Object type ******************************
                      type objectType = {
                        name: String;
                        city: String;
                        email: String;
                        isLoading: Boolean;
                        age: Number;
                      };

                      const Person: objectType = {
                        name: 'Saten Chauhan',
                        city: 'Kanpur',
                        email: 'saten@demo.com',
                        isLoading: true,
                        age: 35,
                      };

                      console.log(Person.name);

                    //Example ===================================
                    export type User = {
                      name: string;
                      age: number;
                    };

                    const userData: User = {
                      name: 'saten',
                      age: 50,
                    };

                    /* Interface */
                    export interface Person {
                      name: string;
                      age: number;
                      city: string;
                    }
                    const user: Person = {
                      name: 'saten',
                      age: 50,
                      city: 'kanpur',
                    };

                    function show(userData: User) {
                      return userData.name + userData.age;
                    }

                    console.log(show(user));