TypeScript Specifying Type Arguments
Last Updated :
28 Apr, 2025
TypeScript is a powerful statically typed superset of JavaScript that allows you to define and enforce types in your code. One of the key features of TypeScript is the ability to specify type arguments when working with generic functions and classes. This article will provide a comprehensive guide on how to specify type arguments in TypeScript, including syntax, approaches, and complete code examples.
These are the following approaches to specify typescript type arguments:
- Generic Class
- Generic Function
Generic Class
Class generics in TypeScript allow you to create classes that can work with various data types by defining type parameters within the class definition. These parameters enable you to create instances of the class with specific data types.
Syntax:
class GenericClass<T> {
// Implementation part
}
Where-
- GenericClass defines a TypeScript class called GenericClass that's generic.
- <T> notation serves as a placeholder for the actual data type to be used within the class.
Example: This TypeScript code defines a generic class called CustomContainer<T> that stores data of any specified type T. It takes an initial value of type T in its constructor, ensuring type safety. It also offers a retrieveData() method to access and return the stored data, preserving its original type. Two instances of CustomContainer are created with different data types (number and string).
JavaScript
class CustomContainer<T> {
constructor(private data: T) {}
retrieveData(): T {
return this.data;
}
}
const customNumberContainer =
new CustomContainer<number>(400);
const customStringContainer =
new CustomContainer<string>('GEEKSFORGEEKS');
console.log(customNumberContainer.retrieveData());
console.log(customStringContainer.retrieveData());
Output:

Generics Function
Function generics in TypeScript involve the use of type parameters within function definitions, allowing you to create flexible and reusable functions that work with various data types. These type parameters are placeholders for actual types and enable type inference, ensuring type safety while accommodating different input types. Function generics are valuable for creating versatile and type-conscious functions in TypeScript.
Syntax:
function functionName<T>(arg: T): T {
// Implementation part
}
Where-
- functionName<T> Defines a generic function named functionName with a type parameter T.
- arg: T specifies a function parameter named arg of type T.
- : T Denotes that the funciton returns a value of the same type T as the input paramater.
Example: This TypeScript code defines a generic function reverseTuple that swaps the types of elements in a two-element tuple. It's invoked with a tuple [31, 'GFG'], which is inferred as [number, string], and it returns ['GFG', 31]. The result is assigned to 'reversed' and logged to the console, demonstrating type swapping in tuples.
JavaScript
function reverseTuple<T, U>(tuple: [T, U]): [U, T] {
return [tuple[1], tuple[0]];
}
// Reversing the tuple
const reversed = reverseTuple([31, 'GFG']);
console.log(reversed);
Output:
Similar Reads
Typescript Generic Type Array A generic type array is an array that is defined using the generic type in TypeScript. A generic type can be defined between the angled brackets(<>). Syntax:// Syntax for generic type arraylet myArray: Array<Type>;Example 1: Creating a simple generic type array of number type in Typescri
1 min read
TypeScript Assertions Type TypeScript Assertions Type, also known as Type Assertion, is a feature that lets developers manually override the inferred or expected type of a value, providing more control over type checking in situations where TypeScript's automatic type inference may not be sufficient.Syntaxlet variableName: As
2 min read
Data types in TypeScript In TypeScript, a data type defines the kind of values a variable can hold, ensuring type safety and enhancing code clarity.Primitive Types: Basic types like number, string, boolean, null, undefined, and symbol.Object Types: Complex structures including arrays, classes, interfaces, and functions.Prim
3 min read
TypeScript any Type In TypeScript, any type is a dynamic type that can represent values of any data type. It allows for flexible typing but sacrifices type safety, as it lacks compile-time type checking, making it less recommended in strongly typed TypeScript code. It allows developers to specify types for variables, f
4 min read
TypeScript Generic Object Types TypeScript Generic Object Types allow you to create flexible and reusable type definitions for objects. These generic types can work with different shapes of objects while providing type safety, ensuring your code is both robust and adaptable. They are particularly useful for creating functions or c
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 Specify Return Type in TypeScript Arrow Functions ? To Specify the return type in the TypeScript arrow function, we have multiple approaches. In this article, we are going to learn how to Specify the type in the TypeScript arrow function. Below are the approaches used to Specify the return type in the TypeScript arrow function: Table of Content Expli
2 min read
TypeScript Interfaces Type TypeScript Interfaces Type offers an alternative method for defining an object's type, allowing for a distinct naming approach. Syntax:interface InterfaceName { property1: type1; property2?: type2; readonly property3: type3; // ... method1(): returnType1; method2(): returnType2; // ...}Parameters:in
2 min read
What are Hybrid Types in TypeScript? Hybrid types are used by TypeScript to refer to types that combine different things such as objects, functions, arrays etc. In this article, we will learn more about Hybrid Types in TypeScript.What are Hybrid Types in TypeScript?In TypeScript, using interfaces or type aliases we can define hybrid ty
2 min read
TypeScript Narrowing Assignments TypeScript narrowing assignments is a type of type inference mechanism that occurs when TypeScript examines the right side of an assignment and uses that information to narrow down the type of the variable or property on the left side of the assignment.Syntaxlet value: string | number = valueOftheVa
3 min read