awesome-typescript
awesome-typescript copied to clipboard
A collection of awesome TypeScript resources for client-side and server-side development
### 第四题 `Pick` 的作用是将某个类型中的子属性挑出来,变成包含这个类型部分属性的子类型。 ```typescript interface Todo { title: string; description: string; completed: boolean; } type TodoPreview = Pick; const todo: TodoPreview = { title: "Clean room", completed: false };...
实现一个 `Permutation` 工具类型,当输入一个联合类型时,返回一个包含该联合类型的全排列类型数组。具体的使用示例如下所示: ```typescript type Permutation = // 你的实现代码 // ["a", "b"] | ["b", "a"] type P0 = Permutation // ['a', 'b'] | ['b' | 'a'] // type P1 =...
实现一个 `IsEqual` 工具类型,用于比较两个类型是否相等。具体的使用示例如下所示: ```typescript type IsEqual = // 你的实现代码 // 测试用例 type E0 = IsEqual; // false type E1 = IsEqual // true type E2 = IsEqual; // false ```...
### 第十题 实现一个 `Trim` 工具类型,用于对字符串字面量类型进行去空格处理。具体的使用示例如下所示: ```typescript type Trim = // 你的实现代码 // 测试用例 Trim //=> 'semlinker' ``` > 提示:可以考虑先定义 TrimLeft 和 TrimRight 工具类型,再组合成 Trim 工具类型。 > 请在下面评论你的答案。
实现一个 `Tail` 工具类型,用于获取数组类型除了第一个类型外,剩余的类型。具体的使用示例如下所示: ```typescript type Tail = // 你的实现代码 // 测试用例 type T0 = Tail // [] type T1 = Tail // [2] type T2 = Tail // [2, 3,...
实现 `ConsistsOnlyOf` 工具类型,用于判断 `LongString` 字符串类型是否由 0 个或多个 `Substring` 字符串类型组成。具体的使用示例如下所示: ```typescript type ConsistsOnlyOf = // 你的实现代码 type C0 = ConsistsOnlyOf //=> true type C1 = ConsistsOnlyOf //=> true type C2 =...
实现 `RequireExactlyOne` 工具类型,用于满足以下功能。即只能包含 `age` 或 `gender` 属性,不能同时包含这两个属性。具体的使用示例如下所示: ```typescript interface Person { name: string; age?: number; gender?: number; } // 只能包含Keys中唯一的一个Key type RequireExactlyOne = // 你的实现代码 const p1: RequireExactlyOne = {...
实现一个 `RequireAtLeastOne` 工具类型,它将创建至少含有一个给定 `Keys` 的类型,其余的 `Keys` 保持原样。具体的使用示例如下所示: ```typescript type Responder = { text?: () => string; json?: () => string; secure?: boolean; }; type RequireAtLeastOne< ObjectType, KeysType extends keyof ObjectType...
实现 `JsonifiedObject` 工具类型,用于对 `object` 对象类型进行序列化操作。具体的使用示例如下所示: ```typescript type JsonifiedObject = // 你的实现代码 type MyObject = { str: "literalstring", fn: () => void, date: Date, customClass: MyClass, obj: { prop: "property", clz:...
完善 `Chainable` 类型的定义,使得 TS 能成功推断出 `result` 变量的类型。调用 `option` 方法之后会不断扩展当前对象的类型,使得调用 `get` 方法后能获取正确的类型。 ```typescript declare const config: Chainable type Chainable = { option(key: string, value: any): any get(): any } const result...