Union 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.
12345678910function 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
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.
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
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
Union Types
Stryg for at vise menuen
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.
12345678910function 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
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.
Tak for dine kommentarer!