diff --git a/docs/interview/Typescript/index.md b/docs/interview/Typescript/index.md index cdfd794..2fcbf81 100644 --- a/docs/interview/Typescript/index.md +++ b/docs/interview/Typescript/index.md @@ -1442,3 +1442,52 @@ console.log(window.customProperty); // "This is a custom property" | `Merge` | 合并多个类型,属性重复时后面的类型会覆盖前面的类型 | | `Intersection` | 将多个类型合并成一个类型,包含所有属性 | | `Overwrite` | 用类型 `U` 中的字段覆盖类型 `T` 中的相应字段 | + +## 34. typescript 代码实现题 + +```typescript +// ReadOnly 实现 +type MyReadOnly = { + readonly [P in keyof T]: T[P]; +}; + +type Info = { + name: string; + age: number; +}; + +type myonly = MyReadOnly; + + +// Pick 实现 +type User = { + name: string; + age: number; +}; + + +type MyPick { + [P in K] : T[P] +} + +type myPick = MyPick + + +// Omit 实现 +type MyOmit = { + [P in keyof T as P extends K ? never : P]: T[P]; +}; + +type Foot = { + name: string; + price: number; +}; + +type GreatFoot = MyOmit; + +// Exclude 实现 +type MyExclude = T extends U ? never : T; + +type test = MyExclude; + +```