top of page

KeyOf Operator in TypeScript

The keyof is one of the operators that can be used to fetch the user values; also, it follows the union operator formats and its properties; it retrieves the indexes for each value which is specified with the users so we can call it as index type query the keyof operator will be combined to the other predefined keywords like “extends keyof and in keyof” parallely we can use the keyof operator in objects like collections classes that is mapping technique we can store and retrieve the datas by using key and value pairs so by using the map instance object.keys() method will fetch the keys which are stored in the memory.


The TypeScript keyof operator is used for fetching and retrieving the indexes in the key ranges also it is used in the correct and exactly property names are needed and keyof operator used as the indexed type queries which will be yielded through the property ranges for the specific set of objects which is mapped and isolated to the keyof operator in TypeScript.

class classname
---- some typescript codes depends upon the requirement----
var or let variable name: keyof  classname;
varaiable  name=””;

The above codes are the basic syntax for ustilising the keyof operator in TypeScript. Here we have created the class with name and using the var or let data type with the variable name and call the classname with keyof operator whatever value is assigned to the variable name it will display the output screen.

How keyof Work in TypeScript?

  • Generally, TypeScript provides the set of utility types operators, keywords, and functions used to develop the applications in user prospectiveness. The keyof operator is also known as indexed type query operator, and it yields the union that contains property names and keys of its operand values and most probably the keyof operator will precede the object literal types, and it is most widely used in the primitive data types. The indexed base type query will fetch the values from the properties and their attributes which is related to the elements like default keywords and their data types.

  • The other keywords are used and derived with the character like “T” it covers and uses all the possible values, and it needs more features like arrays, collection techniques in the typescript. By using the object.Key () method we can store and retrieve the keys from the hash memory is hash memory the datas are stored in the hash reference by using some loops like for, forEach the values are iterated and display it on the output console.

  • Sometimes keyof method will concatenate the input values that will be in either numeric and non-numeric characters; the array values are stored it as the parent object, and the state is will be a generic one, and the typed function are calling both keys and values as the argument constructor so with the help of keyof operator and the method which retrieve the key index of all the values which is related to the user inputs. The key index is the unique one, so it will not accept the duplicate keys in the script.



class demo 
    p: number = 6253;
    r:string="hgsv jshdv  jv bhdcwc dbjw gc jshd  g hds jchdb jch jewcv 
    whcjbd jcwbd bw d j wb j wdv jwbhd  jb  wdbj j dwb kjwk b jkb w  bjwd  
    b wc kdjb kjd kjd wjbd cjhw wkj db jkwbd wkje b k jw bdghc jdh jwegh 
    wjged jd"
class demo1 
    s: keyof demo;
    example(t: demo): any 
        return t[this.s];

let d: demo1 = new demo1();
d.s = "p";
var res = d.example(new demo());


In the above example, we used the parent and child class relationships with keyof keyword. Here we used one class named like the demo. We have initialized the two variables with two data types like string and numbers; the number variable declares the values with number type and the string values using the string data type. We also used one method like example() in that we have return the values using this keyword. That method will be called from one variable with var global data type, and the values are stored and printed on the output console. With the variable like let and the demo1 class object is created with the let data type, and through its reference, the parent class variables are called through the child class(demo1) references.

The Tech Platform



bottom of page