diff --git a/src/roadmaps/typescript/content/104-combining-types/100-union-types.md b/src/roadmaps/typescript/content/104-combining-types/100-union-types.md index 68d004f8b..02670f36e 100644 --- a/src/roadmaps/typescript/content/104-combining-types/100-union-types.md +++ b/src/roadmaps/typescript/content/104-combining-types/100-union-types.md @@ -1 +1,16 @@ -# Union types \ No newline at end of file +# 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) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/104-combining-types/101-intersection-types.md b/src/roadmaps/typescript/content/104-combining-types/101-intersection-types.md index 748e918b6..c5c363ee8 100644 --- a/src/roadmaps/typescript/content/104-combining-types/101-intersection-types.md +++ b/src/roadmaps/typescript/content/104-combining-types/101-intersection-types.md @@ -1 +1,22 @@ -# Intersection types \ No newline at end of file +# 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) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/104-combining-types/102-type-aliases.md b/src/roadmaps/typescript/content/104-combining-types/102-type-aliases.md index fb31d4a0f..b6715c3ab 100644 --- a/src/roadmaps/typescript/content/104-combining-types/102-type-aliases.md +++ b/src/roadmaps/typescript/content/104-combining-types/102-type-aliases.md @@ -1 +1,20 @@ -# Type aliases \ No newline at end of file +# 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) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/104-combining-types/103-keyof-operator.md b/src/roadmaps/typescript/content/104-combining-types/103-keyof-operator.md index 1cb7e6892..bf337d685 100644 --- a/src/roadmaps/typescript/content/104-combining-types/103-keyof-operator.md +++ b/src/roadmaps/typescript/content/104-combining-types/103-keyof-operator.md @@ -1 +1,22 @@ -# Keyof operator \ No newline at end of file +# 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) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/104-combining-types/index.md b/src/roadmaps/typescript/content/104-combining-types/index.md index acfd59f75..b9d8423b4 100644 --- a/src/roadmaps/typescript/content/104-combining-types/index.md +++ b/src/roadmaps/typescript/content/104-combining-types/index.md @@ -1 +1,31 @@ -# Combining types \ No newline at end of file +# 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) \ No newline at end of file