Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Union Types | Advanced TypeScript Features
TypeScript Foundations

bookUnion Types

Union types in TypeScript allow you to specify that a variable, parameter, or return value can be one of several types. You define a union type by separating each allowed type with a vertical bar (|). This gives you the flexibility to accept different types of values while still benefiting from TypeScript's static type checking. For example, if you want a variable to hold either a string or a number, you can declare its type as string | number. This is especially useful when working with APIs, user input, or functions that can handle multiple types of data.

12345678910
function printId(id: string | number) { if (typeof id === "string") { console.log("Your ID is: " + id.toUpperCase()); } else { console.log("Your ID is: " + id); } } printId("abc123"); // Output: Your ID is: ABC123 printId(456789); // Output: Your ID is: 456789
copy

Union types are commonly used in situations where a value might reasonably be more than one type. Typical use cases include functions that accept parameters of several types, handling data from external sources that may vary, or designing APIs that need to be flexible. For instance, you might use a union type for a function that processes both single items and arrays, or when dealing with values that could be null or a specific type. Union types help you write safer, more expressive code without sacrificing flexibility.

question mark

Which of the following is the correct way to declare a variable that can be either a number or a string in TypeScript?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 1

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Awesome!

Completion rate improved to 5.56

bookUnion Types

Svep för att visa menyn

Union types in TypeScript allow you to specify that a variable, parameter, or return value can be one of several types. You define a union type by separating each allowed type with a vertical bar (|). This gives you the flexibility to accept different types of values while still benefiting from TypeScript's static type checking. For example, if you want a variable to hold either a string or a number, you can declare its type as string | number. This is especially useful when working with APIs, user input, or functions that can handle multiple types of data.

12345678910
function printId(id: string | number) { if (typeof id === "string") { console.log("Your ID is: " + id.toUpperCase()); } else { console.log("Your ID is: " + id); } } printId("abc123"); // Output: Your ID is: ABC123 printId(456789); // Output: Your ID is: 456789
copy

Union types are commonly used in situations where a value might reasonably be more than one type. Typical use cases include functions that accept parameters of several types, handling data from external sources that may vary, or designing APIs that need to be flexible. For instance, you might use a union type for a function that processes both single items and arrays, or when dealing with values that could be null or a specific type. Union types help you write safer, more expressive code without sacrificing flexibility.

question mark

Which of the following is the correct way to declare a variable that can be either a number or a string in TypeScript?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 1
some-alt