Adding content to 111-utility-types

pull/3423/head
syedmouaazfarrukh 2 years ago committed by Kamran Ahmed
parent 49e78cf1c0
commit 1df4e4b836
  1. 31
      src/roadmaps/typescript/content/111-utility-types/100-partial.md
  2. 27
      src/roadmaps/typescript/content/111-utility-types/101-pick.md
  3. 41
      src/roadmaps/typescript/content/111-utility-types/102-omit.md
  4. 21
      src/roadmaps/typescript/content/111-utility-types/103-readonly.md
  5. 27
      src/roadmaps/typescript/content/111-utility-types/104-record.md
  6. 20
      src/roadmaps/typescript/content/111-utility-types/105-exclude.md
  7. 17
      src/roadmaps/typescript/content/111-utility-types/106-extract.md
  8. 17
      src/roadmaps/typescript/content/111-utility-types/107-non-nullable.md
  9. 39
      src/roadmaps/typescript/content/111-utility-types/108-parameters.md
  10. 45
      src/roadmaps/typescript/content/111-utility-types/109-return-type.md
  11. 35
      src/roadmaps/typescript/content/111-utility-types/110-instance-type.md
  12. 22
      src/roadmaps/typescript/content/111-utility-types/111-awaited.md
  13. 14
      src/roadmaps/typescript/content/111-utility-types/index.md

@ -1 +1,30 @@
# Partial
# 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>): 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<Type>](https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype)

@ -1 +1,26 @@
# Pick
# 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<Todo, "title" | "completed">;
const todo: TodoPreview = {
title: "Clean room",
completed: false,
};
todo;
const todo: TodoPreview
```
Learn more from the following links:
- [Pick<Type, Keys>](https://www.typescriptlang.org/docs/handbook/utility-types.html#picktype-keys)

@ -1 +1,40 @@
# Omit
# 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<Todo, "description">;
const todo: TodoPreview = {
title: "Clean room",
completed: false,
createdAt: 1615544252770,
};
todo;
const todo: TodoPreview
type TodoInfo = Omit<Todo, "completed" | "createdAt">;
const todoInfo: TodoInfo = {
title: "Pick up kids",
description: "Kindergarten closes at 5pm",
};
todoInfo;
const todoInfo: TodoInfo
```
Learn more from the following links:
- [Omit<Type, Keys>](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys)

@ -1 +1,20 @@
# Readonly
# 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<Todo> = {
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<Type>](https://www.typescriptlang.org/docs/handbook/utility-types.html#readonlytype)

@ -1 +1,26 @@
# Record
# 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<CatName, CatInfo> = {
miffy: { age: 10, breed: "Persian" },
boris: { age: 5, breed: "Maine Coon" },
mordred: { age: 16, breed: "British Shorthair" },
};
cats.boris;
const cats: Record<CatName, CatInfo>
```
Learn more from the following links:
- [Record<Keys, Type>](https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type)

@ -1 +1,19 @@
# Exclude
# 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<string | number | (() => void), Function>;
type T2 = string | number
```
Learn more from the following links:
- [Exclude<UnionType, ExcludedMembers>](https://www.typescriptlang.org/docs/handbook/utility-types.html#excludeuniontype-excludedmembers)

@ -1 +1,16 @@
# Extract
# 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<string | number | (() => void), Function>;
type T1 = () => void
```
Learn more from the following links:
- [Extract<Type, Union>](https://www.typescriptlang.org/docs/handbook/utility-types.html#extracttype-union)

@ -1 +1,16 @@
# Non nullable
# Non Nullable
Nun Nullable constructs a type by excluding null and undefined from Type.
```
type T0 = NonNullable<string | number | undefined>;
type T0 = string | number
type T1 = NonNullable<string[] | null | undefined>;
type T1 = string[]
```
Learn more from the following links:
- [NonNullable<Type>](https://www.typescriptlang.org/docs/handbook/utility-types.html#nonnullabletype)

@ -1 +1,38 @@
# Parameters
# 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<<T>(arg: T) => T>;
type T2 = [arg: unknown]
type T3 = Parameters<typeof f1>;
type T3 = [arg: {
a: number;
b: string;
}]
type T4 = Parameters<any>;
type T4 = unknown[]
type T5 = Parameters<never>;
type T5 = never
type T6 = Parameters<string>;
Type 'string' does not satisfy the constraint '(...args: any) => any'.
type T6 = never
type T7 = Parameters<Function>;
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<Type>](https://www.typescriptlang.org/docs/handbook/utility-types.html#parameterstype)

@ -1 +1,44 @@
# Return type
# 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>() => T>;
type T2 = unknown
type T3 = ReturnType<<T extends U, U extends number[]>() => T>;
type T3 = number[]
type T4 = ReturnType<typeof f1>;
type T4 = {
a: number;
b: string;
}
type T5 = ReturnType<any>;
type T5 = any
type T6 = ReturnType<never>;
type T6 = never
type T7 = ReturnType<string>;
Type 'string' does not satisfy the constraint '(...args: any) => any'.
type T7 = any
type T8 = ReturnType<Function>;
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<Type>](https://www.typescriptlang.org/docs/handbook/utility-types.html#returntypetype)

@ -1 +1,34 @@
# Instance type
# 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<typeof C>;
type T0 = C
type T1 = InstanceType<any>;
type T1 = any
type T2 = InstanceType<never>;
type T2 = never
type T3 = InstanceType<string>;
Type 'string' does not satisfy the constraint 'abstract new (...args: any) => any'.
type T3 = any
type T4 = InstanceType<Function>;
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<Type>](https://www.typescriptlang.org/docs/handbook/utility-types.html#instancetypetype)

@ -1 +1,21 @@
# Awaited
# 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<Promise<string>>;
type A = string
type B = Awaited<Promise<Promise<number>>>;
type B = number
type C = Awaited<boolean | Promise<number>>;
type C = number | boolean
```
Learn more from the following links:
- [Awaited<Type>](https://www.typescriptlang.org/docs/handbook/utility-types.html#awaitedtype)

@ -1 +1,13 @@
# Utility types
# 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)
Loading…
Cancel
Save