TypeScript Assignability of Functions
Last Updated :
22 Jul, 2024
In this article, we will explore the concept of assignability of functions in TypeScript. Specifically, we will discuss how functions with a return type of void can be assigned to function types with other return types, including those that return values.
Understanding Assignability
In TypeScript, functions with a void return type are assignable to function types with different return types. This means you can assign a function that does not return a value (void) to a variable or parameter expecting a function that returns a different type of value. However, TypeScript does not check if the returned value is being used when performing such assignments.
Contextual Syntax vs. Literal Syntax
1. Contextual Syntax:
This allows assignability and does not produce errors.
type FunctionType = () => void;
const functionName: FunctionType = () => {
// Function body
};
2. Literal Syntax:
This will give an error if the function does not match the expected return type.
function functionName(parameter1: Type1, parameter2: Type2, ...): void {
// Function body
}
Parameters
- functionType: custom name for function type.
- functionName: The name of the function of the type defined.
- : void: Indicates the return type of void, meaning the function doesn't return a meaningful value.
Examples of TypeScript Assignability of Functions
Example 1: In this example, we assign a void function to a function returning a string value. When the return value of the f1 function is assigned to another variable, it retains the type void.
JavaScript
type voidFunc = () => void;
const f1: voidFunc = () => {
return "GeeksforGeeks";
};
const gfg = f1();
console.log(gfg)
console.log(typeof gfg)
Output:

Example 2: In this example, we will see even though Array.prototype.push returns a number and the Array.prototype.forEach method expects a function with a return type of void, the code is valid.
JavaScript
const gfg =
["Java","C++", "Python","React","Typescript"];
const course = [];
gfg.forEach((el) => course.push(el));
console.log(course)
Output:

Similar Reads
TypeScript Anonymous Functions Type In TypeScript, an Anonymous Function Type defines a function without a specific name, specifying parameters and return types. This allows for flexible and reusable function definitions, enabling the assignment of functions to variables and the use of type annotations for parameters and return values
3 min read
TypeScript Functions Type TypeScript function types define the structure of a function, including its parameter types and return type, ensuring consistent and type-safe usage.Help validate the types of parameters passed to a function.Ensure the function returns the expected type.Improve code clarity and prevent runtime error
6 min read
TypeScript Function Type Expressions In this article, we are going to learn about TypeScript Function Type Expressions in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, a function type expression represents the type of a function, including its parameter types
3 min read
TypeScript Generic Functions TypeScript generic functions allow you to create functions that work with various types while maintaining type safety. By using type parameters, defined within angle brackets (<T>), generics enable functions to operate on different data types without losing the benefits of TypeScript's type-ch
3 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