mirror of
https://github.com/kamranahmedse/developer-roadmap.git
synced 2025-01-29 12:18:23 +01:00
Adding content to 104-combining-types
This commit is contained in:
parent
59d47c5b1e
commit
179bf366cc
@ -1 +1,16 @@
|
||||
# Union types
|
||||
# Union Types
|
||||
|
||||
Union Types in TypeScript allow you to specify multiple possible types for a single variable or parameter. A union type is written as a vertical bar (|) separated list of types.
|
||||
|
||||
For example, consider a function that takes either a string or a number as an argument:
|
||||
|
||||
```
|
||||
function combine(input1: string | number, input2: string | number) {
|
||||
return input1 + input2;
|
||||
}
|
||||
```
|
||||
|
||||
Learn more from the following links:
|
||||
|
||||
- [Unions Types](https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html)
|
||||
- [Union Types in TypeScript](https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html)
|
@ -1 +1,22 @@
|
||||
# Intersection types
|
||||
# Intersection Types
|
||||
|
||||
Intersection Types in TypeScript allow you to combine multiple types into a single type. An intersection type is written as an ampersand (&) separated list of types.
|
||||
|
||||
For example, consider an object that has both a name property and a email property:
|
||||
|
||||
```
|
||||
interface User {
|
||||
name: string;
|
||||
email: string;
|
||||
}
|
||||
|
||||
const user: User = {
|
||||
name: 'John Doe',
|
||||
email: 'johndoe@example.com'
|
||||
};
|
||||
```
|
||||
|
||||
Learn more from the following links:
|
||||
|
||||
- [Intersection Types](https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#intersection-types)
|
||||
- [Implement Intersection Types in the Typescript](https://www.youtube.com/watch?v=adr7W5uyIMk)
|
@ -1 +1,20 @@
|
||||
# Type aliases
|
||||
# Type Aliases
|
||||
|
||||
A Type Alias in TypeScript allows you to create a new name for a type.
|
||||
|
||||
Here's an example:
|
||||
|
||||
```
|
||||
type Name = string;
|
||||
type Age = number;
|
||||
type User = { name: Name; age: Age };
|
||||
|
||||
const user: User = { name: 'John', age: 30 };
|
||||
```
|
||||
|
||||
In the example above, `Name` and `Age` are type aliases for `string` and `number` respectively. And `User` is a type alias for an object with properties `name` of type `Name` and `age` of type `Age`.
|
||||
|
||||
Learn more from the following links:
|
||||
|
||||
- [Type Aliases](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-aliases)
|
||||
- [TypeScript Tutorial - Type Aliases](youtube.com/watch?v=AmpwfbdFYL8)
|
@ -1 +1,22 @@
|
||||
# Keyof operator
|
||||
# Keyof Operator
|
||||
|
||||
The keyof operator in TypeScript is used to get the union of keys from an object type.
|
||||
Here's an example of how it can be used:
|
||||
|
||||
```
|
||||
interface User {
|
||||
name: string;
|
||||
age: number;
|
||||
location: string;
|
||||
}
|
||||
|
||||
type UserKeys = keyof User; // "name" | "age" | "location"
|
||||
const key: UserKeys = "name";
|
||||
```
|
||||
|
||||
In this example, `UserKeys` is a type that represents the union of keys from the `User` interface, which is `"name"` | `"age"` | `"location"`. And a constant named `key` with the type `UserKeys` is declared with the value `"name"`.
|
||||
|
||||
Learn more from the following links:
|
||||
|
||||
- [Keyof Type Operator](https://www.typescriptlang.org/docs/handbook/2/keyof-types.html#handbook-content)
|
||||
- [Typescript Generics - Understanding the keyof Operator](https://www.youtube.com/watch?v=uy6fw4znJF4)
|
@ -1 +1,31 @@
|
||||
# Combining types
|
||||
# Combining Types
|
||||
|
||||
In TypeScript, you can combine types using type union and type intersection.
|
||||
|
||||
Type Union:
|
||||
The union operator `|` is used to combine two or more types into a single type that represents all the possible types. For example:
|
||||
|
||||
```
|
||||
type stringOrNumber = string | number;
|
||||
let value: stringOrNumber = "hello";
|
||||
value = 42;
|
||||
```
|
||||
|
||||
Type Intersection:
|
||||
The intersection operator `&` is used to intersect two or more types into a single type that represents the properties of all the types. For example:
|
||||
|
||||
```
|
||||
interface A {
|
||||
a: string;
|
||||
}
|
||||
interface B {
|
||||
b: number;
|
||||
}
|
||||
type AB = A & B;
|
||||
let value: AB = { a: "hello", b: 42 };
|
||||
```
|
||||
|
||||
Learn more from the following links:
|
||||
|
||||
- [Creating Types from Types](https://www.typescriptlang.org/docs/handbook/2/types-from-types.html#handbook-content)
|
||||
- [Typescript - Combining Types with Generic](https://www.youtube.com/watch?v=Z3g8dVFsuMM)
|
Loading…
x
Reference in New Issue
Block a user