![]() |
In TypeScript, a union type is a powerful way to express a variable that can be one of several types. Union types are used when a function or variable is expected to support multiple types of input. Union types are defined using the pipe (|) symbol between two or more types. This indicates that a variable or return value can be any one of these types. Following are the methods through which one can return union type in TypeScript. Table of Content Union type as a return type of functionIn the simplest form, you can specify a union type as the return type of a function. This approach is used when a function returns values of two or more types. Syntax:function functionName(parameters): TypeA | TypeB {
// function body
} Example: A function that returns either a string or a number. This example demonstrates a function getID that accepts and returns a union type of string | number. Depending on the type of argument passed, it returns either a string or a number.
Output: 101
202 Overloading FunctionsFunction overloading in TypeScript allows you to have multiple function signatures for the same function name, each with different return types. This is a more sophisticated way to handle union types as return values, especially when the return type is dependent on the function’s input types. Syntax:function functionName(parameters: TypeA): ReturnTypeA;
function functionName(parameters: TypeB): ReturnTypeB;
function functionName(parameters: any): any {
// function body
} Example: Overloading a function to return different types based on input. This example shows a function getValue that is overloaded to return a string when the input is a string and a number when the input is a number.
Output: Value is a string: username
Value is a number: 123 Returning Objects with Union TypesReturning objects that may contain union types within their properties is used when you want to return complex data structures that might have properties of different types. Syntax:function functionName(parameters): { keyA: TypeA; keyB: TypeB | TypeC } {
// function body
} Example: Returning an object with properties as union types. In this example, the getUserStatus function returns an object with a status property that is a union type ‘active’ | ‘inactive’.
Output: { name: 'User1', status: 'active' }
{ name: 'User2', status: 'inactive' } Using Type Aliases for Union TypesType aliases can be used to define a union type that can then be used as a return type for a function. This approach makes the code more readable and maintainable, especially for complex union types. Syntax:type MyUnionType = TypeA | TypeB;
function functionName(parameters): MyUnionType {
// function body
} Example: Using type aliases for a function’s return type. Here, a type alias ID is defined for the union type string | number and used as the return type of the getIdentifier function.
Output: 456
abc Using Generic Functions with Union TypesYou can define a generic function that accepts a type parameter and uses this parameter within a union type for the return value. The function can then decide which type to return based on the input or specific conditions within the function body. Example Here’s an example of a generic function that returns a value from a union type based on a condition:
Output: "Hello World"
123
{ error: "String is empty" }
{ error: "Number is negative" } |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |