From 3686f32fe7f293f5c821e488d669874c3dbe9a69 Mon Sep 17 00:00:00 2001 From: zhuba-Ahhh <3477826311@qq.com> Date: Mon, 8 Apr 2024 16:07:44 +0800 Subject: [PATCH] =?UTF-8?q?=E7=89=B9=E6=80=A7:=20:sparkles:=20git=20add=20?= =?UTF-8?q?08-ts-utility=5Ftypes=20content?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/pnpm.yml | 4 +-- src/08-ts-utility_types/Awaited.ts | 8 +++++ .../ConstructorParameters.ts | 9 ++++++ src/08-ts-utility_types/Exclude.ts | 6 ++++ src/08-ts-utility_types/Extract.ts | 5 ++++ src/08-ts-utility_types/InstanceType.ts | 13 ++++++++ src/08-ts-utility_types/NonNullable.ts | 5 ++++ src/08-ts-utility_types/Omit.ts | 28 +++++++++++++++++ src/08-ts-utility_types/OmitThisParameter.ts | 10 +++++++ src/08-ts-utility_types/Parameters.ts | 13 ++++++++ src/08-ts-utility_types/Partial.ts | 20 +++++++++++++ src/08-ts-utility_types/Pick.ts | 17 +++++++++++ src/08-ts-utility_types/Readonly.ts | 16 ++++++++++ src/08-ts-utility_types/Record.ts | 17 +++++++++++ src/08-ts-utility_types/Required.ts | 12 ++++++++ src/08-ts-utility_types/ReturnType.ts | 14 +++++++++ src/08-ts-utility_types/ThisParameterType.ts | 10 +++++++ src/08-ts-utility_types/ThisType.ts | 30 +++++++++++++++++++ ...15\344\275\234\347\261\273\345\236\213.ts" | 5 ++++ 19 files changed, 239 insertions(+), 3 deletions(-) create mode 100644 src/08-ts-utility_types/Awaited.ts create mode 100644 src/08-ts-utility_types/ConstructorParameters.ts create mode 100644 src/08-ts-utility_types/Exclude.ts create mode 100644 src/08-ts-utility_types/Extract.ts create mode 100644 src/08-ts-utility_types/InstanceType.ts create mode 100644 src/08-ts-utility_types/NonNullable.ts create mode 100644 src/08-ts-utility_types/Omit.ts create mode 100644 src/08-ts-utility_types/OmitThisParameter.ts create mode 100644 src/08-ts-utility_types/Parameters.ts create mode 100644 src/08-ts-utility_types/Partial.ts create mode 100644 src/08-ts-utility_types/Pick.ts create mode 100644 src/08-ts-utility_types/Readonly.ts create mode 100644 src/08-ts-utility_types/Record.ts create mode 100644 src/08-ts-utility_types/Required.ts create mode 100644 src/08-ts-utility_types/ReturnType.ts create mode 100644 src/08-ts-utility_types/ThisParameterType.ts create mode 100644 src/08-ts-utility_types/ThisType.ts create mode 100644 "src/08-ts-utility_types/\345\206\205\345\234\250\345\255\227\347\254\246\344\270\262\346\223\215\344\275\234\347\261\273\345\236\213.ts" diff --git a/.github/workflows/pnpm.yml b/.github/workflows/pnpm.yml index adf1801..32ec9fa 100644 --- a/.github/workflows/pnpm.yml +++ b/.github/workflows/pnpm.yml @@ -1,4 +1,4 @@ -name: Bear-UI Pnpm +name: Pnpm on: push: @@ -20,5 +20,3 @@ jobs: run: pnpm install - name: Jest run: pnpm jest - # - name: Serve Docs - # run: pnpm docs:serve diff --git a/src/08-ts-utility_types/Awaited.ts b/src/08-ts-utility_types/Awaited.ts new file mode 100644 index 0000000..7ffff27 --- /dev/null +++ b/src/08-ts-utility_types/Awaited.ts @@ -0,0 +1,8 @@ +// Awaited +// 这种类型旨在模拟 async 函数中的 await 之类的操作,或 Promise 上的 .then() 方法 - 特别是它们递归解包 Promise 的方式。 + +type A = Awaited>; + +type B = Awaited>>; + +type C1 = Awaited>; diff --git a/src/08-ts-utility_types/ConstructorParameters.ts b/src/08-ts-utility_types/ConstructorParameters.ts new file mode 100644 index 0000000..b71a47e --- /dev/null +++ b/src/08-ts-utility_types/ConstructorParameters.ts @@ -0,0 +1,9 @@ +// ConstructorParameters +// 从构造函数类型的类型构造元组或数组类型。它生成一个包含所有参数类型的元组类型(如果 Type 不是函数,则生成类型 never)。 + +type T01 = ConstructorParameters; +type T11 = ConstructorParameters; +type T21 = ConstructorParameters; +type T31 = ConstructorParameters; + +type T41 = ConstructorParameters; \ No newline at end of file diff --git a/src/08-ts-utility_types/Exclude.ts b/src/08-ts-utility_types/Exclude.ts new file mode 100644 index 0000000..7c5792b --- /dev/null +++ b/src/08-ts-utility_types/Exclude.ts @@ -0,0 +1,6 @@ +// Exclude +// 通过从 UnionType 中排除所有可分配给 ExcludedMembers 的联合成员来构造一个类型。 + +type T02 = Exclude<"a" | "b" | "c", "a">; +type T12 = Exclude<"a" | "b" | "c", "a" | "b">; +type T22 = Exclude void), Function>; \ No newline at end of file diff --git a/src/08-ts-utility_types/Extract.ts b/src/08-ts-utility_types/Extract.ts new file mode 100644 index 0000000..15f4411 --- /dev/null +++ b/src/08-ts-utility_types/Extract.ts @@ -0,0 +1,5 @@ +// Extract +// 通过从 Type 中提取所有可分配给 Union 的联合成员来构造一个类型。 + +type T04 = Extract<"a" | "b" | "c", "a" | "f">; +type T14 = Extract void), Function>; \ No newline at end of file diff --git a/src/08-ts-utility_types/InstanceType.ts b/src/08-ts-utility_types/InstanceType.ts new file mode 100644 index 0000000..b1a1a25 --- /dev/null +++ b/src/08-ts-utility_types/InstanceType.ts @@ -0,0 +1,13 @@ +// InstanceType +// 构造一个由 Type 中的构造函数的实例类型组成的类型。 + +class C { + x = 0; + y = 0; +} + +type T0 = InstanceType; +type T1 = InstanceType; +type T2 = InstanceType; +type T3 = InstanceType; +type T4 = InstanceType; diff --git a/src/08-ts-utility_types/NonNullable.ts b/src/08-ts-utility_types/NonNullable.ts new file mode 100644 index 0000000..5ca65cf --- /dev/null +++ b/src/08-ts-utility_types/NonNullable.ts @@ -0,0 +1,5 @@ +// NonNullable +// 通过从 Type 中排除 null 和 undefined 来构造一个类型。 + +type T0 = NonNullable; +type T1 = NonNullable; diff --git a/src/08-ts-utility_types/Omit.ts b/src/08-ts-utility_types/Omit.ts new file mode 100644 index 0000000..4c4ffb6 --- /dev/null +++ b/src/08-ts-utility_types/Omit.ts @@ -0,0 +1,28 @@ +// Omit +// 通过从 Type 中选择所有属性然后删除 Keys(字符串字面或字符串字面的并集)来构造一个类型。 + +interface Todo { + title: string; + description: string; + completed: boolean; + createdAt: number; +} + +type TodoPreview = Omit; + +const todo: TodoPreview = { + title: "Clean room", + completed: false, + createdAt: 1615544252770, +}; + +todo; + +type TodoInfo = Omit; + +const todoInfo: TodoInfo = { + title: "Pick up kids", + description: "Kindergarten closes at 5pm", +}; + +todoInfo; \ No newline at end of file diff --git a/src/08-ts-utility_types/OmitThisParameter.ts b/src/08-ts-utility_types/OmitThisParameter.ts new file mode 100644 index 0000000..a637a84 --- /dev/null +++ b/src/08-ts-utility_types/OmitThisParameter.ts @@ -0,0 +1,10 @@ +// OmitThisParameter +// 从 Type 中删除 this 参数。如果 Type 没有显式声明的 this 参数,则结果只是 Type。否则,将从 Type 创建一个没有 this 参数的新函数类型。泛型被删除,只有最后一个重载签名被传播到新的函数类型中。 + +function toHex(this: Number) { + return this.toString(16); +} + +const fiveToHex: OmitThisParameter = toHex.bind(5); + +console.log(fiveToHex()); diff --git a/src/08-ts-utility_types/Parameters.ts b/src/08-ts-utility_types/Parameters.ts new file mode 100644 index 0000000..ff827fa --- /dev/null +++ b/src/08-ts-utility_types/Parameters.ts @@ -0,0 +1,13 @@ +// Parameters +// 从函数类型 Type 的参数中使用的类型构造元组类型。 + +declare function f1(arg: { a: number; b: string }): void; + +type T0 = Parameters<() => string>; +type T1 = Parameters<(s: string) => void>; +type T2 = Parameters<(arg: T) => T>; +type T3 = Parameters; +type T4 = Parameters; +type T5 = Parameters; +type T6 = Parameters; +type T7 = Parameters; \ No newline at end of file diff --git a/src/08-ts-utility_types/Partial.ts b/src/08-ts-utility_types/Partial.ts new file mode 100644 index 0000000..8ed767b --- /dev/null +++ b/src/08-ts-utility_types/Partial.ts @@ -0,0 +1,20 @@ +// Partial +// 构造一个将 Type 的所有属性设置为可选的类型。此实用程序将返回一个表示给定类型的所有子集的类型。 + +interface Todo { + title: string; + description: string; +} + +function updateTodo(todo: Todo, fieldsToUpdate: Partial) { + return { ...todo, ...fieldsToUpdate }; +} + +const todo1:Todo = { + title: "organize desk", + description: "clear clutter", +}; + +const todo2 = updateTodo(todo1, { + description: "throw out trash", +}); \ No newline at end of file diff --git a/src/08-ts-utility_types/Pick.ts b/src/08-ts-utility_types/Pick.ts new file mode 100644 index 0000000..426f67d --- /dev/null +++ b/src/08-ts-utility_types/Pick.ts @@ -0,0 +1,17 @@ +// Pick +// 通过从 Type 中选取一组属性 Keys(字符串字面或字符串字面的并集)来构造一个类型。 + +interface Todo3 { + title: string; + description: string; + completed: boolean; +} + +type TodoPreview1 = Pick; + +const todo4: TodoPreview1 = { + title: "Clean room", + completed: false, +}; + +todo; diff --git a/src/08-ts-utility_types/Readonly.ts b/src/08-ts-utility_types/Readonly.ts new file mode 100644 index 0000000..90ee04f --- /dev/null +++ b/src/08-ts-utility_types/Readonly.ts @@ -0,0 +1,16 @@ +// Readonly +// 构造一个将 Type 的所有属性设置为 readonly 的类型,这意味着构造类型的属性不能重新分配。 + +interface Todo2 { + title: string; +} + +const todo3: Readonly = { + title: "Delete inactive users", +}; + +todo3.title = "Hello"; + + +// Object.freeze +// function freeze(obj: Type): Readonly; \ No newline at end of file diff --git a/src/08-ts-utility_types/Record.ts b/src/08-ts-utility_types/Record.ts new file mode 100644 index 0000000..c48621f --- /dev/null +++ b/src/08-ts-utility_types/Record.ts @@ -0,0 +1,17 @@ +// Record +// 构造一个对象类型,其属性键为 Keys,其属性值为 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; diff --git a/src/08-ts-utility_types/Required.ts b/src/08-ts-utility_types/Required.ts new file mode 100644 index 0000000..d21c231 --- /dev/null +++ b/src/08-ts-utility_types/Required.ts @@ -0,0 +1,12 @@ +// // Required +// 构造一个由设置为 required 的 Type 的所有属性组成的类型。与Partial相反。 + +interface Props { + a?: number; + b?: string; +} + +const obj: Props = { a: 5 }; + +const obj2: Required = { a: 5 }; +// Property 'b' is missing in type '{ a: number; }' but required in type 'Required'. \ No newline at end of file diff --git a/src/08-ts-utility_types/ReturnType.ts b/src/08-ts-utility_types/ReturnType.ts new file mode 100644 index 0000000..fbb4fc0 --- /dev/null +++ b/src/08-ts-utility_types/ReturnType.ts @@ -0,0 +1,14 @@ +// ReturnType +// 构造一个由函数 Type 的返回类型组成的类型。 + +declare function f1(): { a: number; b: string }; + +type T0 = ReturnType<() => string>; +type T1 = ReturnType<(s: string) => void>; +type T2 = ReturnType<() => T>; +type T3 = ReturnType<() => T>; +type T4 = ReturnType; +type T5 = ReturnType; +type T6 = ReturnType; +type T7 = ReturnType; +type T8 = ReturnType; diff --git a/src/08-ts-utility_types/ThisParameterType.ts b/src/08-ts-utility_types/ThisParameterType.ts new file mode 100644 index 0000000..fd37443 --- /dev/null +++ b/src/08-ts-utility_types/ThisParameterType.ts @@ -0,0 +1,10 @@ +// ThisParameterType +// 提取函数类型的 此 参数的类型,如果函数类型没有 this 参数,则提取 未知。 + +function toHex(this: Number) { + return this.toString(16); +} + +function numberToString(n: ThisParameterType) { + return toHex.apply(n); +} diff --git a/src/08-ts-utility_types/ThisType.ts b/src/08-ts-utility_types/ThisType.ts new file mode 100644 index 0000000..929c530 --- /dev/null +++ b/src/08-ts-utility_types/ThisType.ts @@ -0,0 +1,30 @@ +// ThisType +// 此实用程序不返回转换后的类型。相反,它用作上下文 this 类型的标记。请注意,必须启用 noImplicitThis 标志才能使用此实用程序。 + +type ObjectDescriptor = { + data?: D; + methods?: M & ThisType; // Type of 'this' in methods is D & M +}; + +function makeObject(desc: ObjectDescriptor): D & M { + let data: object = desc.data || {}; + let methods: object = desc.methods || {}; + return { ...data, ...methods } as D & M; +} + +let obj = makeObject({ + data: { x: 0, y: 0 }, + methods: { + moveBy(dx: number, dy: number) { + this.x += dx; // Strongly typed this + this.y += dy; // Strongly typed this + }, + }, +}); + +obj.x = 10; +obj.y = 20; +obj.moveBy(5, 5); +// 在上面的示例中,makeObject 参数中的 methods 对象具有包含 ThisType 的上下文类型,因此 methods 对象内的方法中的 此 类型是 { x: number, y: number } & { moveBy(dx: number, dy: number): number }。请注意 methods 属性的类型如何同时是方法中 this 类型的推理目标和源。 + +// ThisType 标记接口只是在 lib.d.ts 中声明的一个空接口。除了在对象字面的上下文类型中被识别之外,接口的行为就像任何空接口。 \ No newline at end of file diff --git "a/src/08-ts-utility_types/\345\206\205\345\234\250\345\255\227\347\254\246\344\270\262\346\223\215\344\275\234\347\261\273\345\236\213.ts" "b/src/08-ts-utility_types/\345\206\205\345\234\250\345\255\227\347\254\246\344\270\262\346\223\215\344\275\234\347\261\273\345\236\213.ts" new file mode 100644 index 0000000..92a29e6 --- /dev/null +++ "b/src/08-ts-utility_types/\345\206\205\345\234\250\345\255\227\347\254\246\344\270\262\346\223\215\344\275\234\347\261\273\345\236\213.ts" @@ -0,0 +1,5 @@ +// 内在字符串操作类型 +// Uppercase +// Lowercase +// Capitalize +// Uncapitalize中英