ts-type 3.0.1 → 3.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +33 -0
- package/README.md +43 -9
- package/docs/01-base-types.md +218 -0
- package/docs/02-built-in-types.md +200 -0
- package/docs/03-function-types.md +156 -0
- package/docs/04-promise-types.md +166 -0
- package/docs/05-array-tuple-types.md +159 -0
- package/docs/06-object-record-types.md +234 -0
- package/docs/07-string-number-types.md +325 -0
- package/docs/08-logic-types.md +154 -0
- package/docs/README.md +155 -0
- package/lib/_build-in.d.ts +56 -2
- package/lib/_build-in.js +7 -2
- package/lib/_build-in.js.map +1 -1
- package/lib/generic.d.ts +33 -0
- package/lib/generic.js.map +1 -1
- package/lib/helper/array/readonly.d.ts +16 -0
- package/lib/helper/array/readonly.js.map +1 -1
- package/lib/helper/filter.d.ts +62 -4
- package/lib/helper/filter.js.map +1 -1
- package/lib/helper/infer.d.ts +19 -0
- package/lib/helper/infer.js.map +1 -1
- package/lib/helper/intersection.d.ts +3 -0
- package/lib/helper/intersection.js.map +1 -1
- package/lib/helper/key-value.d.ts +90 -4
- package/lib/helper/key-value.js +5 -1
- package/lib/helper/key-value.js.map +1 -1
- package/lib/helper/number.d.ts +23 -0
- package/lib/helper/number.js.map +1 -1
- package/lib/helper/overwrite.d.ts +4 -0
- package/lib/helper/overwrite.js.map +1 -1
- package/lib/helper/promise.d.ts +8 -0
- package/lib/helper/promise.js.map +1 -1
- package/lib/helper/readonly.d.ts +114 -2
- package/lib/helper/readonly.js.map +1 -1
- package/lib/helper/record/enum.d.ts +43 -0
- package/lib/helper/record/enum.js +7 -0
- package/lib/helper/record/enum.js.map +1 -1
- package/lib/helper/record/member.d.ts +31 -1
- package/lib/helper/record/member.js +7 -0
- package/lib/helper/record/member.js.map +1 -1
- package/lib/helper/record/omit-index.d.ts +45 -1
- package/lib/helper/record/omit-index.js +7 -1
- package/lib/helper/record/omit-index.js.map +1 -1
- package/lib/helper/record/partial.d.ts +28 -1
- package/lib/helper/record/partial.js +7 -0
- package/lib/helper/record/partial.js.map +1 -1
- package/lib/helper/record.d.ts +56 -5
- package/lib/helper/record.js +7 -1
- package/lib/helper/record.js.map +1 -1
- package/lib/helper/string/infer.d.ts +54 -0
- package/lib/helper/string/infer.js +7 -0
- package/lib/helper/string/infer.js.map +1 -1
- package/lib/helper/string/literal-string.d.ts +29 -2
- package/lib/helper/string/literal-string.js +7 -0
- package/lib/helper/string/literal-string.js.map +1 -1
- package/lib/helper/string.d.ts +59 -1
- package/lib/helper/string.js +7 -0
- package/lib/helper/string.js.map +1 -1
- package/lib/helper/tuple.d.ts +15 -2
- package/lib/helper/tuple.js +7 -0
- package/lib/helper/tuple.js.map +1 -1
- package/lib/helper/typeof.d.ts +34 -0
- package/lib/helper/typeof.js +6 -0
- package/lib/helper/typeof.js.map +1 -1
- package/lib/helper.d.ts +20 -3
- package/lib/helper.js.map +1 -1
- package/lib/internal/filter.d.ts +7 -0
- package/lib/internal/filter.js +7 -0
- package/lib/internal/filter.js.map +1 -1
- package/lib/internal/logic/string-literal.d.ts +35 -2
- package/lib/internal/logic/string-literal.js +7 -0
- package/lib/internal/logic/string-literal.js.map +1 -1
- package/lib/logic/any.d.ts +33 -0
- package/lib/logic/any.js +9 -0
- package/lib/logic/any.js.map +1 -1
- package/lib/logic/never.d.ts +22 -0
- package/lib/logic/never.js +7 -0
- package/lib/logic/never.js.map +1 -1
- package/lib/logic/union/index.d.ts +32 -6
- package/lib/logic/union/index.js +7 -0
- package/lib/logic/union/index.js.map +1 -1
- package/lib/type/base.d.ts +77 -1
- package/lib/type/base.js +5 -1
- package/lib/type/base.js.map +1 -1
- package/lib/type/iterator.d.ts +23 -0
- package/lib/type/iterator.js +7 -0
- package/lib/type/iterator.js.map +1 -1
- package/lib/type/promise.d.ts +61 -1
- package/lib/type/promise.js +6 -0
- package/lib/type/promise.js.map +1 -1
- package/lib/type/proxy.d.ts +19 -2
- package/lib/type/proxy.js +5 -1
- package/lib/type/proxy.js.map +1 -1
- package/lib/type/record/empty.d.ts +24 -0
- package/lib/type/record/empty.js +9 -0
- package/lib/type/record/empty.js.map +1 -1
- package/lib/type/record/enum.d.ts +49 -1
- package/lib/type/record/enum.js +7 -0
- package/lib/type/record/enum.js.map +1 -1
- package/lib/type/record/partial.d.ts +29 -0
- package/lib/type/record/partial.js +7 -0
- package/lib/type/record/partial.js.map +1 -1
- package/lib/type/record/readonly.d.ts +29 -0
- package/lib/type/record/readonly.js +7 -0
- package/lib/type/record/readonly.js.map +1 -1
- package/lib/type/record.d.ts +137 -15
- package/lib/type/record.js +6 -0
- package/lib/type/record.js.map +1 -1
- package/lib/type/tuple/empty.d.ts +15 -0
- package/lib/type/tuple/empty.js +7 -0
- package/lib/type/tuple/empty.js.map +1 -1
- package/package.json +32 -17
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
# 字串與數字類型工具 / String & Number Type Utilities
|
|
2
|
+
|
|
3
|
+
## 概述 / Overview
|
|
4
|
+
|
|
5
|
+
提供字串和數字類型的操作工具,包括字面量類型轉換,大小寫轉換、類型推斷等。
|
|
6
|
+
|
|
7
|
+
Provides string and number type manipulation utilities, including literal type conversion, case conversion, type inference, and more.
|
|
8
|
+
|
|
9
|
+
## 來源 / Source
|
|
10
|
+
|
|
11
|
+
- [`lib/helper/string.ts`](../lib/helper/string.ts)
|
|
12
|
+
- [`lib/helper/string/infer.ts`](../lib/helper/string/infer.ts)
|
|
13
|
+
- [`lib/helper/string/literal-string.ts`](../lib/helper/string/literal-string.ts)
|
|
14
|
+
- [`lib/helper/number.ts`](../lib/helper/number.ts)
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## 字串類型工具 / String Type Utilities
|
|
19
|
+
|
|
20
|
+
### ITSToStringLiteral
|
|
21
|
+
|
|
22
|
+
**將類型轉換為字面量類型**
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
type Str = ITSToStringLiteral<'hello'>;
|
|
26
|
+
// type Str = "hello"
|
|
27
|
+
|
|
28
|
+
type Num = ITSToStringLiteral<42>;
|
|
29
|
+
// type Num = "42"
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
### ITSTypeAndStringLiteral
|
|
35
|
+
|
|
36
|
+
**原始類型與其字面量類型的聯合**
|
|
37
|
+
|
|
38
|
+
適合用在 enum 或 string literal union,可以接受 enum 的字面量或基底類型。
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
// 應用於 string literal union
|
|
42
|
+
type Status = 'active' | 'inactive' | 'pending';
|
|
43
|
+
type IStatus = ITSTypeAndStringLiteral<Status>;
|
|
44
|
+
// type IStatus = "active" | "inactive" | "pending" | string
|
|
45
|
+
|
|
46
|
+
// 應用於 enum
|
|
47
|
+
enum EnumPackageManager {
|
|
48
|
+
'yarn' = 'yarn',
|
|
49
|
+
'npm' = 'npm',
|
|
50
|
+
'pnpm' = 'pnpm',
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
type IPackageManager = ITSTypeAndStringLiteral<EnumPackageManager>;
|
|
54
|
+
// type IPackageManager = EnumPackageManager | string
|
|
55
|
+
|
|
56
|
+
// 應用於 number,可接受數字或數字字串
|
|
57
|
+
type INumber = ITSTypeAndStringLiteral<number>;
|
|
58
|
+
// type INumber = number | `${number}`
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
### ITSAndStringLiteral
|
|
64
|
+
|
|
65
|
+
**原始類型 S 與 T 的字面量類型的聯合**
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
// 應用於數字字面量聯合
|
|
69
|
+
type Result = ITSAndStringLiteral<1 | 2 | 3, number>;
|
|
70
|
+
// type Result = number | "1" | "2" | "3"
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
### ITSAndTypeAndStringLiteral
|
|
76
|
+
|
|
77
|
+
**原始類型 S、T 與 T 的字面量類型的聯合**
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
// 應用於數字字面量聯合
|
|
81
|
+
type Result = ITSAndTypeAndStringLiteral<1 | 2 | 3, number>;
|
|
82
|
+
// type Result = number | "1" | "2" | "3"
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
---
|
|
86
|
+
|
|
87
|
+
### ITSPascalCase
|
|
88
|
+
|
|
89
|
+
**將連字符命名字符串轉換為 PascalCase**
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
type Pascal = ITSPascalCase<'foo-bar'>;
|
|
93
|
+
// type Pascal = "FooBar"
|
|
94
|
+
|
|
95
|
+
type Multiple = ITSPascalCase<'foo-bar-baz'>;
|
|
96
|
+
// type Multiple = "FooBarBaz"
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
### ITSCamelCase
|
|
102
|
+
|
|
103
|
+
**將連字符命名字符串轉換為 camelCase**
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
type Camel = ITSCamelCase<'foo-bar'>;
|
|
107
|
+
// type Camel = "fooBar"
|
|
108
|
+
|
|
109
|
+
type Multiple = ITSCamelCase<'foo-bar-baz'>;
|
|
110
|
+
// type Multiple = "fooBarBaz"
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
### ITSStringInferToNumber
|
|
116
|
+
|
|
117
|
+
**從字串推斷數字類型**
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
type Num = ITSStringInferToNumber<'123'>;
|
|
121
|
+
// type Num = 123
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
### ITSStringInferToBoolean
|
|
127
|
+
|
|
128
|
+
**從字串推斷布林類型**
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
type Bool = ITSStringInferToBoolean<'true'>;
|
|
132
|
+
// type Bool = true
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
### ITSStringInferToNull
|
|
138
|
+
|
|
139
|
+
**從字串推斷 null 類型**
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
type Null = ITSStringInferToNull<'null'>;
|
|
143
|
+
// type Null = null
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
---
|
|
147
|
+
|
|
148
|
+
### ITSStringInferToUndefined
|
|
149
|
+
|
|
150
|
+
**從字串推斷 undefined 類型**
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
type Undefined = ITSStringInferToUndefined<'undefined'>;
|
|
154
|
+
// type Undefined = undefined
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
## 數字類型工具 / Number Type Utilities
|
|
160
|
+
|
|
161
|
+
### ITSNumberString
|
|
162
|
+
|
|
163
|
+
**將數字轉換為字串字面量類型**
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
type NumStr = ITSNumberString<42>;
|
|
167
|
+
// type NumStr = "42"
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
### ITSUnpackNumberString
|
|
173
|
+
|
|
174
|
+
**從字串中解包出數字類型**
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
type Test = ITSNumberString<123>;
|
|
178
|
+
type Result = ITSUnpackNumberString<Test>;
|
|
179
|
+
// type Result = 123
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
---
|
|
183
|
+
|
|
184
|
+
### ITSNumberValue
|
|
185
|
+
|
|
186
|
+
**數字值類型:數字或字串**
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
type NumVal = ITSNumberValue;
|
|
190
|
+
// type NumVal = number | string
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
---
|
|
194
|
+
|
|
195
|
+
### ITSNumberValue2
|
|
196
|
+
|
|
197
|
+
**數字值類型2:數字或數字字串**
|
|
198
|
+
|
|
199
|
+
```typescript
|
|
200
|
+
type NumVal2 = ITSNumberValue2;
|
|
201
|
+
// type NumVal2 = number | "42" (字面量)
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
---
|
|
205
|
+
|
|
206
|
+
## 使用範例 / Usage Examples
|
|
207
|
+
|
|
208
|
+
### 字符串大小寫轉換
|
|
209
|
+
|
|
210
|
+
```typescript
|
|
211
|
+
import { ITSPascalCase, ITSCamelCase } from 'ts-type';
|
|
212
|
+
|
|
213
|
+
// 轉為 PascalCase
|
|
214
|
+
type ComponentName = ITSPascalCase<'my-component'>;
|
|
215
|
+
// type ComponentName = "MyComponent"
|
|
216
|
+
|
|
217
|
+
// 轉為 camelCase
|
|
218
|
+
type variableName = ITSCamelCase<'my-variable'>;
|
|
219
|
+
// type variableName = "myVariable"
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### 字串推斷類型
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
import { ITSStringInferToNumber, ITSStringInferToBoolean } from 'ts-type';
|
|
226
|
+
|
|
227
|
+
// 從字面量推斷數字
|
|
228
|
+
type Age = ITSStringInferToNumber<'25'>;
|
|
229
|
+
// type Age = 25
|
|
230
|
+
|
|
231
|
+
// 從字面量推斷布林
|
|
232
|
+
type IsActive = ITSStringInferToBoolean<'true'>;
|
|
233
|
+
// type IsActive = true
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
### 數字與字串互轉
|
|
237
|
+
|
|
238
|
+
```typescript
|
|
239
|
+
import { ITSNumberString, ITSUnpackNumberString } from 'ts-type';
|
|
240
|
+
|
|
241
|
+
// 數字轉字串
|
|
242
|
+
type StrNum = ITSNumberString<100>;
|
|
243
|
+
// type StrNum = "100"
|
|
244
|
+
|
|
245
|
+
// 字串轉數字
|
|
246
|
+
type Num = ITSUnpackNumberString<'42'>;
|
|
247
|
+
// type Num = 42
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### 應用在 String Literal Union
|
|
251
|
+
|
|
252
|
+
```typescript
|
|
253
|
+
import { ITSTypeAndStringLiteral } from 'ts-type';
|
|
254
|
+
|
|
255
|
+
// 定義 string literal union
|
|
256
|
+
type OrderStatus = 'pending' | 'processing' | 'shipped' | 'delivered' | 'cancelled';
|
|
257
|
+
|
|
258
|
+
// 可以接受 string literal 或 string 類型
|
|
259
|
+
type IOrderStatus = ITSTypeAndStringLiteral<OrderStatus>;
|
|
260
|
+
|
|
261
|
+
// 有效
|
|
262
|
+
const status1: IOrderStatus = 'pending';
|
|
263
|
+
const status2: IOrderStatus = 'processing';
|
|
264
|
+
const status3: IOrderStatus = 'any-string-value'; // 接受任意字串
|
|
265
|
+
|
|
266
|
+
// 這是有效的,因為 string 是基底類型
|
|
267
|
+
function updateStatus(status: IOrderStatus): void {
|
|
268
|
+
console.log(`Status: ${status}`);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
updateStatus('delivered');
|
|
272
|
+
updateStatus('random-status'); // 合法
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
### 應用在 Enum
|
|
276
|
+
|
|
277
|
+
```typescript
|
|
278
|
+
import { ITSTypeAndStringLiteral } from 'ts-type';
|
|
279
|
+
|
|
280
|
+
enum EnumPackageManager {
|
|
281
|
+
yarn = 'yarn',
|
|
282
|
+
npm = 'npm',
|
|
283
|
+
pnpm = 'pnpm',
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// 可以接受 enum 的字面量或基底類型
|
|
287
|
+
type IPackageManager = ITSTypeAndStringLiteral<EnumPackageManager>;
|
|
288
|
+
|
|
289
|
+
// 有效
|
|
290
|
+
const manager1: IPackageManager = 'yarn';
|
|
291
|
+
const manager2: IPackageManager = 'npm';
|
|
292
|
+
const manager3: IPackageManager = EnumPackageManager.yarn;
|
|
293
|
+
|
|
294
|
+
// 這也是有效的,因為 string 是基底類型
|
|
295
|
+
const manager4: IPackageManager = 'pnpm';
|
|
296
|
+
|
|
297
|
+
// 不會接受其他未在 enum 中定義的字串
|
|
298
|
+
// const invalid: IPackageManager = 'bun'; // Error!
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
### 應用在數字類型
|
|
302
|
+
|
|
303
|
+
```typescript
|
|
304
|
+
import { ITSTypeAndStringStringLiteral, ITSLiteral, ITSAndAndTypeAndStringLiteral } from 'ts-type';
|
|
305
|
+
|
|
306
|
+
// 接受數字或數字字串
|
|
307
|
+
type INumber = ITSTypeAndStringLiteral<number>;
|
|
308
|
+
// type INumber = number | `${number}`
|
|
309
|
+
|
|
310
|
+
const num1: INumber = 42;
|
|
311
|
+
const num2: INumber = '42';
|
|
312
|
+
|
|
313
|
+
// 數字字面量聯合
|
|
314
|
+
type Result1 = ITSAndStringLiteral<1 | 2 | 3, number>;
|
|
315
|
+
// type Result1 = number | "1" | "2" | "3"
|
|
316
|
+
|
|
317
|
+
const val1: Result1 = 1;
|
|
318
|
+
const val2: Result1 = '2';
|
|
319
|
+
|
|
320
|
+
type Result2 = ITSAndTypeAndStringLiteral<1 | 2 | 3, number>;
|
|
321
|
+
// type Result2 = number | "1" | "2" | "3"
|
|
322
|
+
|
|
323
|
+
const val3: Result2 = 3;
|
|
324
|
+
const val4: Result2 = '1';
|
|
325
|
+
```
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# 邏輯類型工具 / Logic Type Utilities
|
|
2
|
+
|
|
3
|
+
## 概述 / Overview
|
|
4
|
+
|
|
5
|
+
提供類型層級的邏輯判斷工具,包括檢測 any、never、union 等特殊類型。
|
|
6
|
+
|
|
7
|
+
Provides type-level logic detection utilities, including detecting special types like any, never, union, and more.
|
|
8
|
+
|
|
9
|
+
## 來源 / Source
|
|
10
|
+
|
|
11
|
+
- [`lib/logic/any.ts`](../lib/logic/any.ts)
|
|
12
|
+
- [`lib/logic/never.ts`](../lib/logic/never.ts)
|
|
13
|
+
- [`lib/logic/union/index.ts`](../lib/logic/union/index.ts)
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## 類型列表 / Type List
|
|
18
|
+
|
|
19
|
+
### ITSLogicIsAny
|
|
20
|
+
|
|
21
|
+
**檢測類型是否為 any**
|
|
22
|
+
|
|
23
|
+
利用 any 與任何類型交叉都會得到 any 的特性來檢測。
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
type Test1 = ITSLogicIsAny<any>;
|
|
27
|
+
// type Test1 = true
|
|
28
|
+
|
|
29
|
+
type Test2 = ITSLogicIsAny<string>;
|
|
30
|
+
// type Test2 = false
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
### ITSLogicNotAny
|
|
36
|
+
|
|
37
|
+
**檢測類型是否不為 any**
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
type Test1 = ITSLogicNotAny<any>;
|
|
41
|
+
// type Test1 = false
|
|
42
|
+
|
|
43
|
+
type Test2 = ITSLogicNotAny<string>;
|
|
44
|
+
// type Test2 = true
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
### ITSLogicIsNever
|
|
50
|
+
|
|
51
|
+
**檢測類型是否為 never**
|
|
52
|
+
|
|
53
|
+
利用 never 是所有類型的子類型特性來檢測。
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
type Test1 = ITSLogicIsNever<never>;
|
|
57
|
+
// type Test1 = true
|
|
58
|
+
|
|
59
|
+
type Test2 = ITSLogicIsNever<string>;
|
|
60
|
+
// type Test2 = false
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
### ITSLogicIsUnion
|
|
66
|
+
|
|
67
|
+
**檢測類型是否為聯合類型**
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
type Test1 = ITSLogicIsUnion<string | number>;
|
|
71
|
+
// type Test1 = true
|
|
72
|
+
|
|
73
|
+
type Test2 = ITSLogicIsUnion<string>;
|
|
74
|
+
// type Test2 = false
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
### ITSLogicIsSingleNonUnion
|
|
80
|
+
|
|
81
|
+
**檢測類型是否為單一非聯合類型**
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
type Test1 = ITSLogicIsSingleNonUnion01<string>;
|
|
85
|
+
// type Test1 = true
|
|
86
|
+
|
|
87
|
+
type Test2 = ITSLogicIsSingleNonUnion01<string | number>;
|
|
88
|
+
// type Test2 = false
|
|
89
|
+
|
|
90
|
+
type Test3 = ITSLogicIsSingleNonUnion01<any>;
|
|
91
|
+
// type Test3 = false
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## 使用範例 / Usage Examples
|
|
97
|
+
|
|
98
|
+
### 檢測 Any 類型
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
import { ITSLogicIsAny, ITSLogicNotAny } from 'ts-type';
|
|
102
|
+
|
|
103
|
+
// 檢測是否為 any
|
|
104
|
+
function isAny<T>(value: T): ITSLogicIsAny<T> {
|
|
105
|
+
return true as any;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const anyResult = isAny('test'); // true
|
|
109
|
+
const stringResult = isAny('hello'); // false
|
|
110
|
+
|
|
111
|
+
// 檢測是否不為 any
|
|
112
|
+
function isNotAny<T>(value: T): ITSLogicNotAny<T> {
|
|
113
|
+
return true as any;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const notAnyResult = isNotAny('test'); // true
|
|
117
|
+
const anyCheck = isNotAny<any>('test'); // false
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### 條件類型中的應用
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
import { ITSLogicIsAny } from 'ts-type';
|
|
124
|
+
|
|
125
|
+
type SafeReturn<T> = ITSLogicIsAny<T> extends true
|
|
126
|
+
? unknown
|
|
127
|
+
: T;
|
|
128
|
+
|
|
129
|
+
type Result1 = SafeReturn<any>; // unknown
|
|
130
|
+
type Result2 = SafeReturn<string>; // string
|
|
131
|
+
type Result3 = SafeReturn<number>; // number
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### 防止 Any 類型
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
import { ITSLogicIsAny } from 'ts-type';
|
|
138
|
+
|
|
139
|
+
function strictFunction<T extends string | number>(value: T): T {
|
|
140
|
+
// 如果傳入 any,則回傳 never,防止錯誤使用
|
|
141
|
+
type Check = ITSLogicIsAny<T>;
|
|
142
|
+
if (Check extends true) {
|
|
143
|
+
throw new Error('any type is not allowed');
|
|
144
|
+
}
|
|
145
|
+
return value;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// 正常工作
|
|
149
|
+
strictFunction('hello'); // "hello"
|
|
150
|
+
strictFunction(42); // 42
|
|
151
|
+
|
|
152
|
+
// 編譯錯誤
|
|
153
|
+
// strictFunction(anyValue); // Error!
|
|
154
|
+
```
|
package/docs/README.md
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# ts-type 文件索引 / ts-type Documentation Index
|
|
2
|
+
|
|
3
|
+
## 概述 / Overview
|
|
4
|
+
|
|
5
|
+
ts-type 是一個 TypeScript 類型工具庫,提供豐富的類型操作工具和重新導出的內建類型。
|
|
6
|
+
|
|
7
|
+
ts-type is a TypeScript type utility library that provides rich type manipulation utilities and re-exported built-in types.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## 目錄 / Table of Contents
|
|
12
|
+
|
|
13
|
+
### 1. [基礎類型 / Base Types](01-base-types.md)
|
|
14
|
+
|
|
15
|
+
包含常用的基礎類型別名和工具類型,是整個庫的建構基礎。
|
|
16
|
+
|
|
17
|
+
- `ITSArrayListMaybeReadonly` - 陣列類型
|
|
18
|
+
- `ITSKeys` - 鍵的類型
|
|
19
|
+
- `ITSConstructorLike` - 建構函數類型
|
|
20
|
+
- `ITSValueOrArray` - 值或陣列
|
|
21
|
+
- `ITSAnyFunction` - 任意函數類型
|
|
22
|
+
- `ITSBasicPrimitive` - 基本原始類型
|
|
23
|
+
- `ITSTypeFunction` - 類型函數
|
|
24
|
+
- `ITSMapLike` / `ITSSetLike` - 類似 Map/Set 的介面
|
|
25
|
+
- `ITSIterator` / `ITSIteratorResult` - 迭代器類型
|
|
26
|
+
- `ITSProxify` - Proxy 類型
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
### 2. [內建類型 / Built-in Types](02-built-in-types.md)
|
|
31
|
+
|
|
32
|
+
重新導出 TypeScript 內建類型,並提供包裝類型以增強 IDE 識別能力。
|
|
33
|
+
|
|
34
|
+
- `ITSParameters` - 函數參數類型
|
|
35
|
+
- `ITSConstructorParameters` - 建構函數參數類型
|
|
36
|
+
- `ITSPartial` / `ITSPick` - 類型操作
|
|
37
|
+
- `ITSInstanceType` - 實例類型
|
|
38
|
+
- `ITSClassDecorator` / `ITSPropertyDecorator` - 裝飾器類型
|
|
39
|
+
- `ITSNonNullable` - 排除 null/undefined
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
### 3. [函數類型工具 / Function Type Utilities](03-function-types.md)
|
|
44
|
+
|
|
45
|
+
提供函數類型的操作工具。
|
|
46
|
+
|
|
47
|
+
- `ITSOverwriteReturnType` - 覆寫返回值類型
|
|
48
|
+
- `ITSWrapFunctionPromise` - 包裝為 Promise
|
|
49
|
+
- `ITSOverwriteThisFunction` - 覆寫 this 類型
|
|
50
|
+
- `ITSUnionToIntersection` - 聯集轉交集
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
### 4. [Promise 類型工具 / Promise Type Utilities](04-promise-types.md)
|
|
55
|
+
|
|
56
|
+
提供 Promise 相關的類型操作工具。
|
|
57
|
+
|
|
58
|
+
- `ITSAwaitedReturnType` - 解析 Promise 返回類型
|
|
59
|
+
- `ITSDeferred` - 延遲物件類型
|
|
60
|
+
- `ITSPromiseFulfilledResult` - 解決結果
|
|
61
|
+
- `ITSPromiseRejectedResult` - 拒絕結果
|
|
62
|
+
- `ITSPromiseSettledResult` - Settled 結果
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
### 5. [陣列與元組類型工具 / Array & Tuple Type Utilities](05-array-tuple-types.md)
|
|
67
|
+
|
|
68
|
+
提供陣列和元組類型的操作工具。
|
|
69
|
+
|
|
70
|
+
- `ITSValueOfArray` - 陣列元素類型
|
|
71
|
+
- `ITSToReadonlyArray` / `ITSToWriteableArray` - 陣列互轉
|
|
72
|
+
- `ITSTupleKeys` - 元組索引鍵
|
|
73
|
+
- `ITSEmptyTuple` - 空元組
|
|
74
|
+
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
### 6. [物件與記錄類型工具 / Object & Record Type Utilities](06-object-record-types.md)
|
|
78
|
+
|
|
79
|
+
提供物件類型的選擇、覆寫、合併等操作。
|
|
80
|
+
|
|
81
|
+
- `ITSOverwrite` - 覆寫屬性
|
|
82
|
+
- `ITSMergeBoth` - 合併物件
|
|
83
|
+
- `ITSRequiredPick` / `ITSPartialPick` - 選擇並設定必填/可選
|
|
84
|
+
- `ITSRequireAtLeastOne` / `ITSRequireOnlyOne` - 互斥鍵
|
|
85
|
+
- `ITSReadonlyPartial` / `ITSWriteable` - 唯讀/可寫屬性
|
|
86
|
+
- `ITSOmitIndexSignatures` - 移除索引簽名
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
### 7. [字串與數字類型工具 / String & Number Type Utilities](07-string-number-types.md)
|
|
91
|
+
|
|
92
|
+
提供字串和數字類型的操作工具。
|
|
93
|
+
|
|
94
|
+
- `ITSToStringLiteral` - 轉為字面量類型
|
|
95
|
+
- `ITSTypeAndStringLiteral` - 原始類型與字面量聯合
|
|
96
|
+
- `ITSPascalCase` / `ITSCamelCase` - 大小寫轉換
|
|
97
|
+
- `ITSStringInferToNumber` / `ITSStringInferToBoolean` - 字串推斷類型
|
|
98
|
+
- `ITSNumberString` - 數字轉字串
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
### 8. [邏輯類型工具 / Logic Type Utilities](08-logic-types.md)
|
|
103
|
+
|
|
104
|
+
提供類型層級的邏輯判斷工具。
|
|
105
|
+
|
|
106
|
+
- `ITSLogicIsAny` - 檢測 any 類型
|
|
107
|
+
- `ITSLogicNotAny` - 檢測非 any 類型
|
|
108
|
+
- `ITSLogicIsNever` - 檢測 never 類型
|
|
109
|
+
- `ITSLogicIsUnion` - 檢測聯合類型
|
|
110
|
+
- `ITSLogicIsSingleNonUnion` - 檢測單一非聯合類型
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
## 快速開始 / Quick Start
|
|
115
|
+
|
|
116
|
+
### 安裝 / Installation
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
# npm
|
|
120
|
+
npm install ts-type
|
|
121
|
+
|
|
122
|
+
# yarn
|
|
123
|
+
yarn add ts-type
|
|
124
|
+
|
|
125
|
+
# pnpm
|
|
126
|
+
pnpm add ts-type
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### 使用 / Usage
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
import {
|
|
133
|
+
ITSOverwrite,
|
|
134
|
+
ITSRequiredPick,
|
|
135
|
+
ITSValueOfArray
|
|
136
|
+
} from 'ts-type';
|
|
137
|
+
|
|
138
|
+
// 覆寫屬性
|
|
139
|
+
type UpdatedConfig = ITSOverwrite<{ host: string; port: number }, { port: string }>;
|
|
140
|
+
|
|
141
|
+
// 選擇必填鍵
|
|
142
|
+
type RequiredUser = ITSRequiredPick<User, 'id'>;
|
|
143
|
+
|
|
144
|
+
// 提取陣列元素類型
|
|
145
|
+
type Element = ITSValueOfArray<string[]>;
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
## 相關資源 / Related Resources
|
|
151
|
+
|
|
152
|
+
- [TypeScript 官方文檔](https://www.typescriptlang.org/docs/)
|
|
153
|
+
- [ts-toolbelt](https://github.com/millsp/ts-toolbelt)
|
|
154
|
+
- [type-challenges](https://github.com/type-challenges/type-challenges)
|
|
155
|
+
- [ts-essentials](https://github.com/krzkaczor/ts-essentials)
|
package/lib/_build-in.d.ts
CHANGED
|
@@ -1,18 +1,72 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* Created by user on 2019/6/11.
|
|
3
|
+
*
|
|
4
|
+
* 重新導出內建類型
|
|
5
|
+
* Re-export built-in types
|
|
6
|
+
*
|
|
7
|
+
* 在某些情況下 IDE 無法正確識別類型,此文件提供統一的類別導出
|
|
8
|
+
* In some cases IDE cannot correctly identify types, this file provides unified type exports
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* 取得函數的參數類型
|
|
12
|
+
* Get the parameter types of a function
|
|
13
|
+
*
|
|
14
|
+
* @see Parameters
|
|
4
15
|
*/
|
|
5
16
|
export type ITSParameters<T extends (...args: any[]) => any> = Parameters<T>;
|
|
17
|
+
/**
|
|
18
|
+
* 取得建構函數的參數類型
|
|
19
|
+
* Get the constructor parameter types
|
|
20
|
+
*
|
|
21
|
+
* @see ConstructorParameters
|
|
22
|
+
*/
|
|
6
23
|
export type ITSConstructorParameters<T extends new (...args: any[]) => any> = ConstructorParameters<T>;
|
|
24
|
+
/**
|
|
25
|
+
* 將類型的所有屬性設為可選
|
|
26
|
+
* Make all properties of T optional
|
|
27
|
+
*
|
|
28
|
+
* @see Partial
|
|
29
|
+
*/
|
|
7
30
|
export type ITSPartial<T> = Partial<T>;
|
|
31
|
+
/**
|
|
32
|
+
* 從類型 T 中選取指定的屬性 K
|
|
33
|
+
* Pick a set of properties K from T
|
|
34
|
+
*
|
|
35
|
+
* @see Pick
|
|
36
|
+
*/
|
|
8
37
|
export type ITSPick<T, K extends keyof T = keyof T> = Pick<T, K>;
|
|
38
|
+
/**
|
|
39
|
+
* 取得建構函數實例的類型
|
|
40
|
+
* Get the instance type created by a constructor
|
|
41
|
+
*
|
|
42
|
+
* @see InstanceType
|
|
43
|
+
*/
|
|
9
44
|
export type ITSInstanceType<T extends new (...args: any[]) => any> = InstanceType<T>;
|
|
45
|
+
/**
|
|
46
|
+
* 類別裝飾器類型
|
|
47
|
+
* Class decorator type
|
|
48
|
+
*/
|
|
10
49
|
export type ITSClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void;
|
|
50
|
+
/**
|
|
51
|
+
* 屬性裝飾器類型
|
|
52
|
+
* Property decorator type
|
|
53
|
+
*/
|
|
11
54
|
export type ITSPropertyDecorator = (target: object, propertyKey: string | symbol) => void;
|
|
55
|
+
/**
|
|
56
|
+
* 方法裝飾器類型
|
|
57
|
+
* Method decorator type
|
|
58
|
+
*/
|
|
12
59
|
export type ITSMethodDecorator = <T>(target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
|
|
60
|
+
/**
|
|
61
|
+
* 參數裝飾器類型
|
|
62
|
+
* Parameter decorator type
|
|
63
|
+
*/
|
|
13
64
|
export type ITSParameterDecorator = (target: object, propertyKey: string | symbol, parameterIndex: number) => void;
|
|
14
65
|
/**
|
|
66
|
+
* 從 T 中排除 null 和 undefined
|
|
15
67
|
* Exclude null and undefined from T
|
|
68
|
+
*
|
|
69
|
+
* @see NonNullable
|
|
16
70
|
* @see https://devblogs.microsoft.com/typescript/announcing-typescript-4-8/#improved-intersection-reduction-union-compatibility-and-narrowing
|
|
17
71
|
*/
|
|
18
72
|
export type ITSNonNullable<T> = NonNullable<T>;
|