Skip to content

Commit afeb366

Browse files
committed
feat: 工具类型
1 parent f09651d commit afeb366

File tree

4 files changed

+108
-1
lines changed

4 files changed

+108
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [1.0.0](https://github.com/Rain120/typescript-guide/compare/0.0.1...1.0.0) (2022-01-21)
1+
# [1.0.0](https://github.com/Rain120/typescript-guide/compare/0.0.1...1.0.0) (2024-01-16)
22

33

44
### Bug Fixes
@@ -20,6 +20,7 @@
2020
* **extends:** update ([8cd0fdc](https://github.com/Rain120/typescript-guide/commit/8cd0fdc819baf8178a16e3b3dcc77b3b90c11ec3))
2121
* **intersection-types:** update ([e5ffe1b](https://github.com/Rain120/typescript-guide/commit/e5ffe1bd61dd154329422a420c9a91f3346e6a90))
2222
* **keyof:** update usage ([37c61cb](https://github.com/Rain120/typescript-guide/commit/37c61cbf14eb66f5c84c8cd90a27b79183cc0bf7))
23+
* **utility types:** custom ([50e8703](https://github.com/Rain120/typescript-guide/commit/50e87037bc7ad9b20757efa1ad18734db428f0a5))
2324
* add Previously at keyword for descript the resource about it ([be81d8e](https://github.com/Rain120/typescript-guide/commit/be81d8e5f2f6d2892852f72143b09607ddc1d78a))
2425
* blog tips & pdfs & books ([310765f](https://github.com/Rain120/typescript-guide/commit/310765ffd4e95bcde3dc3a33c3ca7115f80d30f1))
2526
* compile-config about module resolver & flie inclusive; keyword about is ([4b5832d](https://github.com/Rain120/typescript-guide/commit/4b5832df75a82cd75b553463d1ed476f22d20fd1))

docs/.vuepress/utils/alias.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
"interface-vs-type": "interface 和 type 有什么异同点?",
3636
"module-resolver": "Typescript 模块解析",
3737
"custom-utility-types": "自定义工具类型",
38+
"if-else": "条件类型",
39+
"spread": "拓展运算符",
3840
"tips": "Typescript Tips",
3941
"faqs": "Typescript FAQs"
4042
}

docs/zh/tips/if-else/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## 定义
2+
3+
条件类型,条件类型冒号左边为 if 右边为 else
4+
5+
## 使用
6+
7+
```ts
8+
type If<T, K> = T extends K ? true : false;
9+
10+
type IfValue<T extends boolean, K, L> = T extends true ? K : L;
11+
```
12+
13+
## 快来耍耍啊
14+
15+
### 🌰🌰
16+
17+
<!-- 题目 -->
18+
19+
```ts
20+
type A = { name: string }
21+
type B = { name: string; age?: number | string }
22+
23+
type x = If<A, B>
24+
25+
type xA = IfValue<true, A, B>
26+
type xB = IfValue<false, A, B>
27+
28+
```
29+
30+
[Go Demo](https://www.typescriptlang.org/play?#code/C4TwDgpgBAkgZgHgCoBooGkB8UC8UlQQAewEAdgCYDOGUA-FMAE4Cu0AXFHAIYA2VEANwAoYaEiw4ANT5tkhEuWpQARgHs1vCNzJp0aADLY8BYqUo1mberU4GRY8NACCuKAG8oZbgFsInKmYASzIAcygAX0cJACE3T28-AOCwwShuUIg6TjIWHxUIJigAHyhAphDwqOjoIjd4BGc0GMxRcVrXPHgZXjkrCDQmqBaaqCI4rulZCAQefgGoIZagA)
31+
32+
### 游乐场
33+
34+
<br />
35+
36+
<Editor
37+
value='// enjoy yourself'
38+
/>
39+
40+
### 参考答案
41+
42+
```ts
43+
// answer
44+
```
45+
46+
## 参考资料
47+
48+

docs/zh/tips/spread/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## 定义
2+
3+
拓展运算符
4+
5+
## 使用
6+
7+
```ts
8+
type spreadFirst<T> = T extends [infer First, ...infer Rest] ? First : never;
9+
10+
type spreadLast<T> = T extends [...infer First, infer Last] ? Last : never;
11+
12+
type spreadFirstChar<T> = T extends `${infer First}${infer Rest}`? First : never;
13+
14+
type spreadRestChar<T> = T extends `${infer First}${infer Rest}`? Rest : never;
15+
```
16+
17+
## 快来耍耍啊
18+
19+
### 🌰🌰
20+
21+
<!-- 题目 -->
22+
23+
```ts
24+
type Arr = [1, 2, 3]
25+
type Str = '123'
26+
type Num = 123
27+
28+
type FA = spreadFirst<Arr> // 1
29+
type LA = spreadLast<Arr> // 3
30+
31+
type FS = spreadFirstChar<Str> // 1
32+
type LS = spreadRestChar<Str> // 23
33+
34+
type FN = spreadFirstChar<Num> // never
35+
36+
```
37+
38+
[Go Demo](https://www.typescriptlang.org/play?#code/C4TwDgpgBAzmBOECGATAYgS3jYAeAKgHxQC8U+UEAHsBAHYoxQDaGdAZhPFJtsADRQAdCLaduAJQg4AulAD8PLDigAuKHQgA3LgG4AUPtCRYCZCgAySHAWJkK1WgybMRQsVyV9BH7ldkKUP7AahraeobG0HCIqLw4AMIAFkjwtqTklDT0jFAABgAkAN6+XjgAvsWlUhV5ivEh6po68AZG4NFmqDXAyanp9llOuYUlHJ4NlWPiUD3ldbPSjWEtbVFQAILw3GTMAIyCAEyCAMwy7SYAysA7UADke4cndxfQAHIArgC2GY8nkR0eBsMjFzA1cFt4IRXkFgWRQahghDttCYWhLiCuuhlL0UmlrlCYRYMfCsT0+vibqj1mg3pjYti+BTcJ8vtCgA)
39+
40+
### 游乐场
41+
42+
<br />
43+
44+
<Editor
45+
value='// enjoy yourself'
46+
/>
47+
48+
### 参考答案
49+
50+
```ts
51+
// answer
52+
```
53+
54+
## 参考资料
55+
56+

0 commit comments

Comments
 (0)