ts-type 3.0.8 → 3.0.12
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 +52 -0
- package/docs/10-same-logic-diff-impl.md +1150 -0
- package/docs/README.md +11 -0
- package/lib/_build-in.d.ts +64 -0
- package/lib/_build-in.js.map +1 -1
- package/lib/generic.d.ts +8 -0
- package/lib/generic.js.map +1 -1
- package/lib/helper/record/pick-one.d.ts +7 -0
- package/lib/helper/record/pick-one.js.map +1 -1
- package/lib/type/base.d.ts +14 -0
- package/lib/type/base.js.map +1 -1
- package/lib/type/record.d.ts +22 -1
- package/lib/type/record.js.map +1 -1
- package/package.json +2 -2
package/docs/README.md
CHANGED
|
@@ -111,6 +111,17 @@ ts-type is a TypeScript type utility library that provides rich type manipulatio
|
|
|
111
111
|
|
|
112
112
|
---
|
|
113
113
|
|
|
114
|
+
### 9. [相同邏輯的不同實現 / Different Implementations of Same Logic](10-same-logic-diff-impl.md)
|
|
115
|
+
|
|
116
|
+
記錄功能相似但實現方式不同的類型,幫助選擇適合的類型。
|
|
117
|
+
|
|
118
|
+
- 互斥鍵: `ITSPickOne` vs `ITSRequireOnlyOne`
|
|
119
|
+
- 唯讀操作: 類型級別 vs 記錄級別
|
|
120
|
+
- 空類型: 記錄 vs 元組
|
|
121
|
+
- 列舉操作: 類型定義 vs 操作工具
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
114
125
|
## 快速開始 / Quick Start
|
|
115
126
|
|
|
116
127
|
### 安裝 / Installation
|
package/lib/_build-in.d.ts
CHANGED
|
@@ -11,6 +11,13 @@
|
|
|
11
11
|
* 取得函數的參數類型
|
|
12
12
|
* Get the parameter types of a function
|
|
13
13
|
*
|
|
14
|
+
* @example
|
|
15
|
+
* function greet(name: string, age: number): string {
|
|
16
|
+
* return `Hello, ${name}, you are ${age} years old.`;
|
|
17
|
+
* }
|
|
18
|
+
* type Params = ITSParameters<typeof greet>;
|
|
19
|
+
* // type Params = [name: string, age: number]
|
|
20
|
+
*
|
|
14
21
|
* @see Parameters
|
|
15
22
|
*/
|
|
16
23
|
export type ITSParameters<T extends (...args: any[]) => any> = Parameters<T>;
|
|
@@ -18,6 +25,13 @@ export type ITSParameters<T extends (...args: any[]) => any> = Parameters<T>;
|
|
|
18
25
|
* 取得建構函數的參數類型
|
|
19
26
|
* Get the constructor parameter types
|
|
20
27
|
*
|
|
28
|
+
* @example
|
|
29
|
+
* class User {
|
|
30
|
+
* constructor(public name: string, public age: number) {}
|
|
31
|
+
* }
|
|
32
|
+
* type CtorParams = ITSConstructorParameters<typeof User>;
|
|
33
|
+
* // type CtorParams = [name: string, age: number]
|
|
34
|
+
*
|
|
21
35
|
* @see ConstructorParameters
|
|
22
36
|
*/
|
|
23
37
|
export type ITSConstructorParameters<T extends new (...args: any[]) => any> = ConstructorParameters<T>;
|
|
@@ -25,6 +39,11 @@ export type ITSConstructorParameters<T extends new (...args: any[]) => any> = Co
|
|
|
25
39
|
* 將類型的所有屬性設為可選
|
|
26
40
|
* Make all properties of T optional
|
|
27
41
|
*
|
|
42
|
+
* @example
|
|
43
|
+
* interface User { name: string; age: number; }
|
|
44
|
+
* type PartialUser = ITSPartial<User>;
|
|
45
|
+
* // type PartialUser = { name?: string; age?: number; }
|
|
46
|
+
*
|
|
28
47
|
* @see Partial
|
|
29
48
|
*/
|
|
30
49
|
export type ITSPartial<T> = Partial<T>;
|
|
@@ -32,6 +51,11 @@ export type ITSPartial<T> = Partial<T>;
|
|
|
32
51
|
* 從類型 T 中選取指定的屬性 K
|
|
33
52
|
* Pick a set of properties K from T
|
|
34
53
|
*
|
|
54
|
+
* @example
|
|
55
|
+
* interface User { name: string; age: number; email: string; }
|
|
56
|
+
* type PickedUser = ITSPick<User, 'name' | 'age'>;
|
|
57
|
+
* // type PickedUser = { name: string; age: number; }
|
|
58
|
+
*
|
|
35
59
|
* @see Pick
|
|
36
60
|
*/
|
|
37
61
|
export type ITSPick<T, K extends keyof T = keyof T> = Pick<T, K>;
|
|
@@ -39,33 +63,73 @@ export type ITSPick<T, K extends keyof T = keyof T> = Pick<T, K>;
|
|
|
39
63
|
* 取得建構函數實例的類型
|
|
40
64
|
* Get the instance type created by a constructor
|
|
41
65
|
*
|
|
66
|
+
* @example
|
|
67
|
+
* class User { name: string; }
|
|
68
|
+
* type UserInstance = ITSInstanceType<typeof User>;
|
|
69
|
+
* // type UserInstance = User
|
|
70
|
+
*
|
|
42
71
|
* @see InstanceType
|
|
43
72
|
*/
|
|
44
73
|
export type ITSInstanceType<T extends new (...args: any[]) => any> = InstanceType<T>;
|
|
45
74
|
/**
|
|
46
75
|
* 類別裝飾器類型
|
|
47
76
|
* Class decorator type
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* const myClassDecorator: ITSClassDecorator = (target) => {
|
|
80
|
+
* console.log(`Decorating ${target.name}`);
|
|
81
|
+
* return target;
|
|
82
|
+
* };
|
|
83
|
+
*
|
|
84
|
+
* @decorator
|
|
48
85
|
*/
|
|
49
86
|
export type ITSClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void;
|
|
50
87
|
/**
|
|
51
88
|
* 屬性裝飾器類型
|
|
52
89
|
* Property decorator type
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* const myPropertyDecorator: ITSPropertyDecorator = (target, propertyKey) => {
|
|
93
|
+
* console.log(`Decorating property ${String(propertyKey)} on`, target);
|
|
94
|
+
* };
|
|
95
|
+
*
|
|
96
|
+
* @decorator
|
|
53
97
|
*/
|
|
54
98
|
export type ITSPropertyDecorator = (target: object, propertyKey: string | symbol) => void;
|
|
55
99
|
/**
|
|
56
100
|
* 方法裝飾器類型
|
|
57
101
|
* Method decorator type
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* const myMethodDecorator: ITSMethodDecorator = (target, propertyKey, descriptor) => {
|
|
105
|
+
* console.log(`Decorating method ${String(propertyKey)}`);
|
|
106
|
+
* return descriptor;
|
|
107
|
+
* };
|
|
108
|
+
*
|
|
109
|
+
* @decorator
|
|
58
110
|
*/
|
|
59
111
|
export type ITSMethodDecorator = <T>(target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
|
|
60
112
|
/**
|
|
61
113
|
* 參數裝飾器類型
|
|
62
114
|
* Parameter decorator type
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* const myParameterDecorator: ITSParameterDecorator = (target, propertyKey, parameterIndex) => {
|
|
118
|
+
* console.log(`Decorating parameter ${parameterIndex} of ${String(propertyKey)}`);
|
|
119
|
+
* };
|
|
120
|
+
*
|
|
121
|
+
* @decorator
|
|
63
122
|
*/
|
|
64
123
|
export type ITSParameterDecorator = (target: object, propertyKey: string | symbol, parameterIndex: number) => void;
|
|
65
124
|
/**
|
|
66
125
|
* 從 T 中排除 null 和 undefined
|
|
67
126
|
* Exclude null and undefined from T
|
|
68
127
|
*
|
|
128
|
+
* @example
|
|
129
|
+
* type Test = string | number | null | undefined;
|
|
130
|
+
* type NonNull = ITSNonNullable<Test>;
|
|
131
|
+
* // type NonNull = string | number
|
|
132
|
+
*
|
|
69
133
|
* @see NonNullable
|
|
70
134
|
* @see https://devblogs.microsoft.com/typescript/announcing-typescript-4-8/#improved-intersection-reduction-union-compatibility-and-narrowing
|
|
71
135
|
*/
|
package/lib/_build-in.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_build-in.js","sourceRoot":"","sources":["_build-in.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG","sourcesContent":["/**\n * Created by user on 2019/6/11.\n * \n * 重新導出內建類型\n * Re-export built-in types\n * \n * 在某些情況下 IDE 無法正確識別類型,此文件提供統一的類別導出\n * In some cases IDE cannot correctly identify types, this file provides unified type exports\n */\n\n/**\n * 取得函數的參數類型\n * Get the parameter types of a function\n * \n * @see Parameters\n */\nexport type ITSParameters<T extends (...args: any[]) => any> = Parameters<T>\n/**\n * 取得建構函數的參數類型\n * Get the constructor parameter types\n * \n * @see ConstructorParameters\n */\nexport type ITSConstructorParameters<T extends new (...args: any[]) => any> = ConstructorParameters<T>\n/**\n * 將類型的所有屬性設為可選\n * Make all properties of T optional\n * \n * @see Partial\n */\nexport type ITSPartial<T> = Partial<T>\n/**\n * 從類型 T 中選取指定的屬性 K\n * Pick a set of properties K from T\n * \n * @see Pick\n */\nexport type ITSPick<T, K extends keyof T = keyof T> = Pick<T, K>\n/**\n * 取得建構函數實例的類型\n * Get the instance type created by a constructor\n * \n * @see InstanceType\n */\nexport type ITSInstanceType<T extends new (...args: any[]) => any> = InstanceType<T>\n\n\n/**\n * 類別裝飾器類型\n * Class decorator type\n */\nexport type ITSClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void;\n/**\n * 屬性裝飾器類型\n * Property decorator type\n */\nexport type ITSPropertyDecorator = (target: object, propertyKey: string | symbol) => void;\n/**\n * 方法裝飾器類型\n * Method decorator type\n */\nexport type ITSMethodDecorator = <T>(target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;\n/**\n * 參數裝飾器類型\n * Parameter decorator type\n */\nexport type ITSParameterDecorator = (target: object, propertyKey: string | symbol, parameterIndex: number) => void;\n\n/**\n * 從 T 中排除 null 和 undefined\n * Exclude null and undefined from T\n * \n * @see NonNullable\n * @see https://devblogs.microsoft.com/typescript/announcing-typescript-4-8/#improved-intersection-reduction-union-compatibility-and-narrowing\n */\nexport type ITSNonNullable<T> = NonNullable<T>;\n"]}
|
|
1
|
+
{"version":3,"file":"_build-in.js","sourceRoot":"","sources":["_build-in.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG","sourcesContent":["/**\n * Created by user on 2019/6/11.\n * \n * 重新導出內建類型\n * Re-export built-in types\n * \n * 在某些情況下 IDE 無法正確識別類型,此文件提供統一的類別導出\n * In some cases IDE cannot correctly identify types, this file provides unified type exports\n */\n\n/**\n * 取得函數的參數類型\n * Get the parameter types of a function\n * \n * @example\n * function greet(name: string, age: number): string {\n * return `Hello, ${name}, you are ${age} years old.`;\n * }\n * type Params = ITSParameters<typeof greet>;\n * // type Params = [name: string, age: number]\n * \n * @see Parameters\n */\nexport type ITSParameters<T extends (...args: any[]) => any> = Parameters<T>\n/**\n * 取得建構函數的參數類型\n * Get the constructor parameter types\n * \n * @example\n * class User {\n * constructor(public name: string, public age: number) {}\n * }\n * type CtorParams = ITSConstructorParameters<typeof User>;\n * // type CtorParams = [name: string, age: number]\n * \n * @see ConstructorParameters\n */\nexport type ITSConstructorParameters<T extends new (...args: any[]) => any> = ConstructorParameters<T>\n/**\n * 將類型的所有屬性設為可選\n * Make all properties of T optional\n * \n * @example\n * interface User { name: string; age: number; }\n * type PartialUser = ITSPartial<User>;\n * // type PartialUser = { name?: string; age?: number; }\n * \n * @see Partial\n */\nexport type ITSPartial<T> = Partial<T>\n/**\n * 從類型 T 中選取指定的屬性 K\n * Pick a set of properties K from T\n * \n * @example\n * interface User { name: string; age: number; email: string; }\n * type PickedUser = ITSPick<User, 'name' | 'age'>;\n * // type PickedUser = { name: string; age: number; }\n * \n * @see Pick\n */\nexport type ITSPick<T, K extends keyof T = keyof T> = Pick<T, K>\n/**\n * 取得建構函數實例的類型\n * Get the instance type created by a constructor\n * \n * @example\n * class User { name: string; }\n * type UserInstance = ITSInstanceType<typeof User>;\n * // type UserInstance = User\n * \n * @see InstanceType\n */\nexport type ITSInstanceType<T extends new (...args: any[]) => any> = InstanceType<T>\n\n\n/**\n * 類別裝飾器類型\n * Class decorator type\n * \n * @example\n * const myClassDecorator: ITSClassDecorator = (target) => {\n * console.log(`Decorating ${target.name}`);\n * return target;\n * };\n * \n * @decorator\n */\nexport type ITSClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void;\n/**\n * 屬性裝飾器類型\n * Property decorator type\n * \n * @example\n * const myPropertyDecorator: ITSPropertyDecorator = (target, propertyKey) => {\n * console.log(`Decorating property ${String(propertyKey)} on`, target);\n * };\n * \n * @decorator\n */\nexport type ITSPropertyDecorator = (target: object, propertyKey: string | symbol) => void;\n/**\n * 方法裝飾器類型\n * Method decorator type\n * \n * @example\n * const myMethodDecorator: ITSMethodDecorator = (target, propertyKey, descriptor) => {\n * console.log(`Decorating method ${String(propertyKey)}`);\n * return descriptor;\n * };\n * \n * @decorator\n */\nexport type ITSMethodDecorator = <T>(target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;\n/**\n * 參數裝飾器類型\n * Parameter decorator type\n * \n * @example\n * const myParameterDecorator: ITSParameterDecorator = (target, propertyKey, parameterIndex) => {\n * console.log(`Decorating parameter ${parameterIndex} of ${String(propertyKey)}`);\n * };\n * \n * @decorator\n */\nexport type ITSParameterDecorator = (target: object, propertyKey: string | symbol, parameterIndex: number) => void;\n\n/**\n * 從 T 中排除 null 和 undefined\n * Exclude null and undefined from T\n * \n * @example\n * type Test = string | number | null | undefined;\n * type NonNull = ITSNonNullable<Test>;\n * // type NonNull = string | number\n * \n * @see NonNullable\n * @see https://devblogs.microsoft.com/typescript/announcing-typescript-4-8/#improved-intersection-reduction-union-compatibility-and-narrowing\n */\nexport type ITSNonNullable<T> = NonNullable<T>;\n"]}
|
package/lib/generic.d.ts
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 類型函數:返回指定類型的函數
|
|
3
3
|
* Type function: a function that returns the specified type
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* type StringFunc = ITSTypeFunction<string>;
|
|
7
|
+
* // type StringFunc = (...args: any[]) => string
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* type NumberFunc = ITSTypeFunction<number>;
|
|
11
|
+
* // type NumberFunc = (...args: any[]) => number
|
|
4
12
|
*/
|
|
5
13
|
export type ITSTypeFunction<T> = (...args: any[]) => T;
|
|
6
14
|
/**
|
package/lib/generic.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generic.js","sourceRoot":"","sources":["generic.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * 類型函數:返回指定類型的函數\n * Type function: a function that returns the specified type\n */\nexport type ITSTypeFunction<T> = (...args: any[]) => T;\n\n/**\n * 類似 Map 的介面定義\n * Map-like interface definition\n */\nexport interface ITSMapLike<K, V>\n{\n\t/**\n\t * 取得指定鍵的值\n\t * Get the value for the specified key\n\t */\n\tget(key: K): V | undefined;\n\n\t/**\n\t * 檢查是否包含指定鍵\n\t * Check if the map contains the specified key\n\t */\n\thas(key: K): boolean;\n}\n\n/**\n * 類似 Set 的介面定義\n * Set-like interface definition\n */\nexport interface ITSSetLike<V>\n{\n\t/**\n\t * 檢查是否包含指定值\n\t * Check if the set contains the specified value\n\t */\n\thas(value: V): boolean;\n}\n\n/**\n * 可解析的類型:支援直接值或 PromiseLike\n * Resolvable type: supports direct value or PromiseLike\n * \n * @see bluebird\n */\nexport type ITSResolvable<R> = R | PromiseLike<R>;\n\n/**\n * 可寫入的類陣列介面\n * Writable array-like interface\n */\nexport interface ITSArrayLikeWriteable<T>\n{\n\t/** 只讀的長度屬性 / Readonly length property */\n\treadonly length: number;\n\t/** 數字索引存取 / Numeric index access */\n\t[n: number]: T;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"generic.js","sourceRoot":"","sources":["generic.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * 類型函數:返回指定類型的函數\n * Type function: a function that returns the specified type\n * \n * @example\n * type StringFunc = ITSTypeFunction<string>;\n * // type StringFunc = (...args: any[]) => string\n * \n * @example\n * type NumberFunc = ITSTypeFunction<number>;\n * // type NumberFunc = (...args: any[]) => number\n */\nexport type ITSTypeFunction<T> = (...args: any[]) => T;\n\n/**\n * 類似 Map 的介面定義\n * Map-like interface definition\n */\nexport interface ITSMapLike<K, V>\n{\n\t/**\n\t * 取得指定鍵的值\n\t * Get the value for the specified key\n\t */\n\tget(key: K): V | undefined;\n\n\t/**\n\t * 檢查是否包含指定鍵\n\t * Check if the map contains the specified key\n\t */\n\thas(key: K): boolean;\n}\n\n/**\n * 類似 Set 的介面定義\n * Set-like interface definition\n */\nexport interface ITSSetLike<V>\n{\n\t/**\n\t * 檢查是否包含指定值\n\t * Check if the set contains the specified value\n\t */\n\thas(value: V): boolean;\n}\n\n/**\n * 可解析的類型:支援直接值或 PromiseLike\n * Resolvable type: supports direct value or PromiseLike\n * \n * @see bluebird\n */\nexport type ITSResolvable<R> = R | PromiseLike<R>;\n\n/**\n * 可寫入的類陣列介面\n * Writable array-like interface\n */\nexport interface ITSArrayLikeWriteable<T>\n{\n\t/** 只讀的長度屬性 / Readonly length property */\n\treadonly length: number;\n\t/** 數字索引存取 / Numeric index access */\n\t[n: number]: T;\n}\n"]}
|
|
@@ -3,6 +3,13 @@ import { ITSPartialRecord } from '../../type/record/partial';
|
|
|
3
3
|
* 在项目开发中,很多时候会遇到一种场景,需要定义一个对象的类型,此类型必须包含某n个字段中的其中一种,且只选一个。
|
|
4
4
|
*
|
|
5
5
|
* @see https://juejin.cn/post/7150316226009382919#heading-2
|
|
6
|
+
* @see {@link ITSRequireOnlyOne} 另一種實現方式
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* interface User { name?: string; age?: number; }
|
|
10
|
+
* type OnlyOne2 = ITSPickOne<User, 'name' | 'age'>;
|
|
11
|
+
* // 輸出結果:
|
|
12
|
+
* // type OnlyOne2 = { name: string; age?: void; } | { age: number; name?: void; }
|
|
6
13
|
*/
|
|
7
14
|
export type ITSPickOne<T, Keys extends keyof T = keyof T> = {
|
|
8
15
|
[K in Keys]: Record<K, T[K]> & ITSPartialRecord<Exclude<keyof T, K>, void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pick-one.js","sourceRoot":"","sources":["pick-one.ts"],"names":[],"mappings":"","sourcesContent":["import { ITSPartialRecord } from '../../type/record/partial';\n\n/**\n * 在项目开发中,很多时候会遇到一种场景,需要定义一个对象的类型,此类型必须包含某n个字段中的其中一种,且只选一个。\n *\n * @see https://juejin.cn/post/7150316226009382919#heading-2\n */\nexport type ITSPickOne<T, Keys extends keyof T = keyof T> = {\n\t[K in Keys]: Record<K, T[K]> & ITSPartialRecord<Exclude<keyof T, K>, void>\n}[Keys]\n"]}
|
|
1
|
+
{"version":3,"file":"pick-one.js","sourceRoot":"","sources":["pick-one.ts"],"names":[],"mappings":"","sourcesContent":["import { ITSPartialRecord } from '../../type/record/partial';\n\n/**\n * 在项目开发中,很多时候会遇到一种场景,需要定义一个对象的类型,此类型必须包含某n个字段中的其中一种,且只选一个。\n *\n * @see https://juejin.cn/post/7150316226009382919#heading-2\n * @see {@link ITSRequireOnlyOne} 另一種實現方式\n *\n * @example\n * interface User { name?: string; age?: number; }\n * type OnlyOne2 = ITSPickOne<User, 'name' | 'age'>;\n * // 輸出結果:\n * // type OnlyOne2 = { name: string; age?: void; } | { age: number; name?: void; }\n */\nexport type ITSPickOne<T, Keys extends keyof T = keyof T> = {\n\t[K in Keys]: Record<K, T[K]> & ITSPartialRecord<Exclude<keyof T, K>, void>\n}[Keys]\n"]}
|
package/lib/type/base.d.ts
CHANGED
|
@@ -60,6 +60,20 @@ export type ITSValueOrArrayMaybeReadonly<T> = T | ITSArrayListMaybeReadonly<T>;
|
|
|
60
60
|
* // type PropKey = string | symbol
|
|
61
61
|
*/
|
|
62
62
|
export type ITSPropertyKey = string | symbol;
|
|
63
|
+
/**
|
|
64
|
+
* 屬性鍵或索引類型:字串、符號或數字
|
|
65
|
+
* Property key or index type: string, symbol, or number
|
|
66
|
+
*
|
|
67
|
+
* 適用於物件屬性存取與陣列索引的聯合類型
|
|
68
|
+
* Suitable for union type covering object property access and array index access
|
|
69
|
+
*
|
|
70
|
+
* @see {@link ITSPropertyKey} - 僅字串或符號(不含數字索引)
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* type KeyOrIdx = ITSPropertyKeyOrIndex;
|
|
74
|
+
* // type KeyOrIdx = string | symbol | number
|
|
75
|
+
*/
|
|
76
|
+
export type ITSPropertyKeyOrIndex = ITSPropertyKey | number;
|
|
63
77
|
/**
|
|
64
78
|
* 任意函數類型
|
|
65
79
|
* Any function type
|
package/lib/type/base.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.js","sourceRoot":"","sources":["base.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG","sourcesContent":["/**\n * 基礎類型定義\n * Base Type Definitions\n * \n * 提供常用的基礎類型別名和工具類型\n * Provides commonly used base type aliases and utility types\n */\n\nimport { ITSTypeFunction } from '../generic';\n\n/**\n * 陣列類型:可讀寫陣列或唯讀陣列\n * Array type: writable array or readonly array\n * \n * @example\n * type StringList = ITSArrayListMaybeReadonly<string>;\n * // type StringList = string[] | readonly string[]\n */\nexport type ITSArrayListMaybeReadonly<T> = T[] | readonly T[];\n\n/**\n * 鍵的類型:符號、字串或數字\n * Key types: symbol, string, or number\n * \n * @example\n * type Keys = ITSKeys;\n * // type Keys = symbol | string | number\n */\nexport type ITSKeys = symbol | string | number;\n\n/**\n * 建構函數類型\n * Constructor type\n * \n * @example\n * type MyConstructor = ITSConstructorLike<MyClass>;\n * // type MyConstructor = new (...args: any) => MyClass\n */\nexport type ITSConstructorLike<T extends any = any> = new(...args: any) => T;\n\n/**\n * 值或陣列:單一值或其陣列\n * Value or array: single value or array of that value\n * \n * @example\n * type StringOrArray = ITSValueOrArray<string>;\n * // type StringOrArray = string | string[]\n */\nexport type ITSValueOrArray<T> = T | T[];\n\n/**\n * 值或可能唯讀的陣列:單一值、可讀寫陣列或唯讀陣列\n * Value or possibly readonly array: single value, writable array, or readonly array\n * \n * @example\n * type StringOrList = ITSValueOrArrayMaybeReadonly<string>;\n * // type StringOrList = string | string[] | readonly string[]\n */\nexport type ITSValueOrArrayMaybeReadonly<T> = T | ITSArrayListMaybeReadonly<T>;\n\n/**\n * 屬性鍵類型:字串或符號\n * Property key type: string or symbol\n * \n * @example\n * type PropKey = ITSPropertyKey;\n * // type PropKey = string | symbol\n */\nexport type ITSPropertyKey = string | symbol;\n\n/**\n * 任意函數類型\n * Any function type\n * \n * @example\n * type AnyFunc = ITSAnyFunction;\n * // type AnyFunc = (...args: any[]) => any\n */\nexport type ITSAnyFunction = ITSTypeFunction<any>;\n\n/**\n * 基本原始類型:數字、字串或布林值\n * Basic primitive types: number, string, or boolean\n * \n * @example\n * type Primitive = ITSBasicPrimitive;\n * // type Primitive = number | string | boolean\n */\nexport type ITSBasicPrimitive = number | string | boolean;\n\n/**\n * 可為空的原始類型:null 或 undefined\n * Nullable primitive types: null or undefined\n * \n * @example\n * type Nullish = ITSNullPrimitive;\n * // type Nullish = null | undefined\n */\nexport type ITSNullPrimitive = null | undefined;\n"]}
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["base.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG","sourcesContent":["/**\n * 基礎類型定義\n * Base Type Definitions\n * \n * 提供常用的基礎類型別名和工具類型\n * Provides commonly used base type aliases and utility types\n */\n\nimport { ITSTypeFunction } from '../generic';\n\n/**\n * 陣列類型:可讀寫陣列或唯讀陣列\n * Array type: writable array or readonly array\n * \n * @example\n * type StringList = ITSArrayListMaybeReadonly<string>;\n * // type StringList = string[] | readonly string[]\n */\nexport type ITSArrayListMaybeReadonly<T> = T[] | readonly T[];\n\n/**\n * 鍵的類型:符號、字串或數字\n * Key types: symbol, string, or number\n * \n * @example\n * type Keys = ITSKeys;\n * // type Keys = symbol | string | number\n */\nexport type ITSKeys = symbol | string | number;\n\n/**\n * 建構函數類型\n * Constructor type\n * \n * @example\n * type MyConstructor = ITSConstructorLike<MyClass>;\n * // type MyConstructor = new (...args: any) => MyClass\n */\nexport type ITSConstructorLike<T extends any = any> = new(...args: any) => T;\n\n/**\n * 值或陣列:單一值或其陣列\n * Value or array: single value or array of that value\n * \n * @example\n * type StringOrArray = ITSValueOrArray<string>;\n * // type StringOrArray = string | string[]\n */\nexport type ITSValueOrArray<T> = T | T[];\n\n/**\n * 值或可能唯讀的陣列:單一值、可讀寫陣列或唯讀陣列\n * Value or possibly readonly array: single value, writable array, or readonly array\n * \n * @example\n * type StringOrList = ITSValueOrArrayMaybeReadonly<string>;\n * // type StringOrList = string | string[] | readonly string[]\n */\nexport type ITSValueOrArrayMaybeReadonly<T> = T | ITSArrayListMaybeReadonly<T>;\n\n/**\n * 屬性鍵類型:字串或符號\n * Property key type: string or symbol\n * \n * @example\n * type PropKey = ITSPropertyKey;\n * // type PropKey = string | symbol\n */\nexport type ITSPropertyKey = string | symbol;\n\n/**\n * 屬性鍵或索引類型:字串、符號或數字\n * Property key or index type: string, symbol, or number\n *\n * 適用於物件屬性存取與陣列索引的聯合類型\n * Suitable for union type covering object property access and array index access\n *\n * @see {@link ITSPropertyKey} - 僅字串或符號(不含數字索引)\n *\n * @example\n * type KeyOrIdx = ITSPropertyKeyOrIndex;\n * // type KeyOrIdx = string | symbol | number\n */\nexport type ITSPropertyKeyOrIndex = ITSPropertyKey | number;\n\n/**\n * 任意函數類型\n * Any function type\n * \n * @example\n * type AnyFunc = ITSAnyFunction;\n * // type AnyFunc = (...args: any[]) => any\n */\nexport type ITSAnyFunction = ITSTypeFunction<any>;\n\n/**\n * 基本原始類型:數字、字串或布林值\n * Basic primitive types: number, string, or boolean\n * \n * @example\n * type Primitive = ITSBasicPrimitive;\n * // type Primitive = number | string | boolean\n */\nexport type ITSBasicPrimitive = number | string | boolean;\n\n/**\n * 可為空的原始類型:null 或 undefined\n * Nullable primitive types: null or undefined\n * \n * @example\n * type Nullish = ITSNullPrimitive;\n * // type Nullish = null | undefined\n */\nexport type ITSNullPrimitive = null | undefined;\n"]}
|
package/lib/type/record.d.ts
CHANGED
|
@@ -159,6 +159,24 @@ export type ITSPickExtra<T, RK extends keyof T, PK extends Exclude<keyof T, RK>
|
|
|
159
159
|
* // type Result = { name?: string; } & { age: number; email: string; }
|
|
160
160
|
*/
|
|
161
161
|
export type ITSPickExtra2<T, PK extends keyof T, RK extends Exclude<keyof T, PK> = Exclude<keyof T, PK>> = ITSRequiredPick<T, RK> & ITSPartialPick<T, PK>;
|
|
162
|
+
/**
|
|
163
|
+
* 保留指定鍵不變(維持原本的必選/可選/唯讀屬性),其他鍵變為可選
|
|
164
|
+
* Keep specified keys unchanged (preserving required/optional/readonly modifiers), other keys become Partial
|
|
165
|
+
*
|
|
166
|
+
* 與 ITSPickExtra 的差異:ITSPickExtra 會將指定鍵強制設為必填,而此類型保留原始修飾符
|
|
167
|
+
* Difference from ITSPickExtra: ITSPickExtra forces specified keys to Required, while this type preserves original modifiers
|
|
168
|
+
*
|
|
169
|
+
* @see {@link ITSPickExtra} - 將指定鍵設為必填,其他鍵可選
|
|
170
|
+
* @see {@link ITSPartialWith} - 將指定鍵設為可選,其他鍵不變
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* interface User { name: string; readonly id: string; age?: number; email: string; }
|
|
174
|
+
* type Result = ITSPickAndPartialOther<User, 'name' | 'id'>;
|
|
175
|
+
* // 輸出結果 / Output:
|
|
176
|
+
* // { name: string; readonly id: string; } & { age?: number; email?: string; }
|
|
177
|
+
* // 注意:name 維持必選,id 維持唯讀且必選,age 和 email 變為可選
|
|
178
|
+
*/
|
|
179
|
+
export type ITSPickAndPartialOther<T, RK extends keyof T, PK extends Exclude<keyof T, RK> = Exclude<keyof T, RK>> = Pick<T, RK> & ITSPartialPick<T, PK>;
|
|
162
180
|
/**
|
|
163
181
|
* 保留指定鍵為必填,其他鍵不變
|
|
164
182
|
* Keep specified keys as Required, other keys unchanged
|
|
@@ -198,10 +216,13 @@ export type ITSRequireAtLeastOne<T, Keys extends keyof T = keyof T> = Omit<T, Ke
|
|
|
198
216
|
* Ensure the object can only have exactly one of the specified key sets (mutually exclusive)
|
|
199
217
|
*
|
|
200
218
|
* @see https://stackoverflow.com/questions/40510611/typescript-interface-require-one-of-two-properties-to-exist
|
|
219
|
+
* @see {@link ITSPickOne} 另一種實現方式,使用 `void` 作為其餘鍵的類型
|
|
201
220
|
*
|
|
202
221
|
* @example
|
|
203
222
|
* interface User { name?: string; age?: number; }
|
|
204
|
-
* type
|
|
223
|
+
* type OnlyOne1 = ITSRequireOnlyOne<User, 'name' | 'age'>;
|
|
224
|
+
* // 輸出結果:
|
|
225
|
+
* // type OnlyOne1 = { name: string; age?: never; } | { age: number; name?: never; }
|
|
205
226
|
* // 只能提供 name 或 age 其中一個,不能同時提供
|
|
206
227
|
*/
|
|
207
228
|
export type ITSRequireOnlyOne<T, Keys extends keyof T = keyof T> = Omit<T, Keys> & {
|
package/lib/type/record.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"record.js","sourceRoot":"","sources":["record.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG","sourcesContent":["/**\n * 記錄(Record)類型操作工具\n * Record Type Manipulation Utilities\n * \n * 提供物件類型選擇、覆寫、合併等操作\n * Provides object type selection, overwrite, merge and other operations\n * \n * Created by user on 2019/6/11.\n */\n\nimport type { ITSKeyofBothDiff, ITSKeyofBothSame, ITSKeyofDiff, ITSKeyofSame } from '../helper/filter';\n\nexport type { ITSRequireRecord, ITSPartialRecord } from '../type/record/partial';\n\n/**\n * 取得兩個鍵集合的差異(已棄用)\n * Get the difference between two key sets (deprecated)\n * \n * @deprecated 請使用 ITSKeyofDiff 取代 / Please use ITSKeyofDiff instead\n */\nexport type ITSDiff<T extends keyof any, U extends keyof any> = (\n\t{ [P in T]: P }\n\t&\n\t{ [P in U]: never }\n\t&\n\t{ [x: string]: never }\n\t)[T];\n\n/**\n * 選擇 T 中與 U 相同的鍵\n * Pick keys from T that are the same as U\n * \n * @example\n * type A = { a: 1; b: 2; };\n * type B = { a: 3; c: 4; };\n * type Result = ITSPickSame<A, B>;\n * // type Result = { a: 1 }\n */\nexport type ITSPickSame<T, U> = Pick<T, ITSKeyofSame<T, U>>;\n\n/**\n * 選擇 T 中與 U 不同的鍵\n * Pick keys from T that are different from U\n * \n * @example\n * type A = { a: 1; b: 2; };\n * type B = { a: 3; c: 4; };\n * type Result = ITSPickDiff<A, B>;\n * // type Result = { b: 2 }\n */\nexport type ITSPickDiff<T, U> = Pick<T, ITSKeyofDiff<T, U>>;\n\n/**\n * 選擇 T 與 U 中同時存在的鍵\n * Pick keys that exist in both T and U\n * \n * @example\n * type A = { a: 1; b: 2; };\n * type B = { a: 3; c: 4; };\n * type Result = ITSPickBothSame<A, B>;\n * // type Result = { a: 1 }\n */\nexport type ITSPickBothSame<T, U> = Pick<T & U, ITSKeyofBothSame<T, U>>;\n\n/**\n * 選擇 T 與 U 中不同時存在的鍵\n * Pick keys that do not exist in both T and U\n * \n * @example\n * type A = { a: 1; b: 2; };\n * type B = { a: 3; c: 4; };\n * type Result = ITSPickBothDiff<A, B>;\n * // type Result = { b: 2; c: 4 }\n */\nexport type ITSPickBothDiff<T, U> = Pick<T & U, ITSKeyofBothDiff<T, U>>;\n\n/**\n * 選擇指定鍵集合 K(必須是 T 與 U 中同時存在的鍵)\n * Pick a specified key set K (must be keys that exist in both T and U)\n */\nexport type ITSPickBoth<T, U, K extends ITSKeyofBothSame<T, U> = ITSKeyofBothSame<T, U>> = Pick<T & U, K>;\n\n/**\n * 取得物件成員的類型\n * Get the type of an object member\n * \n * @see https://stackoverflow.com/questions/49198713/override-the-properties-of-an-interface-in-typescript\n */\nexport type ITSPickMember<T, K extends keyof T> = T[K];\n\n/**\n * 排除指定鍵(已棄用)\n * Exclude specified keys (deprecated)\n * \n * @deprecated 請使用 Omit 取代 / Please use Omit instead\n */\nexport type ITSPickNot<T, K extends keyof T> = Omit<T, K>;\n\n/**\n * 覆寫物件類型的屬性\n * Overwrite properties of an object type\n * \n * 用 U 中的屬性覆寫 T 中的同名屬性\n * Overwrites properties in T with the same name from U\n * \n * @see https://stackoverflow.com/questions/49198713/override-the-properties-of-an-interface-in-typescript\n * \n * @example\n * interface A1 { s: string }\n * type A2 = ITSOverwrite<A1, { s: number }>;\n * // type A2 = { s: number }\n */\nexport type ITSOverwrite<T, U> = Omit<T, keyof U> & U;\n\n/**\n * 合併兩個物件類型,處理衝突屬性\n * Merge two object types, handling conflicting properties\n * \n * 對於 T 與 U 中同時存在的鍵,取兩者的聯集類型\n * For keys that exist in both T and U, takes the union type of both\n * \n * @example\n * type Test1 = { id: number, code: string }\n * type Test2 = { id: string, code: number }\n * type Test3 = ITSMergeBoth<Test1, Test2>\n * // type Test3 = { id: string | number; code: string | number }\n * @see https://github.com/microsoft/TypeScript/issues/35627\n */\nexport type ITSMergeBoth<T, U> = ITSPickBothDiff<T, U> & Pick<T | U, ITSKeyofBothSame<T, U>>;\n\n/**\n * 選擇指定鍵並設為必填\n * Pick specified keys and mark as Required\n * \n * @example\n * interface User { name?: string; age?: number; email?: string; }\n * type RequiredName = ITSRequiredPick<User, 'name'>;\n * // type RequiredName = { name: string; age?: number; email?: string; }\n */\nexport type ITSRequiredPick<T, K extends keyof T = keyof T> = {\n\t[P in K]-?: T[P];\n};\n\n/**\n * 選擇指定鍵並設為可選\n * Pick specified keys and mark as Partial\n * \n * @example\n * interface User { name: string; age: number; email: string; }\n * type PartialName = ITSPartialPick<User, 'name'>;\n * // type PartialName = { name?: string; age: number; email: string; }\n */\nexport type ITSPartialPick<T, K extends keyof T = keyof T> = {\n\t[P in K]?: T[P];\n};\n\n/**\n * 複製類型並將指定鍵RK設為必填,其他鍵Pk設為可選\n * Clone a type and mark specified keys as Required, other keys as Partial\n * \n * @example\n * interface User { name: string; age: number; email: string; }\n * type Result = ITSPickExtra<User, 'name', 'email'>;\n * // type Result = { name: string; } & { age?: number; email?: string; }\n */\nexport type ITSPickExtra<T, RK extends keyof T, PK extends Exclude<keyof T, RK> = Exclude<keyof T, RK>> =\n\tITSRequiredPick<T, RK>\n\t& ITSPartialPick<T, PK>;\n\n/**\n * 複製類型並將指定鍵Pk設為可選,其他鍵Rk設為必填(與 ITSPickExtra 相反)\n * Clone a type and mark specified keys as Partial, other keys as Required (opposite of ITSPickExtra)\n * \n * @example\n * interface User { name: string; age: number; email: string; }\n * type Result = ITSPickExtra2<User, 'name', 'email'>;\n * // type Result = { name?: string; } & { age: number; email: string; }\n */\nexport type ITSPickExtra2<T, PK extends keyof T, RK extends Exclude<keyof T, PK> = Exclude<keyof T, PK>> =\n\tITSRequiredPick<T, RK>\n\t& ITSPartialPick<T, PK>;\n\n/**\n * 保留指定鍵為必填,其他鍵不變\n * Keep specified keys as Required, other keys unchanged\n * \n * @example\n * interface User { name: string; age: number; email: string; }\n * type Result = ITSRequiredWith<User, 'name'>;\n * // type Result = { name: string; age: number; email: string; }\n */\nexport type ITSRequiredWith<T, K extends keyof T> = Omit<T, K> & ITSRequiredPick<T, K>;\n\n/**\n * 保留指定鍵為可選,其他鍵不變\n * Keep specified keys as Partial, other keys unchanged\n * \n * @example\n * interface User { name: string; age: number; email: string; }\n * type Result = ITSPartialWith<User, 'name'>;\n * // type Result = { name?: string; age: number; email: string; }\n */\nexport type ITSPartialWith<T, K extends keyof T> = Omit<T, K> & ITSPartialPick<T, K>;\n\n/**\n * 確保物件至少具有指定的鍵集合中的一個\n * Ensure the object has at least one of the specified key sets\n * \n * @see https://stackoverflow.com/questions/40510611/typescript-interface-require-one-of-two-properties-to-exist\n * \n * @example\n * interface User { name?: string; age?: number; }\n * type AtLeastOne = ITSRequireAtLeastOne<User, 'name' | 'age'>;\n * // 需要至少提供 name 或 age 其中一個\n */\nexport type ITSRequireAtLeastOne<T, Keys extends keyof T = keyof T> =\n\tOmit<T, Keys>\n\t& {\n\t[K in Keys]-?: ITSRequiredPick<T, K>\n\t& ITSPartialPick<T, Exclude<Keys, K>>\n}[Keys];\n\n/**\n * 確保物件只能具有指定的鍵集合中的其中一個(互斥)\n * Ensure the object can only have exactly one of the specified key sets (mutually exclusive)\n
|
|
1
|
+
{"version":3,"file":"record.js","sourceRoot":"","sources":["record.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG","sourcesContent":["/**\n * 記錄(Record)類型操作工具\n * Record Type Manipulation Utilities\n * \n * 提供物件類型選擇、覆寫、合併等操作\n * Provides object type selection, overwrite, merge and other operations\n * \n * Created by user on 2019/6/11.\n */\n\nimport type { ITSKeyofBothDiff, ITSKeyofBothSame, ITSKeyofDiff, ITSKeyofSame } from '../helper/filter';\n\nexport type { ITSRequireRecord, ITSPartialRecord } from '../type/record/partial';\n\n/**\n * 取得兩個鍵集合的差異(已棄用)\n * Get the difference between two key sets (deprecated)\n * \n * @deprecated 請使用 ITSKeyofDiff 取代 / Please use ITSKeyofDiff instead\n */\nexport type ITSDiff<T extends keyof any, U extends keyof any> = (\n\t{ [P in T]: P }\n\t&\n\t{ [P in U]: never }\n\t&\n\t{ [x: string]: never }\n\t)[T];\n\n/**\n * 選擇 T 中與 U 相同的鍵\n * Pick keys from T that are the same as U\n * \n * @example\n * type A = { a: 1; b: 2; };\n * type B = { a: 3; c: 4; };\n * type Result = ITSPickSame<A, B>;\n * // type Result = { a: 1 }\n */\nexport type ITSPickSame<T, U> = Pick<T, ITSKeyofSame<T, U>>;\n\n/**\n * 選擇 T 中與 U 不同的鍵\n * Pick keys from T that are different from U\n * \n * @example\n * type A = { a: 1; b: 2; };\n * type B = { a: 3; c: 4; };\n * type Result = ITSPickDiff<A, B>;\n * // type Result = { b: 2 }\n */\nexport type ITSPickDiff<T, U> = Pick<T, ITSKeyofDiff<T, U>>;\n\n/**\n * 選擇 T 與 U 中同時存在的鍵\n * Pick keys that exist in both T and U\n * \n * @example\n * type A = { a: 1; b: 2; };\n * type B = { a: 3; c: 4; };\n * type Result = ITSPickBothSame<A, B>;\n * // type Result = { a: 1 }\n */\nexport type ITSPickBothSame<T, U> = Pick<T & U, ITSKeyofBothSame<T, U>>;\n\n/**\n * 選擇 T 與 U 中不同時存在的鍵\n * Pick keys that do not exist in both T and U\n * \n * @example\n * type A = { a: 1; b: 2; };\n * type B = { a: 3; c: 4; };\n * type Result = ITSPickBothDiff<A, B>;\n * // type Result = { b: 2; c: 4 }\n */\nexport type ITSPickBothDiff<T, U> = Pick<T & U, ITSKeyofBothDiff<T, U>>;\n\n/**\n * 選擇指定鍵集合 K(必須是 T 與 U 中同時存在的鍵)\n * Pick a specified key set K (must be keys that exist in both T and U)\n */\nexport type ITSPickBoth<T, U, K extends ITSKeyofBothSame<T, U> = ITSKeyofBothSame<T, U>> = Pick<T & U, K>;\n\n/**\n * 取得物件成員的類型\n * Get the type of an object member\n * \n * @see https://stackoverflow.com/questions/49198713/override-the-properties-of-an-interface-in-typescript\n */\nexport type ITSPickMember<T, K extends keyof T> = T[K];\n\n/**\n * 排除指定鍵(已棄用)\n * Exclude specified keys (deprecated)\n * \n * @deprecated 請使用 Omit 取代 / Please use Omit instead\n */\nexport type ITSPickNot<T, K extends keyof T> = Omit<T, K>;\n\n/**\n * 覆寫物件類型的屬性\n * Overwrite properties of an object type\n * \n * 用 U 中的屬性覆寫 T 中的同名屬性\n * Overwrites properties in T with the same name from U\n * \n * @see https://stackoverflow.com/questions/49198713/override-the-properties-of-an-interface-in-typescript\n * \n * @example\n * interface A1 { s: string }\n * type A2 = ITSOverwrite<A1, { s: number }>;\n * // type A2 = { s: number }\n */\nexport type ITSOverwrite<T, U> = Omit<T, keyof U> & U;\n\n/**\n * 合併兩個物件類型,處理衝突屬性\n * Merge two object types, handling conflicting properties\n * \n * 對於 T 與 U 中同時存在的鍵,取兩者的聯集類型\n * For keys that exist in both T and U, takes the union type of both\n * \n * @example\n * type Test1 = { id: number, code: string }\n * type Test2 = { id: string, code: number }\n * type Test3 = ITSMergeBoth<Test1, Test2>\n * // type Test3 = { id: string | number; code: string | number }\n * @see https://github.com/microsoft/TypeScript/issues/35627\n */\nexport type ITSMergeBoth<T, U> = ITSPickBothDiff<T, U> & Pick<T | U, ITSKeyofBothSame<T, U>>;\n\n/**\n * 選擇指定鍵並設為必填\n * Pick specified keys and mark as Required\n * \n * @example\n * interface User { name?: string; age?: number; email?: string; }\n * type RequiredName = ITSRequiredPick<User, 'name'>;\n * // type RequiredName = { name: string; age?: number; email?: string; }\n */\nexport type ITSRequiredPick<T, K extends keyof T = keyof T> = {\n\t[P in K]-?: T[P];\n};\n\n/**\n * 選擇指定鍵並設為可選\n * Pick specified keys and mark as Partial\n * \n * @example\n * interface User { name: string; age: number; email: string; }\n * type PartialName = ITSPartialPick<User, 'name'>;\n * // type PartialName = { name?: string; age: number; email: string; }\n */\nexport type ITSPartialPick<T, K extends keyof T = keyof T> = {\n\t[P in K]?: T[P];\n};\n\n/**\n * 複製類型並將指定鍵RK設為必填,其他鍵Pk設為可選\n * Clone a type and mark specified keys as Required, other keys as Partial\n * \n * @example\n * interface User { name: string; age: number; email: string; }\n * type Result = ITSPickExtra<User, 'name', 'email'>;\n * // type Result = { name: string; } & { age?: number; email?: string; }\n */\nexport type ITSPickExtra<T, RK extends keyof T, PK extends Exclude<keyof T, RK> = Exclude<keyof T, RK>> =\n\tITSRequiredPick<T, RK>\n\t& ITSPartialPick<T, PK>;\n\n/**\n * 複製類型並將指定鍵Pk設為可選,其他鍵Rk設為必填(與 ITSPickExtra 相反)\n * Clone a type and mark specified keys as Partial, other keys as Required (opposite of ITSPickExtra)\n * \n * @example\n * interface User { name: string; age: number; email: string; }\n * type Result = ITSPickExtra2<User, 'name', 'email'>;\n * // type Result = { name?: string; } & { age: number; email: string; }\n */\nexport type ITSPickExtra2<T, PK extends keyof T, RK extends Exclude<keyof T, PK> = Exclude<keyof T, PK>> =\n\tITSRequiredPick<T, RK>\n\t& ITSPartialPick<T, PK>;\n\n/**\n * 保留指定鍵不變(維持原本的必選/可選/唯讀屬性),其他鍵變為可選\n * Keep specified keys unchanged (preserving required/optional/readonly modifiers), other keys become Partial\n *\n * 與 ITSPickExtra 的差異:ITSPickExtra 會將指定鍵強制設為必填,而此類型保留原始修飾符\n * Difference from ITSPickExtra: ITSPickExtra forces specified keys to Required, while this type preserves original modifiers\n *\n * @see {@link ITSPickExtra} - 將指定鍵設為必填,其他鍵可選\n * @see {@link ITSPartialWith} - 將指定鍵設為可選,其他鍵不變\n *\n * @example\n * interface User { name: string; readonly id: string; age?: number; email: string; }\n * type Result = ITSPickAndPartialOther<User, 'name' | 'id'>;\n * // 輸出結果 / Output:\n * // { name: string; readonly id: string; } & { age?: number; email?: string; }\n * // 注意:name 維持必選,id 維持唯讀且必選,age 和 email 變為可選\n */\nexport type ITSPickAndPartialOther<T, RK extends keyof T, PK extends Exclude<keyof T, RK> = Exclude<keyof T, RK>> = Pick<T, RK>\n\t& ITSPartialPick<T, PK>;\n\n/**\n * 保留指定鍵為必填,其他鍵不變\n * Keep specified keys as Required, other keys unchanged\n * \n * @example\n * interface User { name: string; age: number; email: string; }\n * type Result = ITSRequiredWith<User, 'name'>;\n * // type Result = { name: string; age: number; email: string; }\n */\nexport type ITSRequiredWith<T, K extends keyof T> = Omit<T, K> & ITSRequiredPick<T, K>;\n\n/**\n * 保留指定鍵為可選,其他鍵不變\n * Keep specified keys as Partial, other keys unchanged\n * \n * @example\n * interface User { name: string; age: number; email: string; }\n * type Result = ITSPartialWith<User, 'name'>;\n * // type Result = { name?: string; age: number; email: string; }\n */\nexport type ITSPartialWith<T, K extends keyof T> = Omit<T, K> & ITSPartialPick<T, K>;\n\n/**\n * 確保物件至少具有指定的鍵集合中的一個\n * Ensure the object has at least one of the specified key sets\n * \n * @see https://stackoverflow.com/questions/40510611/typescript-interface-require-one-of-two-properties-to-exist\n * \n * @example\n * interface User { name?: string; age?: number; }\n * type AtLeastOne = ITSRequireAtLeastOne<User, 'name' | 'age'>;\n * // 需要至少提供 name 或 age 其中一個\n */\nexport type ITSRequireAtLeastOne<T, Keys extends keyof T = keyof T> =\n\tOmit<T, Keys>\n\t& {\n\t[K in Keys]-?: ITSRequiredPick<T, K>\n\t& ITSPartialPick<T, Exclude<Keys, K>>\n}[Keys];\n\n/**\n * 確保物件只能具有指定的鍵集合中的其中一個(互斥)\n * Ensure the object can only have exactly one of the specified key sets (mutually exclusive)\n *\n * @see https://stackoverflow.com/questions/40510611/typescript-interface-require-one-of-two-properties-to-exist\n * @see {@link ITSPickOne} 另一種實現方式,使用 `void` 作為其餘鍵的類型\n *\n * @example\n * interface User { name?: string; age?: number; }\n * type OnlyOne1 = ITSRequireOnlyOne<User, 'name' | 'age'>;\n * // 輸出結果:\n * // type OnlyOne1 = { name: string; age?: never; } | { age: number; name?: never; }\n * // 只能提供 name 或 age 其中一個,不能同時提供\n */\nexport type ITSRequireOnlyOne<T, Keys extends keyof T = keyof T> =\n\tOmit<T, Keys>\n\t& {\n\t[K in Keys]-?: ITSRequiredPick<T, K>\n\t& Partial<Record<Exclude<Keys, K>, never>>\n}[Keys];\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-type",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.12",
|
|
4
4
|
"description": "TypeScript 類型工具庫:提供豐富的類型操作工具和重新導出的內建類型 / TypeScript type utility library: provides rich type manipulation utilities and re-exported built-in types",
|
|
5
5
|
"keywords": [
|
|
6
6
|
".d.ts",
|
|
@@ -97,5 +97,5 @@
|
|
|
97
97
|
"peerDependencies": {
|
|
98
98
|
"ts-toolbelt": "^9.6.0"
|
|
99
99
|
},
|
|
100
|
-
"gitHead": "
|
|
100
|
+
"gitHead": "68144dcde64f5288df56b29573071d1583bc7087"
|
|
101
101
|
}
|