How to Declare Specific Type of Keys in an Object in TypeScript ?
Last Updated :
20 Aug, 2024
In TypeScript, object definitions can include specific key-value types using index signatures. You can declare specific types of keys in an object by using different methods as listed below:
Using Mapped Types
You can define your mapped type using the type alias in TypeScript that can be used to specify the keys of a particular type in an object.
Syntax:
type customType = {};
Example: The below code explains the use of the mapped types to declare specific type keys in an object.
JavaScript
type MyObject = {
[key: string]: number;
};
const obj: MyObject = {
a: 1,
b: 2,
c: 3
};
console.log(obj);
Output:
{ a: 1, b: 2, c: 3 }
Using Interface
You can also use an interface to define your custom type where the keys of the object are typed to a specific type and can later be used to type the interface.
Interface interfaceName{}
Example: The below code example explains the use of the interface to declare specific type of keys in an object.
JavaScript
interface MyObject {
[key: string]: number;
}
const obj: MyObject = {
a: 1,
b: 2,
c: 3
};
console.log(obj);
Output:
{ a: 1, b: 2, c: 3 }
Using Inline Mapped Types with type
You can define a mapped type inline using the type keyword in TypeScript. This allows you to specify the keys of a particular type directly within the type declaration.
Syntax:
type TypeName = { [KeyType]: ValueType };
Example: This TypeScript code defines a type MyObject allowing string keys with number values. It creates an object obj with properties assigned numeric values and logs it.
JavaScript
type MyObject = { [key: string]: number };
const obj: MyObject = {
a: 1,
b: 2,
c: 3
};
console.log(obj);
Output:
{
"a": 1,
"b": 2,
"c": 3
}
Using Record Utility Type
TypeScript provides utility types that help in creating complex types from existing ones. These can be particularly useful for specifying specific key types in an object.
Example: The Record utility type is used to construct an object type with specified keys and a uniform value type.
JavaScript
// Define a type with specific keys of a certain type
type MyObject = Record<'name' | 'age' | 'email', string>;
// Create an object conforming to the specified type
const obj: MyObject = {
name: "Nikunj",
age: "22",
email: "[email protected]"
};
console.log(obj);
In this article, we explored different methods to define specific key-value types in TypeScript using index signatures. We covered using mapped types, interfaces, and inline mapped types with the type keyword. Each method allows for flexible and robust object type definitions, ensuring type safety and consistency within your TypeScript code.
Similar Reads
How to Check the Type of an Object in Typescript ? When working with TypeScript, understanding how to check the type of an object is crucial for ensuring type safety and maintaining code integrity. TypeScript, being a statically typed superset of JavaScript, provides several approaches to accomplish this task as listed below.Table of ContentUsing th
3 min read
How to Filter Keys of Type string[] in TypeScript ? In TypeScript, the filtering of keys of type string[] can be done by iterating over the data object, and applying the condition to the string if the key is a string. If the condition is satisfied, then an array of output results is created which consists of filtered keys of type string[]. The below
3 min read
How To Pick And Omit Keys in TypeScript? In TypeScript, when working with objects, there are scenarios where we may want to pick or omit certain keys from an existing type. TypeScript provides utility types Pick and Omit to accomplish this. These utilities allow us to create new types by including or excluding specific properties from an e
4 min read
How to Declare Object Value Type Without Declaring Key Type in TypeScript ? We will create an object value type without declaring the key type. We can not directly define the type of value we will use different methods for declaring the object value type. These are the following methods for declaring the object value type: Table of Content Using Record Utility TypeUsing Map
4 min read
How to Specify that a Class Property is an Integer in TypeScript ? Specifying that a class property is an integer in TypeScript involves using type annotations or specific TypeScript features to define the data type of the property. Below are the approaches used to specify that a class property is an integer in TypeScript: Table of Content By using Type AnnotationB
2 min read
How to Create a Typed Array from an Object with Keys in TypeScript? Creating a typed array from the keys of an object in TypeScript ensures that your application maintains type safety, particularly when interacting with object properties. Here we'll explore different approaches to achieve this using TypeScript's built-in Object methods.Below are the approaches used
3 min read
How to Create an Object in TypeScript? TypeScript object is a collection of key-value pairs, where keys are strings and values can be any data type. Objects in TypeScript can store various types, including primitives, arrays, and functions, providing a structured way to organize and manipulate data.Creating Objects in TypescriptNow, let
4 min read
How to Convert an Array of Objects into Object in TypeScript ? Converting an array of objects into a single object is a common task in JavaScript and TypeScript programming, especially when you want to restructure data for easier access. In this article, we will see how to convert an array of objects into objects in TypeScript.We are given an array of objects a
3 min read
How to Check if an Object is Empty in TypeScript ? In TypeScript, it's common to encounter scenarios where you need to determine if an object is empty or not. An empty object typically means it contains no properties or all its properties are either undefined or null. Below are the methods to check if an object is empty or not in TypeScript: Table o
3 min read
How to Cast a JSON Object Inside of TypeScript Class? Casting a JSON object to a TypeScript class involves converting a plain JSON object (which lacks methods and proper typing) into an instance of a class that includes all the defined methods and type safety of that class.Types of Objects in TypeScriptPlain Objects: When parsing JSON data using the JS
3 min read