From 7e78c37d6d8504849b3395489cc376d35e36f6fa Mon Sep 17 00:00:00 2001 From: ChinaCarlos <1302151931@qq.com> Date: Wed, 1 Jan 2025 15:13:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E6=89=8B=E5=86=99?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/interview/Typescript/index.md | 49 ++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) 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; + +```