Difference Between valueof and keyof in TypeScript
Last Updated :
05 Jul, 2024
In TypeScript, valueOf() retrieves the primitive value of an object, useful for accessing inherent data types. Conversely, keyof extracts keys as a union type, crucial for type-safe property referencing and dynamic type handling in TypeScript's type system.
ValueOf() method
The valueOf() method is a built-in method in JavaScript and TypeScript that retrieves the primitive value of a specific object. In TypeScript, this method is typically used with objects that have a defined valueOf() method, such as number objects.
Syntax
number.valueOf();
Example 1: In this example, we have used the ValueOf() method.
JavaScript
// valueOf()
let num = new Number(30);
console.log(num.valueOf());
Output:
30
Example 2: In this example, we have used the valueOf() method.
JavaScript
// valueOf() method
let num4 = new Number(563);
console.log("Number Method: tovalueOf()");
console.log(typeof num4 )
console.log(num4 )
console.log(num4 .valueOf())
Output:
Number Method: tovalueOf()
number
[Number: 563]
563
Keyof Operator
The keyof operator in TypeScript is used to extract the keys of a type as a union type. It allows you to access the keys of an object type or an interface, which is useful for scenarios like generic programming and type-safe property accesses.
Syntax:
type KeysOfType = keyof ObjectType;
Example: This example defines an interface Person, a person variable, and a getProperty function. The function retrieves values from the person object using keys, demonstrating type-safe property access.
JavaScript
interface Person {
name: string;
age: number;
gender: string;
}
const person: Person = {
name: "John",
age: 25,
gender: "male",
};
function getProperty<T, K extends keyof T>(obj: T, key: K) {
return obj[key];
}
console.log(getProperty(person, "name")); // "John"
console.log(getProperty(person, "age")); // 25
console.log(getProperty(person, "gender")); // "male"
Output:
John
25
male
Difference between valueof and keyof in TypeScript
Feature | valueOf | keyof |
---|
Purpose | Obtains the primitive value of an object | Obtains a union type of property names |
---|
Usage | Object instance method | TypeScript operator in type definitions |
---|
Example | typescript class MyObject { valueOf() { return this.value; } } | typescript type MyType = { name: string; age: number; city: string; }; type MyKeys = keyof MyType; |
---|
Applicability | General JavaScript usage, not specific to TypeScript | TypeScript-specific, particularly useful in generic programming scenarios |
---|
Return Type | Typically returns a primitive value (e.g., number, string) | Returns a union type of all property names |
---|
These distinctions highlight how valueOf() is used on object instances to retrieve their primitive values, while keyof is employed in TypeScript to manipulate types and ensure type safety in property accesses.
Similar Reads
Difference between interfaces and classes in TypeScript In this article, we will see what is the Difference between "interface" and "Classes" in TypeScript. Interface: Interface is the virtual structure that is used for type-checking. In TypeScript we use interface keyword to create the new interface with identity. It create the structure for the same da
3 min read
What are the differences between any vs Object in TypeScript? TypeScript is an open-source programming language. It is a superset of JavaScript language. TypeScript is designed for the development of large applications. any: It is a built-in data type in TypeScript which helps in describing the type of variable which we are unsure of while writing the code. Th
2 min read
What is the difference between interface and type in TypeScript ? In TypeScript, both interface and type are used to define the structure of objects, but they differ in flexibility and usage. While interface is extendable and primarily for object shapes, type is more versatile, allowing unions, intersections, and more complex type definitions.Type in TypeScriptThe
3 min read
Difference Between instanceof and isPrototypeOf() in JavaScript In JavaScript, understanding object relationships and type checks is crucial for the effective programming. The Two methods that facilitate these checks are instanceof and isPrototypeOf(). While both are used to the determine the type or prototype relationships in the JavaScript they operate differe
2 min read
What is the difference between 'String' and 'string' in TypeScript ? Unlike JavaScript, TypeScript uses static typing, i.e. it specifies what kind of data the variable will be able to hold. Since TypeScript is a superscript of JavaScript, it also holds a distinction between a string and String. The usage of a string object in JS (or TS for that matter) is very minima
4 min read