node_modules/typescript/lib/lib.es5.d.ts
官网:https://www.typescriptlang.org/docs/handbook/utility-types.html

同学笔记:
TS常用关键字和工具范型梳理 https://snowwolf2.yuque.com/staff-rmd8eh/gmflbs/ngchuc
TS Utils https://snowwolf2.yuque.com/staff-rmd8eh/gmflbs/ra4fve#ekml3

https://github.com/type-challenges/type-challenges

Pick

  1. type MyPick <T, K extends keyof T> = {
  2. [P in K]: T[P];
  3. };

用法

https://www.typescriptlang.org/docs/handbook/utility-types.html#picktype-keys

解析:

  1. function myPick (todo, keys) {
  2. let ans = {};
  3. for (let key of keys) {
  4. if (key in todo) {
  5. ans[key] = todo[key];
  6. }
  7. }
  8. return ans;
  9. }

涉及到的知识点

:::success 1.返回一个对象
2.遍历foreach mapped
https://www.typescriptlang.org/docs/handbook/2/mapped-types.html
3.todo[key] 取值indexed
https://www.typescriptlang.org/docs/handbook/2/indexed-access-types.html
4.看看key在不在todo里面
1.keyof lookup
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html#keyof-and-lookup-types
2.extends条件约束
https://www.typescriptlang.org/docs/handbook/2/generics.html#generic-constraints :::

Readonly

  1. type MyReadonly<T> = {
  2. readonly [P in keyof T]: T[P];
  3. }

用法

https://www.typescriptlang.org/docs/handbook/utility-types.html#readonlytype

解析:

  1. function myReadonly(todo) {
  2. let ans = {};
  3. for (let key in todo) {
  4. ans[`readonly ${key}`] = todo[key];
  5. }
  6. return ans;
  7. }

涉及到的知识点

:::success 1.返回一个对象
2.遍历对象
2.1in
2.2mapped keyof
2.3lookup
3.添加readonly关键字 【新知识】
4.通过key获取对应的值 :::

Tuple to Object

解析case:

  1. typeof 获取类型
  2. 字面量类型 , ex: const
  3. @ts-expect-error 注释

解析题目:

  1. 返回一个对象
  2. 遍历数组 P in T[number]
  1. type TupleToObject<T extends readonly (number | string | symbol)[]> = {
  2. [P in T[number]]: P;
  3. }

image.png