Open In App

How to Infer Return Type from a Tuple Parameter ?

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

In TypeScript, inferring the return type from a tuple parameter involves deriving the type of value returned by a function based on the types of its tuple parameters. This allows the compiler to deduce the function’s output type without explicit annotation, enhancing type safety and readability.

These are the following approaches:

Approach 1: Using Conditional Type

In this approach, we are using a conditional type to infer the return type from a tuple parameter. It deconstructs the tuple, extracting the last element as the return type, ensuring accurate type inference for functions with tuple parameters.

Example: We define myReturnType to infer the return type from a tuple parameter. It applies this to myFunction, then assigns the return value of booleanReturn() to a variable, result, with the inferred type.

Javascript
type myReturnType<T extends any[]> = T extends
    [infer First, ...infer Rest, infer Return] ? Return : never;

function myFunction(...args: [number, string, boolean]):
    string {
    return "GeeksforGeeks";
}

type Result = myReturnType<Parameters<typeof myFunction>>;

function booleanReturn(): boolean {
    return true;
}
const result: Result = booleanReturn();
console.log(result);

Output:

true

Approach 2: Using ReturnType with Function Type

In this approach, we are using ReturnType with function types. It derives the return type from a function’s signature. For instance, ReturnType<typeof func> infers the return type of function func.

Example: We define myFunction returning a tuple of string and number. booleanReturn returns a value matching myFunction’s return type.

Javascript
function myFunction(...args: [number, string, boolean]):
    [string, number] {
    return [&quot;GeeksforGeeks&quot;, 18];
}

type Result = ReturnType&lt;typeof myFunction&gt;;

function booleanReturn(): Result {
    return [&quot;true&quot;, 18];
}

const result: Result = booleanReturn();

console.log(result);

Output:

[ 'true', 18 ]

Approach 3: Using Inference with Tuple Parameter

This approach utilizes TypeScript’s type inference capabilities to deduce the return type from a tuple parameter directly within the function signature. By leveraging the types of the tuple parameters, TypeScript infers the appropriate return type, enhancing code readability and reducing the need for explicit type annotations.

Example: We define a function, extractReturnType, which takes a tuple parameter and returns the last element of the tuple. The return type is directly inferred by TypeScript based on the tuple parameter provided.

JavaScript
function extractReturnType<T extends any[]>(...args: T): T[number] {
    return args[args.length - 1];
}

// Usage
const result1: string | number | boolean = extractReturnType("Hello", 42, true); 
const result2: number = extractReturnType(1, 2, 3, 4); 

console.log(result1);
console.log(result2);

Output:

true 
4 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads