High-level overview of type and interface in Typescript

High-level overview of type and interface in Typescript

Standard javascript object is a map of key: value pairs. When we define an object with properties (keys) and values, TypeScript creates an?implicit interface?by looking at the property names and data types of their values in the object. This happens because of the?type of inference. or we can explicitly gives type to object.

If we create the same type of object then we need to too much copy-paste for the same object also if want to change in object structure like age will be a string instead of number, then we have to go everywhere to change the type.

No alt text provided for this image

We can solve this type of issue using type or interface.

Type keyword:

The type keyword is a way to describe a type alias for variables, objects, and functions. This alias essentially describes what our data look likes. we can describe data by their type string, number, boolean, etc.

No alt text provided for this image

We can create types for string,number,boolean , tuples also.

No alt text provided for this image

We can combine more than one type using &. if any property we miss from combined types, then typescript will give an error.

No alt text provided for this image

Interface

An interface is the shape of javascript. the interface is just like an object but it only contains information about the key and their types.

Let's convert types examples in the interface and then understand the difference.

No alt text provided for this image

We need to extend keywords to inherit property from another interface.

No alt text provided for this image

We need to extend keywords in class to the user interface

No alt text provided for this image


Difference between interface and type:

  • we cannot use the interface for primitive values. so the below things is not possible in the interface.

type Age =  number;

type Price = number | string ;

const age : Age = 24;
const price: Price = 9999        

  • if more than one interface is defined with the same name then it will be merged. all property is required to use that interface. while in type we only use one name for each type.

No alt text provided for this image

  • we can use & for inheriting other types and use extends in the interface for inheriting another interface.

No alt text provided for this image

要查看或添加评论,请登录

Meet Vaghasiya的更多文章

  • START EXPRESS JS WITH TYPESCRIPT

    START EXPRESS JS WITH TYPESCRIPT

    In this article, we will learn How to install express js with typescript and minimal setup configuration of…

  • Redux with typescript

    Redux with typescript

    Let's see how we use redux with typescript install react typescript app npx create-react-app typescript-practise…

  • Using typescript with react.

    Using typescript with react.

    Here we will learn step by step process for starting the #typescript with a #reactjs . Steps: create a typescript app.

  • Index signature and keyof operator in Typescript

    Index signature and keyof operator in Typescript

    Index signature Let's say we have two types of objects. and you want to make a common function that returns total marks.

  • Use generics like pro

    Use generics like pro

    There is a rule in programming called DRY- don't repeat yourself. generics help us to use this convention in typescript.

  • Classes in typescript - 6thday of 100daysofcoding

    Classes in typescript - 6thday of 100daysofcoding

    Typescript is fully supported to work with modern javascript keyword class. Generally, classes are blueprints to make…

  • Functions and object in typescript- DAY5 OF 100DAYSOFCODE

    Functions and object in typescript- DAY5 OF 100DAYSOFCODE

    Functions: Functions are a very important part of any programming language as it provides reusability. let's see, how…

  • ARRAY AND TUPLES IN TYPESCRIPT - DAY4 OF #100DAYSOFCODE

    ARRAY AND TUPLES IN TYPESCRIPT - DAY4 OF #100DAYSOFCODE

    ARRAY IN TYPESCRIPT In typescript, typescript need to know type of each items in array. so it will tell us error if we…

  • TYPE- any in ts - DAY3 OF 100DAYSOFCODE

    TYPE- any in ts - DAY3 OF 100DAYSOFCODE

    Any type in typescript in the previous article, if you remember the last moment, if we don't declare and initialize…

    2 条评论
  • Type and Type system in Typescript - DAY2 of 100 DAYSOFCODE

    Type and Type system in Typescript - DAY2 of 100 DAYSOFCODE

    What is a type system? When we learn any programming languages, first they introduces with various data-types of that…

社区洞察

其他会员也浏览了