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 are modify, or perform some actions in array/item of array.

No alt text provided for this image

Above we have different user. if we don't mension type and if there is any item in array then typescript will auto assume type based on typeof array. in above example all value is string so type of this array is string[]( square bracket [] after typeof item for single dimentsional array).

No alt text provided for this image

If we don't give value to array , then it will take type any[]. But we should not allowed any type. for more about any type visit this

We also give explicit type by type annotations in array like below. if you don't know type annotation then read my this previous blog

No alt text provided for this image

For two dimensional array , we have to give two bracket after item type like below,

No alt text provided for this image

Why we need to give type to array ?

we are doing lot's of manipulation to the array like adding a new elements, changing the value of an item of the array, apply some methods to the items. so we need to be aware of that. let's understand by examples

  • let's say we are fetching some items from an array and store in some variable. then if we give type to the array, then the type script automatically gives type to this new variable by inference. and if we are doing wrong assign value in future, or apply the wrong method then typescript give us the error

No alt text provided for this image

  • if you are adding an item with a different type, it will mark red line. because the Argument of type 'number' is not assignable to the parameter of type 'string'.

No alt text provided for this image

  • if we perform the wrong method in items like below then it 23 give

No alt text provided for this image

We can give multiple types to the same array, but we can see in the future. now let's understand tuples.

What is tuple?

Sometimes we want key-value only. as we can't give the key to array in js we need to make an object for this. but for only 2 things, it is also not suitable to make object. right!!!

a tuple is a solution for this. tuple allowed us to express an array with a fixed size of length, whose types are the same but not known but don't need to same. let's say you want to store user id and email only. you can make either object or make tuple for this as shown below. but tuple is suitable more for this type of example.

No alt text provided for this image

The order of id and email should always be the same for each tuple, so we can know that the first element will always be id and the second will always email. you can not reverse. it will give an error.

const userUsingTuple: [number, string] = [1000, "[email protected]"];

userUsingTuple[0] = '[email protected]'
// you can't change type of element.  it will give Type 'string' is not assignable to type 'number'


const userUsingTuple1: [number, string] = ["[email protected]", 1001]
// Type 'string' is not assignable to type 'number'.        

Let's see more examples with different lengths of tuples.

No alt text provided for this image

Generally, we use a tuple when we do not want to create the object because of a few numbers of key-value pairs.

The difference between an array in a tuple

  • array can be empty. the tuple will throw an error.
  • length of a tuple in fixed but not in the array
  • there is meaning of the order of an element. if we change order, then the meaning will change. but that is not in the case of an array.

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

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.

  • 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…

  • 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…

  • 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…

社区洞察

其他会员也浏览了