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.

No alt text provided for this image

Here we have two different types of objects. so if we want to make an interface then we have to create 2 separate interfaces as the key is different. but one thing is common and that is both objects' keys is a string and the value is a number. so index signature comes help in this.

?{ [key: string]: number }, where we are telling typescript that object with all key type is string and value is number is valid. Here word key does not matter, we can use any word instead of the key. like { [index: string]: number } .

we also make a type for this and use it as below

No alt text provided for this image

In the above, MarkIndex2 is not valid. because [key: string]: number is telling typescript that all property has number type of value. so we can not give other types. however, if we want to use multiple types we make a union for this.

No alt text provided for this image

We can also define nested properties for objects using index signature.

No alt text provided for this image


Index signature caveats

  • we can only use the index, and a number of symbols as type.

[key: string] : number

[key: number] : number

[key: symbol] : number

[key: boolean] : number //Error        

  • We can use any string. so sometimes if we misspell or property does not exist in object, then it will give undefined like below.

No alt text provided for this image

Keyof Operator

Suppose you have a user object and you want to return a value based on the key. so we need to restrict the value of the key based on object property. in the below example, key is invalid still typescript will not throw an error.

No alt text provided for this image

So keyof operator is used in this type of scenario.

No alt text provided for this image

now only the name and age property are valid.

Using generics

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.

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

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

社区洞察

其他会员也浏览了