1] Get a Return type of function

In TypeScript, "Get a return type of function" refers to the ability to extract and infer the return type of a given function. This feature is achieved using TypeScript's built-in utility type called ReturnType. The ReturnType utility type takes a function type as its argument and returns the inferred return type of that function.

Here's how you can use ReturnType:

// Sample function with a specific return type
function add(a: number, b: number){
  return a + b;
}

type AddReturnType = ReturnType<typeof add>;

// AddReturnType will be inferred as "number" because the return type 
// of the "add" function is "number"

In this example, we have a function add that takes two parameters of type number and returns a value of type number. By using ReturnType<typeof add>, we extract the return type of the add function, which results in the type being inferred as number.


2] Typeof keyword and Type level

In TypeScript, the typeof keyword is used for type querying and type inference at the type level. It allows developers to extract the type of a value or a variable, enabling the creation of more flexible code.

Example : Extract parameters of a function using Parameters<T> utility type available in TS.


3] Extract Awaited Result of a Promise in TS.

In TypeScript, you can extract the awaited result of a Promise using the Awaited utility type. The Awaited utility type is available in TypeScript 4.4 and later versions and is used to infer the resolved value of a Promise.

Here's how you can use the Awaited utility type:

// Sample async function that returns a Promise
async function fetchData(): Promise<number> {
  return 42;
}

type Result = Awaited<ReturnType<typeof fetchData>>;

// Result will be inferred as "number" because the resolved value of the Promise returned by "fetchData" is of type "number".