How to get Function Parameters from Keys in an Array of Objects Using TypeScript ?
Last Updated :
15 Apr, 2025
In TypeScript, we can extract the function parameters from keys in an array of objects by going through object properties dynamically. We can use Generics and Type Assertion to get Function Parameters from Keys in an Array of Objects Using TypeScript.
Below are the possible approaches:
Using Generics
In this approach, we are using TypeScript Generics to create a function (approach1Fn) that accepts a type parameter T representing the keys of the objects in the data array. The function then takes a specific key as an argument, accesses the corresponding property of the first object in the array, and prints the key-value pair.
Syntax:
function functionName<T>(param: T): T {
// function body
return param;
}
const result: number = functionName<number>(42);
Example: The below example uses Generics to get Function Parameters from Keys in an Array of Objects Using TypeScript.
JavaScript
const data = [
{
title: "Introduction to TypeScript",
views: 1000,
author: "GFG User 1"
},
{
title: "Advanced JavaScript Concepts",
views: 1500, author: "GFG User 2"
},
{
title: "Data Structures in C++",
views: 1200,
author: "GFG User 3"
},
];
function approach1Fn<T extends keyof typeof data[0]>(key: T): void {
const data1 = data[0];
const paraValue = data1[key];
console.log(`${key}: ${paraValue}`);
}
approach1Fn("author");
Output:
"author: GFG User 1"
Using Type Assertion
In this approach, we are using Type Assertion with the keyword to inform TypeScript that the key being accessed (article[key]) is indeed a valid property of the object. The key of typeof article makes sure of type safety by limiting the key to those existing in the type of the individual objects within the data array.
Syntax:
const res = data[key as keyof typeof data];
Example: The below example uses Type Assertion to get Function Parameters from Keys in an Array of Objects Using TypeScript.
JavaScript
const data = [
{
title: "Introduction to TypeScript",
views: 1000, author: "GFG User 1"
},
{
title: "Advanced JavaScript Concepts",
views: 1500,
author: "GFG User 2"
},
{
title: "Data Structures in C++",
views: 1200,
author: "GFG User 3"
},
];
function approach2Fn(key: string): void {
data.forEach((article) => {
const paraValue = article[key as keyof typeof article];
console.log(`${key}: ${paraValue}`);
});
}
approach2Fn("title");
Output:
"title: Introduction to TypeScript"
"title: Advanced JavaScript Concepts"
"title: Data Structures in C++"
Using Object Destructuring
In this approach, we leverage object destructuring in TypeScript to directly extract function parameters from keys in an array of objects. This method offers concise syntax and readability.
Syntax:
function approach3Fn<T extends Record<string, any>>(data: T[], key: keyof T): void {
data.forEach((item) => {
const { [key]: paraValue } = item;
console.log(`${String(key)}: ${paraValue}`);
});
}
Example:
JavaScript
function approach3Fn<T extends Record<string, any>>(data: T[], key: keyof T): void {
data.forEach((item) => {
const { [key]: paraValue } = item;
console.log(`${String(key)}: ${paraValue}`);
});
}
const data = [
{
title: "Introduction to TypeScript",
views: 1000,
author: "GFG User 1"
},
{
title: "Advanced JavaScript Concepts",
views: 1500,
author: "GFG User 2"
},
{
title: "Data Structures in C++",
views: 1200,
author: "GFG User 3"
},
];
approach3Fn(data, "views");
Output:
views: 1000
views: 1500
views: 1200
Similar Reads
How to Convert an Array of Keys into a Union Type of Objects ? We can convert an array of keys into a union type of objects in typescript using mapped types or object types. The process involves dynamically creating types based on the provided array of keys, resulting in a union type that represents all possible combinations of objects with those keys.Table of
3 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 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 get Argument Types from Function in TypeScript ? In TypeScript, the Parameters utility type allows you to extract the types of a function's parameters. This is particularly useful when you want to work with the types of arguments dynamically, enabling functions that operate on any function's parameter types. In this article, we are going to see ho
2 min read
How to Get an Object Value By Key in TypeScript In TypeScript, we can get an object value by key by accessing the specific properties within the objects of the dynamic type. This can be done using Dot Notation, Bracket Notation, and Optional Chaining. In this article, we will explore all these approaches along with their implementation in terms o
5 min read
How can I Define an Array of Objects in TypeScript? In TypeScript, the way of defining the arrays of objects is different from JavaScript. Because we need to explicitly type the array at the time of declaration that it will be an Array of objects. In this article, we will discuss the different methods for declaring an array of objects in TypeScript.
6 min read