Open In App

How to Create TypeScript Generic Function with Safe Type Matching ?

Last Updated : 19 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In TypeScript, generic functions offer a powerful tool for creating flexible and reusable code that can work with various data types. However, ensuring type safety is crucial to prevent runtime errors and maintain code reliability.

These are the following approaches:

Using Type constraints

You can specify constraints on the type parameter to ensure that only certain types are accepted.

Syntax:

function functionName<T>(parameter1: T, parameter2: T, ...): ReturnType {
// Function body
}

Example: The type constraint { length: number } ensures that only objects with a length property of type number can be passed to the printLength function. This prevents runtime errors by catching invalid types at compile time.

Javascript




function printLength<T extends { length: number }>
    (obj: T): void {
    console.log("Length:", obj.length);
}
 
printLength("hello");
printLength([1, 2, 3]);
printLength({ length: 5, width: 3 });
printLength(123);


Output:

Length: 5
Length: 3
Length: 5
Error: Type 'number' does not have a property 'length'

Using Type inference

TypeScript’s type inference mechanism can automatically infer the types based on the provided arguments, reducing the need for explicit type annotations.

Example: TypeScript infers the types of result1, result2, and result3 based on the types of the arguments passed to the identity function. This reduces the need for explicit type annotations and improves code readability.

Javascript




function identity<T>(arg: T): T {
    return arg;
}
 
const result1 = identity("hello");
const result2 = identity(123);
const result3 = identity(true);


Output:

result1 is of type string
result2 is of type number
result3 is of type boolean

Using Type guards

Type guards are conditional statements that check the type of a value at runtime, ensuring that only values of the expected type are used in the function.

Example: The isString type guard checks if the value is a string at runtime. If it is, the printLength function safely accesses the length property of the string. Otherwise, it handles the case where the value has an invalid type gracefully.

Javascript




function isString(value: any): value is string {
    return typeof value === "string";
}
 
function printLength(value: any): void {
    if (isString(value)) {
        console.log("Length:", value.length);
    } else {
        console.log("Invalid type");
    }
}
 
printLength("hello");
printLength(123);


Output:

Length:5
Invalid type

Conclusion

Enhancing type safety in TypeScript generic functions is paramount for building reliable and maintainable codebases. By leveraging type constraints, type inference, and type guards, developers can ensure that their generic functions safely handle a variety of data types, preventing runtime errors and improving code readability.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads