From 1df4e4b836bda702c0aacffefcb651a1c15ee20d Mon Sep 17 00:00:00 2001 From: syedmouaazfarrukh Date: Wed, 1 Feb 2023 08:23:42 -0800 Subject: [PATCH] Adding content to 111-utility-types --- .../content/111-utility-types/100-partial.md | 31 ++++++++++++- .../content/111-utility-types/101-pick.md | 27 ++++++++++- .../content/111-utility-types/102-omit.md | 41 ++++++++++++++++- .../content/111-utility-types/103-readonly.md | 21 ++++++++- .../content/111-utility-types/104-record.md | 27 ++++++++++- .../content/111-utility-types/105-exclude.md | 20 ++++++++- .../content/111-utility-types/106-extract.md | 17 ++++++- .../111-utility-types/107-non-nullable.md | 17 ++++++- .../111-utility-types/108-parameters.md | 39 +++++++++++++++- .../111-utility-types/109-return-type.md | 45 ++++++++++++++++++- .../111-utility-types/110-instance-type.md | 35 ++++++++++++++- .../content/111-utility-types/111-awaited.md | 22 ++++++++- .../content/111-utility-types/index.md | 14 +++++- 13 files changed, 343 insertions(+), 13 deletions(-) diff --git a/src/roadmaps/typescript/content/111-utility-types/100-partial.md b/src/roadmaps/typescript/content/111-utility-types/100-partial.md index febf47d1e..76fb1e40f 100644 --- a/src/roadmaps/typescript/content/111-utility-types/100-partial.md +++ b/src/roadmaps/typescript/content/111-utility-types/100-partial.md @@ -1 +1,30 @@ -# Partial \ No newline at end of file +# Partial + +The Partial type in TypeScript allows you to make all properties of a type optional. This is useful when you need to create an object with only a subset of the properties of an existing type. + +Here's an example of using the Partial type in TypeScript: + + ``` + interface User { + name: string; + age: number; + email: string; + } + + function createUser(user: Partial): User { + return { + name: 'John Doe', + age: 30, + email: 'john.doe@example.com', + ...user + }; + } + + const newUser = createUser({ name: 'Jane Doe' }); + console.log(newUser); + // Output: { name: 'Jane Doe', age: 30, email: 'john.doe@example.com' } + ``` + +Learn more from the following links: + +- [Partial](https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/111-utility-types/101-pick.md b/src/roadmaps/typescript/content/111-utility-types/101-pick.md index 7266b614a..87fa0b03c 100644 --- a/src/roadmaps/typescript/content/111-utility-types/101-pick.md +++ b/src/roadmaps/typescript/content/111-utility-types/101-pick.md @@ -1 +1,26 @@ -# Pick \ No newline at end of file +# Pick + +Pick constructs a type by picking the set of properties Keys (string literal or union of string literals) from Type. + + ``` + interface Todo { + title: string; + description: string; + completed: boolean; + } + + type TodoPreview = Pick; + + const todo: TodoPreview = { + title: "Clean room", + completed: false, + }; + + todo; + + const todo: TodoPreview + ``` + +Learn more from the following links: + +- [Pick](https://www.typescriptlang.org/docs/handbook/utility-types.html#picktype-keys) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/111-utility-types/102-omit.md b/src/roadmaps/typescript/content/111-utility-types/102-omit.md index cb66ccb35..5fa06a904 100644 --- a/src/roadmaps/typescript/content/111-utility-types/102-omit.md +++ b/src/roadmaps/typescript/content/111-utility-types/102-omit.md @@ -1 +1,40 @@ -# Omit \ No newline at end of file +# Omit + +Omit constructs a type by picking all properties from Type and then removing Keys (string literal or union of string literals). + + + ``` + interface Todo { + title: string; + description: string; + completed: boolean; + createdAt: number; + } + + type TodoPreview = Omit; + + const todo: TodoPreview = { + title: "Clean room", + completed: false, + createdAt: 1615544252770, + }; + + todo; + + const todo: TodoPreview + + type TodoInfo = Omit; + + const todoInfo: TodoInfo = { + title: "Pick up kids", + description: "Kindergarten closes at 5pm", + }; + + todoInfo; + + const todoInfo: TodoInfo + ``` + +Learn more from the following links: + +- [Omit](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/111-utility-types/103-readonly.md b/src/roadmaps/typescript/content/111-utility-types/103-readonly.md index 1f4fb3b0f..5057ab999 100644 --- a/src/roadmaps/typescript/content/111-utility-types/103-readonly.md +++ b/src/roadmaps/typescript/content/111-utility-types/103-readonly.md @@ -1 +1,20 @@ -# Readonly \ No newline at end of file +# Readonly + +Readonly constructs a type with all properties of Type set to readonly, meaning the properties of the constructed type cannot be reassigned. + + ``` + interface Todo { + title: string; + } + + const todo: Readonly = { + title: "Delete inactive users", + }; + + todo.title = "Hello"; + Cannot assign to 'title' because it is a read-only property. + ``` + +Learn more from the following links: + +- [Readonly](https://www.typescriptlang.org/docs/handbook/utility-types.html#readonlytype) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/111-utility-types/104-record.md b/src/roadmaps/typescript/content/111-utility-types/104-record.md index b3d22144f..38f09a6a6 100644 --- a/src/roadmaps/typescript/content/111-utility-types/104-record.md +++ b/src/roadmaps/typescript/content/111-utility-types/104-record.md @@ -1 +1,26 @@ -# Record \ No newline at end of file +# Record + +Record constructs an object type whose property keys are Keys and whose property values are Type. This utility can be used to map the properties of a type to another type. + + ``` + interface CatInfo { + age: number; + breed: string; + } + + type CatName = "miffy" | "boris" | "mordred"; + + const cats: Record = { + miffy: { age: 10, breed: "Persian" }, + boris: { age: 5, breed: "Maine Coon" }, + mordred: { age: 16, breed: "British Shorthair" }, + }; + + cats.boris; + + const cats: Record + ``` + +Learn more from the following links: + +- [Record](https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/111-utility-types/105-exclude.md b/src/roadmaps/typescript/content/111-utility-types/105-exclude.md index dda181b99..ef484de87 100644 --- a/src/roadmaps/typescript/content/111-utility-types/105-exclude.md +++ b/src/roadmaps/typescript/content/111-utility-types/105-exclude.md @@ -1 +1,19 @@ -# Exclude \ No newline at end of file +# Exclude + +Exclude constructs a type by excluding from UnionType all union members that are assignable to ExcludedMembers. + + ``` + type T0 = Exclude<"a" | "b" | "c", "a">; + + type T0 = "b" | "c" + type T1 = Exclude<"a" | "b" | "c", "a" | "b">; + + type T1 = "c" + type T2 = Exclude void), Function>; + + type T2 = string | number + ``` + +Learn more from the following links: + +- [Exclude](https://www.typescriptlang.org/docs/handbook/utility-types.html#excludeuniontype-excludedmembers) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/111-utility-types/106-extract.md b/src/roadmaps/typescript/content/111-utility-types/106-extract.md index d0e6da974..91b5b6381 100644 --- a/src/roadmaps/typescript/content/111-utility-types/106-extract.md +++ b/src/roadmaps/typescript/content/111-utility-types/106-extract.md @@ -1 +1,16 @@ -# Extract \ No newline at end of file +# Extract + +Extract constructs a type by extracting from Type all union members that are assignable to Union. + + ``` + type T0 = Extract<"a" | "b" | "c", "a" | "f">; + + type T0 = "a" + type T1 = Extract void), Function>; + + type T1 = () => void + ``` + +Learn more from the following links: + +- [Extract](https://www.typescriptlang.org/docs/handbook/utility-types.html#extracttype-union) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/111-utility-types/107-non-nullable.md b/src/roadmaps/typescript/content/111-utility-types/107-non-nullable.md index 83fd5d36f..be6d3d79d 100644 --- a/src/roadmaps/typescript/content/111-utility-types/107-non-nullable.md +++ b/src/roadmaps/typescript/content/111-utility-types/107-non-nullable.md @@ -1 +1,16 @@ -# Non nullable \ No newline at end of file +# Non Nullable + +Nun Nullable constructs a type by excluding null and undefined from Type. + + ``` + type T0 = NonNullable; + + type T0 = string | number + type T1 = NonNullable; + + type T1 = string[] + ``` + +Learn more from the following links: + +- [NonNullable](https://www.typescriptlang.org/docs/handbook/utility-types.html#nonnullabletype) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/111-utility-types/108-parameters.md b/src/roadmaps/typescript/content/111-utility-types/108-parameters.md index 1f49ec8a4..82761e7e2 100644 --- a/src/roadmaps/typescript/content/111-utility-types/108-parameters.md +++ b/src/roadmaps/typescript/content/111-utility-types/108-parameters.md @@ -1 +1,38 @@ -# Parameters \ No newline at end of file +# Parameters + +Parameters constructs a tuple type from the types used in the parameters of a function type Type. + + ``` + declare function f1(arg: { a: number; b: string }): void; + type T0 = Parameters<() => string>; + type T0 = [] + type T1 = Parameters<(s: string) => void>; + type T1 = [s: string] + type T2 = Parameters<(arg: T) => T>; + type T2 = [arg: unknown] + type T3 = Parameters; + + type T3 = [arg: { + a: number; + b: string; + }] + type T4 = Parameters; + + type T4 = unknown[] + type T5 = Parameters; + + type T5 = never + type T6 = Parameters; + Type 'string' does not satisfy the constraint '(...args: any) => any'. + + type T6 = never + type T7 = Parameters; + Type 'Function' does not satisfy the constraint '(...args: any) => any'. + Type 'Function' provides no match for the signature '(...args: any): any'. + + type T7 = never + ``` + +Learn more from the following links: + +- [Parameters](https://www.typescriptlang.org/docs/handbook/utility-types.html#parameterstype) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/111-utility-types/109-return-type.md b/src/roadmaps/typescript/content/111-utility-types/109-return-type.md index 0f9cc92e8..2294d0dc5 100644 --- a/src/roadmaps/typescript/content/111-utility-types/109-return-type.md +++ b/src/roadmaps/typescript/content/111-utility-types/109-return-type.md @@ -1 +1,44 @@ -# Return type \ No newline at end of file +# Return type + +Return type constructs a type consisting of the return type of function Type. + + ``` + declare function f1(): { a: number; b: string }; + type T0 = ReturnType<() => string>; + + type T0 = string + type T1 = ReturnType<(s: string) => void>; + + type T1 = void + type T2 = ReturnType<() => T>; + + type T2 = unknown + type T3 = ReturnType<() => T>; + + type T3 = number[] + type T4 = ReturnType; + + type T4 = { + a: number; + b: string; + } + type T5 = ReturnType; + + type T5 = any + type T6 = ReturnType; + + type T6 = never + type T7 = ReturnType; + Type 'string' does not satisfy the constraint '(...args: any) => any'. + + type T7 = any + type T8 = ReturnType; + Type 'Function' does not satisfy the constraint '(...args: any) => any'. + Type 'Function' provides no match for the signature '(...args: any): any'. + + type T8 = any + ``` + +Learn more from the following links: + +- [ReturnType](https://www.typescriptlang.org/docs/handbook/utility-types.html#returntypetype) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/111-utility-types/110-instance-type.md b/src/roadmaps/typescript/content/111-utility-types/110-instance-type.md index 2c7c1df96..013dd0047 100644 --- a/src/roadmaps/typescript/content/111-utility-types/110-instance-type.md +++ b/src/roadmaps/typescript/content/111-utility-types/110-instance-type.md @@ -1 +1,34 @@ -# Instance type \ No newline at end of file +# Instance type + + +This type constructs a type consisting of the instance type of a constructor function in Type. + + ``` + class C { + x = 0; + y = 0; + } + + type T0 = InstanceType; + + type T0 = C + type T1 = InstanceType; + + type T1 = any + type T2 = InstanceType; + + type T2 = never + type T3 = InstanceType; + Type 'string' does not satisfy the constraint 'abstract new (...args: any) => any'. + + type T3 = any + type T4 = InstanceType; + Type 'Function' does not satisfy the constraint 'abstract new (...args: any) => any'. + Type 'Function' provides no match for the signature 'new (...args: any): any'. + + type T4 = any + ``` + +Learn more from the following links: + +- [InstanceType](https://www.typescriptlang.org/docs/handbook/utility-types.html#instancetypetype) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/111-utility-types/111-awaited.md b/src/roadmaps/typescript/content/111-utility-types/111-awaited.md index bf536fd09..32541b4cd 100644 --- a/src/roadmaps/typescript/content/111-utility-types/111-awaited.md +++ b/src/roadmaps/typescript/content/111-utility-types/111-awaited.md @@ -1 +1,21 @@ -# Awaited \ No newline at end of file +# Awaited + +This type is meant to model operations like await in async functions, or the .then() method on Promises - specifically, the way that they recursively unwrap Promises. + + ``` + type A = Awaited>; + + type A = string + + type B = Awaited>>; + + type B = number + + type C = Awaited>; + + type C = number | boolean + ``` + +Learn more from the following links: + +- [Awaited](https://www.typescriptlang.org/docs/handbook/utility-types.html#awaitedtype) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/111-utility-types/index.md b/src/roadmaps/typescript/content/111-utility-types/index.md index 83cf608bb..77b12d47b 100644 --- a/src/roadmaps/typescript/content/111-utility-types/index.md +++ b/src/roadmaps/typescript/content/111-utility-types/index.md @@ -1 +1,13 @@ -# Utility types \ No newline at end of file +# Utility Types + +TypeScript provides several utility types that can be used to manipulate and transform existing types. Here are some of the most common ones: + +1. Partial: makes all properties of a type optional. +2. Readonly: makes all properties of a type read-only. +3. Pick: allows you to pick specific properties from a type. +4. Omit: allows you to omit specific properties from a type. +5. Exclude: creates a type that is the set difference of A and B. + +Learn more from the following links: + +- [TypeScript - Utility Types](https://www.typescriptlang.org/docs/handbook/utility-types.html) \ No newline at end of file