Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer 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

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 1

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

Can you explain more use cases for union types in TypeScript?

How do union types differ from intersection types?

Can you show how to use union types with custom types or interfaces?

Awesome!

Completion rate improved to 5.56

bookUnion Types

Veeg om het menu te tonen

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

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 1
some-alt