ts-type 3.0.12 → 3.0.14
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 +46 -0
- package/lib/_build-in.js.map +1 -1
- package/lib/generic.js.map +1 -1
- package/lib/helper/array/readonly.js.map +1 -1
- package/lib/helper/conditional.js.map +1 -1
- package/lib/helper/deep-map.js.map +1 -1
- package/lib/helper/filter.d.ts +165 -0
- package/lib/helper/filter.js.map +1 -1
- package/lib/helper/infer.js.map +1 -1
- package/lib/helper/intersection.js.map +1 -1
- package/lib/helper/key-value.d.ts +42 -5
- package/lib/helper/key-value.js.map +1 -1
- package/lib/helper/number.js.map +1 -1
- package/lib/helper/readonly.js.map +1 -1
- package/lib/helper/record/enum.js.map +1 -1
- package/lib/helper/record/member.d.ts +132 -2
- package/lib/helper/record/member.js.map +1 -1
- package/lib/helper/record/omit-index.js.map +1 -1
- package/lib/helper/record/partial.js.map +1 -1
- package/lib/helper/record/pick-type.js.map +1 -1
- package/lib/helper/record.js.map +1 -1
- package/lib/helper/string/infer.js.map +1 -1
- package/lib/helper/string/literal-string.js.map +1 -1
- package/lib/helper/string/operations.js.map +1 -1
- package/lib/helper/string.js.map +1 -1
- package/lib/helper/tuple.js.map +1 -1
- package/lib/helper/typeof.js.map +1 -1
- package/lib/helper.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/internal/filter.js.map +1 -1
- package/lib/internal/logic/string-literal.js.map +1 -1
- package/lib/logic/any.js.map +1 -1
- package/lib/logic/never.js.map +1 -1
- package/lib/logic/record/empty.js.map +1 -1
- package/lib/logic/union/index.js.map +1 -1
- package/lib/type/base.js.map +1 -1
- package/lib/type/generator.js.map +1 -1
- package/lib/type/iterator.js.map +1 -1
- package/lib/type/promise.js.map +1 -1
- package/lib/type/proxy.js.map +1 -1
- package/lib/type/record/empty.js.map +1 -1
- package/lib/type/record/enum.js.map +1 -1
- package/lib/type/record/partial.js.map +1 -1
- package/lib/type/record/readonly.js.map +1 -1
- package/lib/type/record/union.d.ts +135 -0
- package/lib/type/record/union.js +27 -0
- package/lib/type/record/union.js.map +1 -0
- package/lib/type/record.d.ts +32 -24
- package/lib/type/record.js.map +1 -1
- package/lib/type/tuple/empty.js.map +1 -1
- package/package.json +15 -12
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,52 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [3.0.14](https://github.com/bluelovers/ws-ts-type/compare/ts-type@3.0.13...ts-type@3.0.14) (2026-09-25)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### 🐛 Bug Fixes
|
|
11
|
+
|
|
12
|
+
* **ts-type:** 修復聯集類型扁平化後的屬性映射邏輯 ([201f812](https://github.com/bluelovers/ws-ts-type/commit/201f8120c8ce21a1655b805fd809e2bdb5120ce3))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### ✨ Features
|
|
16
|
+
|
|
17
|
+
* **ts-type:** 新增針對聯集類型的 Record 操作工具 ([cfc6f73](https://github.com/bluelovers/ws-ts-type/commit/cfc6f7328b910281a245a66a8205a454d30cf521))
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### 📦 Code Refactoring
|
|
21
|
+
|
|
22
|
+
* **ts-type:** 優化類型定義結構與清理註解格式 ([d83912f](https://github.com/bluelovers/ws-ts-type/commit/d83912fcc7bd313f7af39140e8d70a951bcb8051))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### ♻️ Chores
|
|
26
|
+
|
|
27
|
+
* **build:** 將專案腳本從 yarn 遷移至 pnpm ([7e4ef6b](https://github.com/bluelovers/ws-ts-type/commit/7e4ef6b601bdc50a6202ecf0cad90dc91b0902d6))
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
## [3.0.13](https://github.com/bluelovers/ws-ts-type/compare/ts-type@3.0.12...ts-type@3.0.13) (2026-03-31)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
### ✨ Features
|
|
36
|
+
|
|
37
|
+
* **ts-type:** add advanced key filtering and member extraction types ([bee21a9](https://github.com/bluelovers/ws-ts-type/commit/bee21a97560a9c58dc8208a90166544054fc3809))
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
### 🛠 Build System
|
|
41
|
+
|
|
42
|
+
* **release:** publish ([26936b2](https://github.com/bluelovers/ws-ts-type/commit/26936b2d770aaf20636d4f79e682b68314b4b926))
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
### 🔖 Miscellaneous
|
|
46
|
+
|
|
47
|
+
* . ([cd29157](https://github.com/bluelovers/ws-ts-type/commit/cd29157bc14de461549af02e6619185666e500f9))
|
|
48
|
+
* . ([6c6082f](https://github.com/bluelovers/ws-ts-type/commit/6c6082f9dbd69f86a69cda44bfa3fea3bae1d15b))
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
6
52
|
## [3.0.12](https://github.com/bluelovers/ws-ts-type/compare/ts-type@3.0.10...ts-type@3.0.12) (2026-03-28)
|
|
7
53
|
|
|
8
54
|
|
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
|
|
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 * 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,\n\tpropertyKey: string | symbol,\n\tdescriptor: TypedPropertyDescriptor<T>,\n) => 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.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
|
|
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\n\t/** 數字索引存取 / Numeric index access */\n\t[n: number]: T;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"readonly.js","sourceRoot":"","sources":["readonly.ts"],"names":[],"mappings":"","sourcesContent":["import { ITSArrayListMaybeReadonly } from '../../type/base';\n\n/**\n * 將可讀寫陣列轉換為唯讀陣列\n * Convert writable array to readonly array\n
|
|
1
|
+
{"version":3,"file":"readonly.js","sourceRoot":"","sources":["readonly.ts"],"names":[],"mappings":"","sourcesContent":["import { ITSArrayListMaybeReadonly } from '../../type/base';\n\n/**\n * 將可讀寫陣列轉換為唯讀陣列\n * Convert writable array to readonly array\n *\n * @example\n * type Writable = [1, 2, 3];\n * type Readonly = ITSToReadonlyArray<Writable>; // readonly [1, 2, 3]\n */\nexport type ITSToReadonlyArray<T extends ITSArrayListMaybeReadonly<any>> = T extends [...infer R] | readonly [...infer R]\n\t? readonly [...R]\n\t: never\n\t;\n\n/**\n * 將唯讀陣列轉換為可讀寫陣列\n * Convert readonly array to writable array\n *\n * @example\n * type Readonly = readonly [1, 2, 3];\n * type Writable = ITSToWriteableArray<Readonly>; // [1, 2, 3]\n */\nexport type ITSToWriteableArray<T extends ITSArrayListMaybeReadonly<any>> = T extends [...infer R] | readonly [...infer R]\n\t? [...R]\n\t: never\n\t;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conditional.js","sourceRoot":"","sources":["conditional.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG","sourcesContent":["/**\n * 條件類型工具\n * Conditional Type Utilities\n
|
|
1
|
+
{"version":3,"file":"conditional.js","sourceRoot":"","sources":["conditional.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG","sourcesContent":["/**\n * 條件類型工具\n * Conditional Type Utilities\n *\n * 提供基於條件的類型選擇工具\n * Provides conditional-based type selection utilities\n */\n\n/**\n * 條件類型:根據條件 C 選擇 T 或 U\n * Conditional type: select T or U based on condition C\n *\n * @example\n * type IsString<T> = T extends string ? true : false;\n * type Result = ITSIf<IsString<T>, 'is string', 'not string'>;\n */\nexport type ITSHelperIf<C extends boolean, T, U> = C extends true ? T : U;\n\n/**\n * 條件類型:根據類型匹配選擇 T 或 U\n * Conditional type: select T or U based on type match\n *\n * @example\n * type Result = ITSIfExtends<string, 'text', 'other'>; // 'text'\n * type Result2 = ITSIfExtends<number, 'text', 'other'>; // 'other'\n */\nexport type ITSHelperIfExtends<T, U, IfTrue, IfFalse> = T extends U ? IfTrue : IfFalse;\n\n/**\n * 條件類型:根據類型相等選擇 T 或 U\n * Conditional type: select T or U based on type equality\n *\n * @example\n * type Result = ITSIfEquals<string, string, 'same', 'different'>; // 'same'\n * type Result2 = ITSIfEquals<string, number, 'same', 'different'>; // 'different'\n */\nexport type ITSHelperIfEquals<T, U, IfTrue, IfFalse> = T extends U\n\t? U extends T\n\t\t? IfTrue\n\t\t: IfFalse\n\t: IfFalse;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deep-map.js","sourceRoot":"","sources":["deep-map.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG","sourcesContent":["/**\n * 深層映射工具\n * Deep Mapping Utilities\n
|
|
1
|
+
{"version":3,"file":"deep-map.js","sourceRoot":"","sources":["deep-map.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG","sourcesContent":["/**\n * 深層映射工具\n * Deep Mapping Utilities\n *\n * 提供對物件進行深層類型轉換的工具\n * Provides utilities for deep type transformation of objects\n */\n\nimport { ITSArrayListMaybeReadonly } from '../type/base';\n\n/**\n * 深層映射:對物件的所有屬性應用轉換 - 無法實現\n * Deep mapping: apply transformation to all properties of an object\n *\n * TypeScript 無法在類型層級將函數作為泛型參數傳遞\n * TypeScript cannot pass functions as generic parameters at type level\n *\n * @example\n * interface User { name: string; profile: { age: number; } }\n * type ReadonlyUser = ITSDeepMap<User, readonly>;\n * // { readonly name: string; readonly profile: { readonly age: number; } }\n */\n// export type ITSDeepMap<T, F> = never;\n\n// 保留原始實現作為參考 / Keep original implementation for reference\n// export type ITSDeepMap<T, F> = T extends object \n// \t? T extends ITSArrayListMaybeReadonly<any>\n// \t\t? T\n// \t\t: {\n// \t\t\t[K in keyof T]: ITSDeepMap<T[K], F>;\n// \t\t }\n// \t: T;\n\n/**\n * 深層映射:對物件的所有屬性應用轉換(包含陣列)- 無法實現\n * Deep mapping: apply transformation to all properties of an object (including arrays)\n *\n * TypeScript 無法在類型層級將函數作為泛型參數傳遞\n * TypeScript cannot pass functions as generic parameters at type level\n *\n * @example\n * interface Data { items: string[]; nested: { values: number[]; } }\n * type ReadonlyData = ITSDeepMapWithArrays<Data, readonly>;\n * // { readonly items: readonly string[]; readonly nested: { readonly values: readonly number[]; } }\n */\n// export type ITSDeepMapWithArrays<T, F> = never;\n\n// 保留原始實現作為參考 / Keep original implementation for reference\n// export type ITSDeepMapWithArrays<T, F extends (x: any) => any> = T extends object \n// \t? T extends ITSArrayListMaybeReadonly<any>\n// \t\t? F<T>\n// \t\t: {\n// \t\t\t[K in keyof T]: ITSDeepMapWithArrays<T[K], F>;\n// \t\t }\n// \t: T;\n\n/**\n * 深層部分映射:對物件的所有屬性應用 Partial\n * Deep partial mapping: apply Partial to all properties of an object\n *\n * @example\n * interface User { name: string; profile: { age: number; } }\n * type PartialUser = ITSDeepPartial<User>;\n * // { name?: string; profile?: { age?: number; } }\n */\nexport type ITSDeepPartial<T> = T extends object\n\t? T extends ITSArrayListMaybeReadonly<any>\n\t\t? T\n\t\t: {\n\t\t\t[K in keyof T]?: ITSDeepPartial<T[K]>;\n\t\t}\n\t: T;\n\n/**\n * 深層必填映射:對物件的所有屬性應用 Required\n * Deep required mapping: apply Required to all properties of an object\n *\n * @example\n * interface User { name?: string; profile?: { age?: number; } }\n * type RequiredUser = ITSDeepRequired<User>;\n * // { name: string; profile: { age: number; } }\n */\nexport type ITSDeepRequired<T> = T extends object\n\t? T extends ITSArrayListMaybeReadonly<any>\n\t\t? T\n\t\t: {\n\t\t\t[K in keyof T]-?: ITSDeepRequired<T[K]>;\n\t\t}\n\t: T;\n"]}
|
package/lib/helper/filter.d.ts
CHANGED
|
@@ -89,3 +89,168 @@ export type ITSKeyofBothSame<T, U> = ITSKeyofSame<T, U> | ITSKeyofSame<U, T>;
|
|
|
89
89
|
* type Result = ITSKeyofBothDiff<A, B>; // 'b' | 'c'
|
|
90
90
|
*/
|
|
91
91
|
export type ITSKeyofBothDiff<T, U> = ITSKeyofDiff<T, U> | ITSKeyofDiff<U, T>;
|
|
92
|
+
/**
|
|
93
|
+
* ITSFilterKeysString - 基礎鍵過濾類型
|
|
94
|
+
* 從聯合類型中排除指定的字符串鍵
|
|
95
|
+
*
|
|
96
|
+
* 使用範例:
|
|
97
|
+
* @example
|
|
98
|
+
* type AllKeys = 'log' | 'debug' | 'constructor' | 'error';
|
|
99
|
+
* type PublicKeys = ITSFilterKeysString<AllKeys, 'constructor'>; // 'log' | 'debug' | 'error'
|
|
100
|
+
* type SafeKeys = ITSFilterKeysString<AllKeys, 'constructor' | 'error'>; // 'log' | 'debug'
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* type ITSFilterKeysString<T, ExcludeKeys extends string = never> = T extends infer U
|
|
104
|
+
* ? U extends string
|
|
105
|
+
* ? U extends ExcludeKeys ? never : U
|
|
106
|
+
* : never
|
|
107
|
+
* : never;
|
|
108
|
+
*
|
|
109
|
+
* @see ITSExcludeFilterKeys
|
|
110
|
+
*/
|
|
111
|
+
export type ITSExcludeFilterKeysString<T, ExcludeKeys extends string = never> = ITSExcludeFilterKeys<T, ExcludeKeys, string>;
|
|
112
|
+
/**
|
|
113
|
+
* ITSFilterKeysPropertyKey - 支持 PropertyKey(string | number | symbol)的版本
|
|
114
|
+
*
|
|
115
|
+
* 使用範例:
|
|
116
|
+
* @example
|
|
117
|
+
* type AllKeys = 'log' | 'debug' | 'constructor' | 42 | symbol;
|
|
118
|
+
* type StringKeys = ITSFilterKeysPropertyKey<AllKeys, number | symbol>; // 'log' | 'debug' | 'constructor'
|
|
119
|
+
* type NoSymbol = ITSFilterKeysPropertyKey<AllKeys, symbol>; // 'log' | 'debug' | 'constructor' | 42
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* type ITSFilterKeysPropertyKey<T, ExcludeKeys extends PropertyKey = never> = T extends infer U
|
|
123
|
+
* ? U extends PropertyKey
|
|
124
|
+
* ? U extends ExcludeKeys ? never : U
|
|
125
|
+
* : never
|
|
126
|
+
* : never;
|
|
127
|
+
*
|
|
128
|
+
* @see ITSExcludeFilterKeys
|
|
129
|
+
*/
|
|
130
|
+
export type ITSExcludeFilterKeysPropertyKey<T, ExcludeKeys extends PropertyKey = never> = ITSExcludeFilterKeys<T, ExcludeKeys, PropertyKey>;
|
|
131
|
+
/**
|
|
132
|
+
* ITSFilterKeys - 可配置鍵類型範圍的版本
|
|
133
|
+
* 第三個類型參數 `AllowedPropertyKey` 允許精確控制保留哪些類型的鍵
|
|
134
|
+
*
|
|
135
|
+
* 使用範例:
|
|
136
|
+
* @example
|
|
137
|
+
* ```
|
|
138
|
+
* type AllKeys = 'log' | 'debug' | 42 | 99 | symbol;
|
|
139
|
+
*
|
|
140
|
+
* // 只保留 string 類型,排除 'log'
|
|
141
|
+
* type StringOnly = ITSFilterKeys<AllKeys, 'log', string>; // 'debug'
|
|
142
|
+
*
|
|
143
|
+
* // 只保留 number 類型,不排除任何鍵
|
|
144
|
+
* type NumOnly = ITSFilterKeys<AllKeys, never, number>; // 42 | 99
|
|
145
|
+
*
|
|
146
|
+
* // 保留 string | number,排除 'debug'
|
|
147
|
+
* type Mixed = ITSFilterKeys<AllKeys, 'debug', string | number>; // 'log' | 42 | 99
|
|
148
|
+
* ```
|
|
149
|
+
*
|
|
150
|
+
* 三代版本對比:
|
|
151
|
+
* ITSFilterKeysString: 固定處理 string(最基礎)
|
|
152
|
+
* ITSFilterKeysPropertyKey: 固定處理 PropertyKey(更廣泛)
|
|
153
|
+
* ITSFilterKeys: 可配置 AllowedPropertyKey 參數(最靈活)
|
|
154
|
+
*
|
|
155
|
+
* @see ITSExtractKeysV13
|
|
156
|
+
*/
|
|
157
|
+
export type ITSExcludeFilterKeys<T, ExcludeKeys extends PropertyKey = never, AllowedPropertyKey extends PropertyKey = PropertyKey> = T extends infer U ? U extends AllowedPropertyKey ? U extends ExcludeKeys ? never : U : never : never;
|
|
158
|
+
/**
|
|
159
|
+
* ITSExtractKeysV13 - 選擇器版本(只保留匹配 IncludeKeys 的鍵)
|
|
160
|
+
*
|
|
161
|
+
* 與 FilterKeysV3 的邏輯差異:
|
|
162
|
+
* - FilterKeysV3: `U extends ExcludeKeys ? never : U` → 排除匹配的鍵
|
|
163
|
+
* - ITSExtractKeysV13: `U extends IncludeKeys ? U : never` → 只保留匹配的鍵
|
|
164
|
+
*
|
|
165
|
+
* 設計理念:
|
|
166
|
+
* ---------------------------
|
|
167
|
+
* ExtractKeyofV12 系列的核心需求是「篩選符合 V 類型的鍵」。
|
|
168
|
+
* FilterKeys 系列的設計是「排除」思維( ExcludeKeys ),
|
|
169
|
+
* 而 ITSExtractKeysV13 採用「選擇」思維( IncludeKeys ),更符合鍵提取的語義。
|
|
170
|
+
*
|
|
171
|
+
* 與 FilterKeysV3 實作的對比:
|
|
172
|
+
* - FilterKeysV3: `FilterKeysV3<T, never, V>`(用 AllowedPropertyKey 篩選,ExcludeKeys 設為 never)
|
|
173
|
+
* - ITSExtractKeysV13: `ITSExtractKeysV13<T, V, PropertyKey>`(直接用 IncludeKeys 篩選)
|
|
174
|
+
*
|
|
175
|
+
* 使用範例與測試驗證:
|
|
176
|
+
* ```typescript
|
|
177
|
+
* type AllKeys = 'log' | 'debug' | 'error' | 42 | 99 | symbol;
|
|
178
|
+
*
|
|
179
|
+
* // 1. 篩選 string 類型鍵(等同 ExtractKeyofV12d<AllKeys, string>)
|
|
180
|
+
* // ✅ 結果:'log' | 'debug' | 'error'
|
|
181
|
+
* type Strings = ITSExtractKeysV13<AllKeys, string, PropertyKey>;
|
|
182
|
+
*
|
|
183
|
+
* // 2. 篩選 number 類型鍵
|
|
184
|
+
* // ✅ 結果:42 | 99
|
|
185
|
+
* type Numbers = ITSExtractKeysV13<AllKeys, number, PropertyKey>;
|
|
186
|
+
*
|
|
187
|
+
* // 3. 只保留特定字面量
|
|
188
|
+
* // ✅ 結果:'log' | 'debug'
|
|
189
|
+
* type Picked = ITSExtractKeysV13<AllKeys, 'log' | 'debug', PropertyKey>;
|
|
190
|
+
*
|
|
191
|
+
* // 4. 特定數字字面量(只保留 42,過濾 99)
|
|
192
|
+
* // ✅ 結果:42
|
|
193
|
+
* // ❌ 99 會被過濾
|
|
194
|
+
* type SpecificNumber = ITSExtractKeysV13<AllKeys, 42, PropertyKey>;
|
|
195
|
+
*
|
|
196
|
+
* // 5. 實作 ExtractKeyofV12(輸出轉為 string)
|
|
197
|
+
* // ✅ 結果:'log' | 'debug' | 'error'
|
|
198
|
+
* type V12_Strings = ITSExtractKeysV13<AllKeys, string, PropertyKey> & string;
|
|
199
|
+
*
|
|
200
|
+
* // 6. 限制輸入類型為 string(等同 FilterKeysV3<AllKeys, never, string>)
|
|
201
|
+
* // ✅ 結果:'log' | 'debug' | 'error'
|
|
202
|
+
* // ❌ number/symbol 被過濾
|
|
203
|
+
* type StringOnly = ITSExtractKeysV13<AllKeys, PropertyKey, string>;
|
|
204
|
+
*
|
|
205
|
+
* // 7. symbol 類型篩選(結果為 never)
|
|
206
|
+
* // ✅ 結果:never(symbol 無法通過 PropertyKey 匹配 string/number)
|
|
207
|
+
* type SymbolFiltered = ITSExtractKeysV13<AllKeys, symbol, PropertyKey>;
|
|
208
|
+
* ```
|
|
209
|
+
*
|
|
210
|
+
* Enum 類型處理範例:
|
|
211
|
+
* ------------------
|
|
212
|
+
* TypeScript enum 作為類型時,需使用模板字面量 `${Enum}` 或 keyof typeof 獲取成員。
|
|
213
|
+
*
|
|
214
|
+
* ```typescript
|
|
215
|
+
* enum LogLevel {
|
|
216
|
+
* DEBUG = 'debug',
|
|
217
|
+
* INFO = 'info',
|
|
218
|
+
* WARN = 'warn',
|
|
219
|
+
* ERROR = 'error'
|
|
220
|
+
* }
|
|
221
|
+
*
|
|
222
|
+
* enum StatusCode {
|
|
223
|
+
* OK = 200,
|
|
224
|
+
* NOT_FOUND = 404,
|
|
225
|
+
* ERROR = 500
|
|
226
|
+
* }
|
|
227
|
+
*
|
|
228
|
+
* // 8. 從 enum 鍵中篩選(使用 keyof typeof)
|
|
229
|
+
* // ✅ 結果:'DEBUG' | 'INFO'
|
|
230
|
+
* type EnumKeys = keyof typeof LogLevel; // 'DEBUG' | 'INFO' | 'WARN' | 'ERROR'
|
|
231
|
+
* type PickedEnumKeys = ITSExtractKeysV13<EnumKeys, 'DEBUG' | 'INFO', PropertyKey>;
|
|
232
|
+
*
|
|
233
|
+
* // 11. 數字 enum 的值篩選(使用 infer 提取數字)
|
|
234
|
+
* // ✅ 結果:200 | 500
|
|
235
|
+
* type StatusEnumValues = `${StatusCode}` extends `${infer N extends number}` ? N : never;
|
|
236
|
+
* type PickedStatusValues = ITSExtractKeysV13<200 | 404 | 500, StatusEnumValues, PropertyKey>;
|
|
237
|
+
*
|
|
238
|
+
* // 12. enum 作為第二參數(IncludeKeys)使用 ITSTypeAndStringLiteral
|
|
239
|
+
* // 搭配 ts-type 的 ITSTypeAndStringLiteral 可直接將 enum 轉為 string 字面量
|
|
240
|
+
* // ✅ 結果:'debug' | 'error'
|
|
241
|
+
* import { ITSTypeAndStringLiteral } from 'ts-type';
|
|
242
|
+
* type MixedInput = ITSTypeAndStringLiteral<LogLevel> | 42 | symbol;
|
|
243
|
+
* type EnumAsIncludeKeys = ITSTypeAndStringLiteral<LogLevel.DEBUG | LogLevel.ERROR>;
|
|
244
|
+
* type Filtered = ITSExtractKeysV13<MixedInput, EnumAsIncludeKeys, PropertyKey>;
|
|
245
|
+
* // ✅ 可賦值:LogLevel.DEBUG, LogLevel.ERROR
|
|
246
|
+
* // ❌ 被過濾:'trace', 'fatal', 42, symbol
|
|
247
|
+
*
|
|
248
|
+
* // 13. 數字 enum 直接作為 IncludeKeys
|
|
249
|
+
* // ✅ 結果:200 | 404 | 500
|
|
250
|
+
* type AllCodes = 200 | 301 | 404 | 500 | 503;
|
|
251
|
+
* type StatusCodes = ITSExtractKeysV13<AllCodes, StatusCode, PropertyKey>;
|
|
252
|
+
* ```
|
|
253
|
+
*
|
|
254
|
+
* @see ITSExcludeFilterKeys
|
|
255
|
+
*/
|
|
256
|
+
export type ITSExtractKeysV13<T, IncludeKeys extends PropertyKey, AllowedPropertyKey extends PropertyKey = PropertyKey> = T extends infer U ? U extends AllowedPropertyKey ? U extends IncludeKeys ? U : never : never : never;
|
package/lib/helper/filter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"filter.js","sourceRoot":"","sources":["filter.ts"],"names":[],"mappings":";AAAA;;GAEG","sourcesContent":["/**\n * Created by user on 2019/6/8.\n */\n\n/**\n * 從 T 中排除可指派給 U 的類型,並替換為 R\n * Exclude from T those types that are assignable to U, and replace to R\n
|
|
1
|
+
{"version":3,"file":"filter.js","sourceRoot":"","sources":["filter.ts"],"names":[],"mappings":";AAAA;;GAEG","sourcesContent":["/**\n * Created by user on 2019/6/8.\n */\n\n/**\n * 從 T 中排除可指派給 U 的類型,並替換為 R\n * Exclude from T those types that are assignable to U, and replace to R\n *\n * @example\n * type Test = string | number | boolean;\n * type Result = ITSExclude2<Test, string, never>; // number | boolean\n */\nexport type ITSExclude2<T, U, R = T> = T extends U ? never : R;\n\n/**\n * 從 T 中提取可指派給 U 的類型,並替換為 R\n * Extract from T those types that are assignable to U, and replace to R\n *\n * @example\n * type Test = string | number | boolean;\n * type Result = ITSExtract2<Test, string, 'text'>; // 'text' | number | boolean\n */\nexport type ITSExtract2<T, U, R = T> = T extends U ? R : never;\n\n/**\n * 提取 T 中屬於 U 的鍵\n * Extract keys from T that belong to U\n */\nexport type ITSExtractKeyof<T, U> = Extract<keyof T, U>;\n\n/**\n * 從類陣列類型中提取指定索引的類型\n * Extract types of specified indexes from array-like type\n *\n * @example\n * type Test = [string, number, boolean];\n * type Result = ITSExtractArrayLike<Test, 0 | 2>; // [string, boolean]\n */\nexport type ITSExtractArrayLike<A, K extends Extract<keyof A, number> = Extract<keyof A, number>> = {\n\t[Index in K]: A[Index];\n};\n\n/**\n * 取得類陣列類型的所有數字索引鍵\n * Get all numeric index keys of array-like type\n */\nexport type ITSKeyofArrayLike<A> = keyof ITSExtractArrayLike<A>;\n\n/**\n * 取得可為 null 或 undefined 的類型\n * Get types that can be null or undefined\n *\n * @example\n * type Test = string | number | null;\n * type Result = ITSNullable<Test>; // string | number | null\n */\nexport type ITSNullable<T> = T extends null | undefined ? T : never;\n\n/**\n * 找出 T 當中與 U 相同的鍵\n * Find keys that are the same between T and U\n *\n * @example\n * type A = { a: 1; b: 2; };\n * type B = { a: 3; c: 4; };\n * type Result = ITSKeyofSame<A, B>; // 'a'\n */\nexport type ITSKeyofSame<T, U> = Extract<keyof T, keyof U>;\n/**\n * 找出 T 當中與 U 不同的鍵\n * Find keys that are different between T and U\n *\n * @example\n * type A = { a: 1; b: 2; };\n * type B = { a: 3; c: 4; };\n * type Result = ITSKeyofDiff<A, B>; // 'b'\n */\nexport type ITSKeyofDiff<T, U> = Exclude<keyof T, ITSKeyofSame<T, U>>;\n\n/**\n * 找出 T 與 U 當中同時存在的鍵\n * Find 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 = ITSKeyofBothSame<A, B>; // 'a'\n */\nexport type ITSKeyofBothSame<T, U> = ITSKeyofSame<T, U> | ITSKeyofSame<U, T>;\n\n/**\n * 去除 T 與 U 當中同時存在的鍵\n * Remove 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 = ITSKeyofBothDiff<A, B>; // 'b' | 'c'\n */\nexport type ITSKeyofBothDiff<T, U> = ITSKeyofDiff<T, U> | ITSKeyofDiff<U, T>;\n\n/**\n * ITSFilterKeysString - 基礎鍵過濾類型\n * 從聯合類型中排除指定的字符串鍵\n *\n * 使用範例:\n * @example\n * type AllKeys = 'log' | 'debug' | 'constructor' | 'error';\n * type PublicKeys = ITSFilterKeysString<AllKeys, 'constructor'>; // 'log' | 'debug' | 'error'\n * type SafeKeys = ITSFilterKeysString<AllKeys, 'constructor' | 'error'>; // 'log' | 'debug'\n *\n * @example\n * type ITSFilterKeysString<T, ExcludeKeys extends string = never> = T extends infer U\n * \t? U extends string\n * \t\t? U extends ExcludeKeys ? never : U\n * \t\t: never\n * \t: never;\n *\n * @see ITSExcludeFilterKeys\n */\nexport type ITSExcludeFilterKeysString<T, ExcludeKeys extends string = never> = ITSExcludeFilterKeys<T, ExcludeKeys, string>;\n\n/**\n * ITSFilterKeysPropertyKey - 支持 PropertyKey(string | number | symbol)的版本\n *\n * 使用範例:\n * @example\n * type AllKeys = 'log' | 'debug' | 'constructor' | 42 | symbol;\n * type StringKeys = ITSFilterKeysPropertyKey<AllKeys, number | symbol>; // 'log' | 'debug' | 'constructor'\n * type NoSymbol = ITSFilterKeysPropertyKey<AllKeys, symbol>; // 'log' | 'debug' | 'constructor' | 42\n *\n * @example\n * type ITSFilterKeysPropertyKey<T, ExcludeKeys extends PropertyKey = never> = T extends infer U\n * \t? U extends PropertyKey\n * \t\t? U extends ExcludeKeys ? never : U\n * \t\t: never\n * \t: never;\n *\n * @see ITSExcludeFilterKeys\n */\nexport type ITSExcludeFilterKeysPropertyKey<T, ExcludeKeys extends PropertyKey = never> = ITSExcludeFilterKeys<T, ExcludeKeys, PropertyKey>;\n\n/**\n * ITSFilterKeys - 可配置鍵類型範圍的版本\n * 第三個類型參數 `AllowedPropertyKey` 允許精確控制保留哪些類型的鍵\n *\n * 使用範例:\n * @example\n * ```\n * type AllKeys = 'log' | 'debug' | 42 | 99 | symbol;\n *\n * // 只保留 string 類型,排除 'log'\n * type StringOnly = ITSFilterKeys<AllKeys, 'log', string>; // 'debug'\n *\n * // 只保留 number 類型,不排除任何鍵\n * type NumOnly = ITSFilterKeys<AllKeys, never, number>; // 42 | 99\n *\n * // 保留 string | number,排除 'debug'\n * type Mixed = ITSFilterKeys<AllKeys, 'debug', string | number>; // 'log' | 42 | 99\n * ```\n *\n * 三代版本對比:\n * ITSFilterKeysString: 固定處理 string(最基礎)\n * ITSFilterKeysPropertyKey: 固定處理 PropertyKey(更廣泛)\n * ITSFilterKeys: 可配置 AllowedPropertyKey 參數(最靈活)\n *\n * @see ITSExtractKeysV13\n */\nexport type ITSExcludeFilterKeys<T, ExcludeKeys extends PropertyKey = never, AllowedPropertyKey extends PropertyKey = PropertyKey> = T extends infer U\n\t? U extends AllowedPropertyKey\n\t\t? U extends ExcludeKeys ? never : U\n\t\t: never\n\t: never;\n\n/**\n * ITSExtractKeysV13 - 選擇器版本(只保留匹配 IncludeKeys 的鍵)\n *\n * 與 FilterKeysV3 的邏輯差異:\n * - FilterKeysV3: `U extends ExcludeKeys ? never : U` → 排除匹配的鍵\n * - ITSExtractKeysV13: `U extends IncludeKeys ? U : never` → 只保留匹配的鍵\n *\n * 設計理念:\n * ---------------------------\n * ExtractKeyofV12 系列的核心需求是「篩選符合 V 類型的鍵」。\n * FilterKeys 系列的設計是「排除」思維( ExcludeKeys ),\n * 而 ITSExtractKeysV13 採用「選擇」思維( IncludeKeys ),更符合鍵提取的語義。\n *\n * 與 FilterKeysV3 實作的對比:\n * - FilterKeysV3: `FilterKeysV3<T, never, V>`(用 AllowedPropertyKey 篩選,ExcludeKeys 設為 never)\n * - ITSExtractKeysV13: `ITSExtractKeysV13<T, V, PropertyKey>`(直接用 IncludeKeys 篩選)\n *\n * 使用範例與測試驗證:\n * ```typescript\n * type AllKeys = 'log' | 'debug' | 'error' | 42 | 99 | symbol;\n *\n * // 1. 篩選 string 類型鍵(等同 ExtractKeyofV12d<AllKeys, string>)\n * // ✅ 結果:'log' | 'debug' | 'error'\n * type Strings = ITSExtractKeysV13<AllKeys, string, PropertyKey>;\n *\n * // 2. 篩選 number 類型鍵\n * // ✅ 結果:42 | 99\n * type Numbers = ITSExtractKeysV13<AllKeys, number, PropertyKey>;\n *\n * // 3. 只保留特定字面量\n * // ✅ 結果:'log' | 'debug'\n * type Picked = ITSExtractKeysV13<AllKeys, 'log' | 'debug', PropertyKey>;\n *\n * // 4. 特定數字字面量(只保留 42,過濾 99)\n * // ✅ 結果:42\n * // ❌ 99 會被過濾\n * type SpecificNumber = ITSExtractKeysV13<AllKeys, 42, PropertyKey>;\n *\n * // 5. 實作 ExtractKeyofV12(輸出轉為 string)\n * // ✅ 結果:'log' | 'debug' | 'error'\n * type V12_Strings = ITSExtractKeysV13<AllKeys, string, PropertyKey> & string;\n *\n * // 6. 限制輸入類型為 string(等同 FilterKeysV3<AllKeys, never, string>)\n * // ✅ 結果:'log' | 'debug' | 'error'\n * // ❌ number/symbol 被過濾\n * type StringOnly = ITSExtractKeysV13<AllKeys, PropertyKey, string>;\n *\n * // 7. symbol 類型篩選(結果為 never)\n * // ✅ 結果:never(symbol 無法通過 PropertyKey 匹配 string/number)\n * type SymbolFiltered = ITSExtractKeysV13<AllKeys, symbol, PropertyKey>;\n * ```\n *\n * Enum 類型處理範例:\n * ------------------\n * TypeScript enum 作為類型時,需使用模板字面量 `${Enum}` 或 keyof typeof 獲取成員。\n *\n * ```typescript\n * enum LogLevel {\n * DEBUG = 'debug',\n * INFO = 'info',\n * WARN = 'warn',\n * ERROR = 'error'\n * }\n *\n * enum StatusCode {\n * OK = 200,\n * NOT_FOUND = 404,\n * ERROR = 500\n * }\n *\n * // 8. 從 enum 鍵中篩選(使用 keyof typeof)\n * // ✅ 結果:'DEBUG' | 'INFO'\n * type EnumKeys = keyof typeof LogLevel; // 'DEBUG' | 'INFO' | 'WARN' | 'ERROR'\n * type PickedEnumKeys = ITSExtractKeysV13<EnumKeys, 'DEBUG' | 'INFO', PropertyKey>;\n *\n * // 11. 數字 enum 的值篩選(使用 infer 提取數字)\n * // ✅ 結果:200 | 500\n * type StatusEnumValues = `${StatusCode}` extends `${infer N extends number}` ? N : never;\n * type PickedStatusValues = ITSExtractKeysV13<200 | 404 | 500, StatusEnumValues, PropertyKey>;\n *\n * // 12. enum 作為第二參數(IncludeKeys)使用 ITSTypeAndStringLiteral\n * // 搭配 ts-type 的 ITSTypeAndStringLiteral 可直接將 enum 轉為 string 字面量\n * // ✅ 結果:'debug' | 'error'\n * import { ITSTypeAndStringLiteral } from 'ts-type';\n * type MixedInput = ITSTypeAndStringLiteral<LogLevel> | 42 | symbol;\n * type EnumAsIncludeKeys = ITSTypeAndStringLiteral<LogLevel.DEBUG | LogLevel.ERROR>;\n * type Filtered = ITSExtractKeysV13<MixedInput, EnumAsIncludeKeys, PropertyKey>;\n * // ✅ 可賦值:LogLevel.DEBUG, LogLevel.ERROR\n * // ❌ 被過濾:'trace', 'fatal', 42, symbol\n *\n * // 13. 數字 enum 直接作為 IncludeKeys\n * // ✅ 結果:200 | 404 | 500\n * type AllCodes = 200 | 301 | 404 | 500 | 503;\n * type StatusCodes = ITSExtractKeysV13<AllCodes, StatusCode, PropertyKey>;\n * ```\n *\n * @see ITSExcludeFilterKeys\n */\nexport type ITSExtractKeysV13<T, IncludeKeys extends PropertyKey, AllowedPropertyKey extends PropertyKey = PropertyKey> = T extends infer U\n\t? U extends AllowedPropertyKey\n\t\t? U extends IncludeKeys ? U : never\n\t\t: never\n\t: never;\n\n"]}
|
package/lib/helper/infer.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"infer.js","sourceRoot":"","sources":["infer.ts"],"names":[],"mappings":";AAAA;;GAEG","sourcesContent":["/**\n * Created by user on 2019/6/12.\n */\n\n/**\n * 取得物件指定成員的值類型\n * Get the value type of a specific object member\n
|
|
1
|
+
{"version":3,"file":"infer.js","sourceRoot":"","sources":["infer.ts"],"names":[],"mappings":";AAAA;;GAEG","sourcesContent":["/**\n * Created by user on 2019/6/12.\n */\n\n/**\n * 取得物件指定成員的值類型\n * Get the value type of a specific object member\n *\n * @example\n * interface User {\n * name: string;\n * age: number;\n * }\n * type NameType = ITSValueOfMember<User, 'name'>; // string\n */\nexport type ITSValueOfMember<T, K extends keyof T> = T extends {\n\t[p in K]: infer U\n} ? U : never;\n\n/**\n * 取得具有 length 屬性之物件的 length 類型\n * Get the length type of an object with length property\n *\n * @example\n * type ArrayLike = { length: 42 };\n * type LengthType = ITSLengthOf<ArrayLike>; // 42\n */\nexport type ITSLengthOf<T extends {\n\tlength: number\n}> = ITSValueOfMember<T, 'length'>;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"intersection.js","sourceRoot":"","sources":["intersection.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * 將聯集類型轉換為交集類型\n * Convert union type to intersection type\n
|
|
1
|
+
{"version":3,"file":"intersection.js","sourceRoot":"","sources":["intersection.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * 將聯集類型轉換為交集類型\n * Convert union type to intersection type\n *\n * @see https://stackoverflow.com/questions/50374908/transform-union-type-to-intersection-type\n * @example\n * type FunctionUnion = (() => void) | ((p: string) => void);\n * type FunctionIntersection = (() => void) & ((p: string) => void);\n * type SynthesizedFunctionIntersection = ITSUnionToIntersection<FunctionUnion>\n * // type SynthesizedFunctionIntersection = (() => void) & ((p: string) => void)\n */\nexport type ITSUnionToIntersection<U> =\n\t(U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never\n"]}
|
|
@@ -30,15 +30,52 @@ export type ITSValueOf<T extends Record<any, any>> = T[keyof T];
|
|
|
30
30
|
/** 取得介面所有值的聯集類型(別名)/ Get the union type of all values in an interface (alias) */
|
|
31
31
|
export type { ITSValueOf as ITSValueOfRecord };
|
|
32
32
|
/**
|
|
33
|
-
*
|
|
34
|
-
*
|
|
33
|
+
* Extracts the keys of a type.
|
|
34
|
+
*
|
|
35
|
+
* For a union type, only keys shared by all union members are returned.
|
|
35
36
|
*
|
|
36
37
|
* @example
|
|
37
|
-
*
|
|
38
|
-
* type
|
|
39
|
-
*
|
|
38
|
+
* type A = { name: string; age: number };
|
|
39
|
+
* type B = { name: string; email: string };
|
|
40
|
+
*
|
|
41
|
+
* type Keys = ITSKeyOf<A | B>;
|
|
42
|
+
* // "name"
|
|
40
43
|
*/
|
|
41
44
|
export type ITSKeyOf<T> = keyof T;
|
|
45
|
+
/**
|
|
46
|
+
* Extracts all keys from every member of a union type.
|
|
47
|
+
*
|
|
48
|
+
* Unlike `keyof T`, this distributes over union members and returns
|
|
49
|
+
* the union of their keys.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* type A = { name: string; age: number };
|
|
53
|
+
* type B = { name: string; email: string };
|
|
54
|
+
*
|
|
55
|
+
* type Keys = ITSKeyOfUnion<A | B>;
|
|
56
|
+
* // "name" | "age" | "email"
|
|
57
|
+
*/
|
|
58
|
+
export type ITSKeyOfUnion<T> = T extends any ? keyof T : never;
|
|
59
|
+
/**
|
|
60
|
+
* Extracts the value type associated with a key from a union type.
|
|
61
|
+
*
|
|
62
|
+
* Only union members containing the specified key contribute their value type.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* type T =
|
|
66
|
+
* | { type: "a"; value: number }
|
|
67
|
+
* | { type: "b"; text: string };
|
|
68
|
+
*
|
|
69
|
+
* type Value = ITSValueOfUnion<T, "value">;
|
|
70
|
+
* // number
|
|
71
|
+
*
|
|
72
|
+
* type Text = ITSValueOfUnion<T, "text">;
|
|
73
|
+
* // string
|
|
74
|
+
*
|
|
75
|
+
* type Type = ITSValueOfUnion<T, "type">;
|
|
76
|
+
* // "a" | "b"
|
|
77
|
+
*/
|
|
78
|
+
export type ITSValueOfUnion<T, K extends ITSKeyOfUnion<T>> = T extends unknown ? K extends keyof T ? T[K] : never : never;
|
|
42
79
|
/**
|
|
43
80
|
* 取得指定鍵集合的值類型聯集
|
|
44
81
|
* Get the union of value types for a specified set of keys
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"key-value.js","sourceRoot":"","sources":["key-value.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG","sourcesContent":["/**\n * 鍵值對類型操作工具\n * Key-Value Type Manipulation Utilities\n
|
|
1
|
+
{"version":3,"file":"key-value.js","sourceRoot":"","sources":["key-value.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG","sourcesContent":["/**\n * 鍵值對類型操作工具\n * Key-Value Type Manipulation Utilities\n *\n * 提供類型安全的鍵值對操作工具,包括取得物件、Map、陣列的值類型等\n * Provides type-safe key-value pair manipulation utilities\n */\n\nimport { ITSMapLike } from '../generic';\nimport { ITSIteratorLazy } from './typeof';\nimport { ITSArrayListMaybeReadonly } from '../type/base';\n\n/**\n * 取得介面所有值的聯集類型\n * Get the union type of all values in an interface\n *\n * 類似 `Object.values()` 的類型版本,回傳物件所有值的聯集\n * Similar to the type version of `Object.values()`, returns the union of all object values\n *\n * @see https://stackoverflow.com/questions/49285864/is-there-a-valueof-similar-to-keyof-in-typescript\n *\n * @example\n * interface User {\n * name: string;\n * age: number;\n * isActive: boolean;\n * }\n * type UserValues = ITSValueOf<User>;\n * // type UserValues = string | number | boolean\n */\nexport type ITSValueOf<T extends Record<any, any>> = T[keyof T];\n\n/** 取得介面所有值的聯集類型(別名)/ Get the union type of all values in an interface (alias) */\nexport type { ITSValueOf as ITSValueOfRecord };\n\n/**\n * Extracts the keys of a type.\n *\n * For a union type, only keys shared by all union members are returned.\n *\n * @example\n * type A = { name: string; age: number };\n * type B = { name: string; email: string };\n *\n * type Keys = ITSKeyOf<A | B>;\n * // \"name\"\n */\nexport type ITSKeyOf<T> = keyof T;\n\n/**\n * Extracts all keys from every member of a union type.\n *\n * Unlike `keyof T`, this distributes over union members and returns\n * the union of their keys.\n *\n * @example\n * type A = { name: string; age: number };\n * type B = { name: string; email: string };\n *\n * type Keys = ITSKeyOfUnion<A | B>;\n * // \"name\" | \"age\" | \"email\"\n */\nexport type ITSKeyOfUnion<T> =\n\tT extends any ? keyof T : never;\n\n/**\n * Extracts the value type associated with a key from a union type.\n *\n * Only union members containing the specified key contribute their value type.\n *\n * @example\n * type T =\n * | { type: \"a\"; value: number }\n * | { type: \"b\"; text: string };\n *\n * type Value = ITSValueOfUnion<T, \"value\">;\n * // number\n *\n * type Text = ITSValueOfUnion<T, \"text\">;\n * // string\n *\n * type Type = ITSValueOfUnion<T, \"type\">;\n * // \"a\" | \"b\"\n */\nexport type ITSValueOfUnion<\n T,\n K extends ITSKeyOfUnion<T>\n> = T extends unknown\n ? K extends keyof T\n ? T[K]\n : never\n : never;\n\n/**\n * 取得指定鍵集合的值類型聯集\n * Get the union of value types for a specified set of keys\n *\n * @example\n * interface User { name: string; age: number; email: string; }\n * type PickedValues = ITSPickValueOf<User, 'name' | 'age'>;\n * // type PickedValues = string | number\n */\nexport type ITSPickValueOf<T, K extends keyof T> = ITSValueOf<Pick<T, K>>;\n\n/**\n * 迭代器值的類型\n * Iterator value type\n *\n * 從 Iterator 或 IteratorResult 類型中提取值類型\n * Extracts value type from Iterator or IteratorResult types\n *\n * @example\n * type IteratorResultType = ITSValueOfIterator<IteratorResult<string>>;\n * // type IteratorResultType = string[]\n */\nexport type ITSValueOfIterator<T extends ITSIteratorLazy<any>> =\n\t(T extends Iterator<infer U> ? U :\n\t\tT extends IteratorResult<infer U> ? U :\n\t\t\tany)[]\n\t;\n\n/**\n * Map 類型的所有值類型\n * All value types of Map type\n *\n * 提取 Map 類型的泛型參數 V,回傳 V[]\n * Extracts the generic parameter V from Map type, returns V[]\n *\n * @example\n * type MapValues = ITSValueOfMap<Map<string, number>>;\n * // type MapValues = number[]\n */\nexport type ITSValueOfMap<T extends ITSMapLike<any, any>> =\n\tT extends ITSMapLike<any, infer U> ? U[] :\n\t\tany[]\n\t;\n\n/**\n * 陣列的元素類型\n * Array element type\n *\n * 從陣列類型中提取元素類型,支援唯讀陣列和可讀寫陣列\n * Extracts element type from array type, supporting both readonly and writable arrays\n *\n * @example\n * type ArrayElement = ITSValueOfArray<string[]>;\n * // type ArrayElement = string\n *\n * @example\n * type ReadonlyArrayElement = ITSValueOfArray<readonly string[]>;\n * // type ReadonlyArrayElement = string\n */\nexport type ITSValueOfArray<T extends ITSArrayListMaybeReadonly<any>> =\n\tT extends readonly (infer U)[]\n\t\t? U : T extends (infer U)[]\n\t\t\t? U : never\n\t;\n\n/**\n * 類陣列的元素類型\n * Array-like element type\n *\n * 從類陣列物件(如 ArrayLike<T>)中提取元素類型\n * Extracts element type from array-like objects (such as ArrayLike<T>)\n *\n * @example\n * type ArrayLikeElement = ITSValueOfArrayLike<ArrayLike<string>>;\n * // type ArrayLikeElement = string\n */\nexport type ITSValueOfArrayLike<T extends ITSArrayListMaybeReadonly<any> | ArrayLike<any>> =\n\tT extends ITSArrayListMaybeReadonly<T>\n\t\t? ITSValueOfArray<T> : T extends ArrayLike<infer U>\n\t\t\t? U : never\n\t;\n"]}
|
package/lib/helper/number.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"number.js","sourceRoot":"","sources":["number.ts"],"names":[],"mappings":"","sourcesContent":["import { ITSToStringLiteral } from './string';\n\n/**\n * 將數字或 bigint 轉換為字串字面量類型\n * Convert number or bigint to string literal type\n
|
|
1
|
+
{"version":3,"file":"number.js","sourceRoot":"","sources":["number.ts"],"names":[],"mappings":"","sourcesContent":["import { ITSToStringLiteral } from './string';\n\n/**\n * 將數字或 bigint 轉換為字串字面量類型\n * Convert number or bigint to string literal type\n *\n * @example\n * type NumType = ITSNumberString<42>; // '42'\n */\nexport type ITSNumberString<N extends number | bigint = number> = ITSToStringLiteral<N>;\n\n/**\n * 從字串中解包出數字類型\n * Unpack number type from string\n *\n * @example\n * type Test = ITSNumberString<123>;\n * type Result = ITSUnpackNumberString<Test>; // 123\n */\nexport type ITSUnpackNumberString<S extends string, R = never> = S extends ITSNumberString<infer N> ? N : R;\n\n/**\n * 數字值類型:數字或字串\n * Number value type: number or string\n */\nexport type ITSNumberValue = number | string;\n\n/**\n * 數字值類型2:數字或數字字串\n * Number value type 2: number or number string\n */\nexport type ITSNumberValue2 = number | ITSNumberString;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"readonly.js","sourceRoot":"","sources":["readonly.ts"],"names":[],"mappings":"","sourcesContent":["import { ITSValueOfArray } from './key-value';\n\n/**\n * 將類型的所有屬性設為唯讀且可選\n * Make all properties of a type readonly and optional\n
|
|
1
|
+
{"version":3,"file":"readonly.js","sourceRoot":"","sources":["readonly.ts"],"names":[],"mappings":"","sourcesContent":["import { ITSValueOfArray } from './key-value';\n\n/**\n * 將類型的所有屬性設為唯讀且可選\n * Make all properties of a type readonly and optional\n *\n * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html\n * @example\n * interface User {\n * name: string;\n * age: number;\n * }\n * type ReadonlyPartialUser = ITSReadonlyPartial<User>;\n * // { readonly name?: string; readonly age?: number; }\n */\nexport type ITSReadonlyPartial<T> = {\n\treadonly [P in keyof T]?: T[P]\n};\n\n/**\n * 將類型的所有屬性設為可讀寫\n * Make all properties of a type writable\n *\n * @example\n * interface User {\n * readonly name: string;\n * readonly age: number;\n * }\n * type WritableUser = ITSWriteable<User>;\n * // { name: string; age: number; }\n */\nexport type ITSWriteable<T> = ITSWriteablePick<T, keyof T>;\n\n/**\n * 將指定屬性設為可讀寫\n * Make specified properties writable\n *\n * @example\n * interface User {\n * readonly name: string;\n * readonly age: number;\n * readonly email: string;\n * }\n * type PartialWritable = ITSWriteablePick<User, 'name' | 'age'>;\n * // { name: string; age: number; readonly email?: string; }\n */\nexport type ITSWriteablePick<T, K extends keyof T = keyof T> = {\n\t-readonly [P in K]: T[P];\n};\n\n/**\n * 將指定屬性設為唯讀\n * Make specified properties readonly\n *\n * @example\n * interface User {\n * name: string;\n * age: number;\n * }\n * type ReadonlyPartial = ITSReadonlyPick<User, 'name'>;\n * // { readonly name: string; age?: number; }\n */\nexport type ITSReadonlyPick<T, K extends keyof T = keyof T> = {\n\treadonly [P in K]: T[P];\n};\n\n/**\n * 保留指定屬性可讀寫,其他屬性不變\n * Keep specified properties writable, other properties unchanged\n *\n * @example\n * interface User {\n * readonly name: string;\n * readonly age: number;\n * email: string;\n * }\n * type Result = ITSWriteableWith<User, 'email'>;\n * // { name: readonly string; age: readonly number; email: string; }\n */\nexport type ITSWriteableWith<T, K extends keyof T = keyof T> = Omit<T, K> & ITSWriteablePick<T, K>;\n\n/**\n * 保留指定屬性唯讀,其他屬性不變\n * Keep specified properties readonly, other properties unchanged\n *\n * @example\n * interface User {\n * name: string;\n * age: number;\n * email: string;\n * }\n * type Result = ITSReadonlyWith<User, 'name' | 'age'>;\n * // { readonly name: string; readonly age: number; email: string; }\n */\nexport type ITSReadonlyWith<T, K extends keyof T = keyof T> = Omit<T, K> & ITSReadonlyPick<T, K>;\n\n/**\n * 將唯讀陣列轉換為可讀寫陣列\n * Convert readonly array to writable array\n *\n * @example\n * type ReadonlyArray = readonly [1, 2, 3];\n * type WritableArray = ITSReadonlyToWriteableArray<ReadonlyArray>;\n * // [1, 2, 3]\n */\nexport type ITSReadonlyToWriteableArray<T extends readonly any[]> = Omit<T, keyof any[]> & ITSValueOfArray<T>[] & {\n\t-readonly [P in number | 'length']: T[P]\n};\n\n/**\n * 深層移除唯讀修飾符\n * Deeply remove readonly modifier\n *\n * @example\n * interface Nested {\n * readonly user: {\n * readonly name: string;\n * readonly profile: {\n * readonly age: number;\n * };\n * };\n * }\n * type WritableNested = ITSWriteableDeep<Nested>;\n * // { user: { name: string; profile: { age: number; } } }\n */\nexport type ITSWriteableDeep<T, K extends keyof T = keyof T> = T extends Record<any, any> ? {\n\t-readonly [P in K]: ITSWriteableDeep<T[P]>;\n} : T;\n\n/**\n * 深層添加唯讀修飾符\n * Deeply add readonly modifier\n *\n * @example\n * interface Nested {\n * user: {\n * name: string;\n * profile: {\n * age: number;\n * };\n * };\n * }\n * type ReadonlyNested = ITSReadonlyDeep<Nested>;\n * // { readonly user: { readonly name: string; readonly profile: { readonly age: number; } } }\n */\nexport type ITSReadonlyDeep<T, K extends keyof T = keyof T> =\n\tT extends Record<any, any> ? {\n\t\treadonly [P in K]: ITSReadonlyDeep<T[P]>;\n\t} : T;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enum.js","sourceRoot":"","sources":["enum.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG","sourcesContent":["/**\n * 列舉類型工具\n * Enum Type Utilities\n
|
|
1
|
+
{"version":3,"file":"enum.js","sourceRoot":"","sources":["enum.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG","sourcesContent":["/**\n * 列舉類型工具\n * Enum Type Utilities\n *\n * 提供列舉類型的操作工具\n * Provides enum type manipulation utilities\n */\n\nimport { ITSEnumLike, ITSNumberEnumLike } from '../../type/record/enum';\nimport { ITSValueOf } from '../key-value';\nimport { ITSStringInferToNumber } from '../string/infer';\nimport { ITSToStringLiteral } from '../string';\n\n/**\n * 從列舉類型中排除指定值\n * Exclude specified values from enum type\n *\n * @example\n * enum Color { Red, Green, Blue }\n * type ExcludedRed = ITSExcludeEnumValue<Color, Color.Red>;\n * // type ExcludedRed = Color.Green | Color.Blue\n */\nexport type ITSExcludeEnumValue<Enum extends ITSEnumLike, U extends ITSValueOf<Enum>> = Exclude<ITSValueOf<Enum>, U>;\n\n/**\n * 從列舉類型中提取指定值\n * Extract specified values from enum type\n *\n * @example\n * enum Color { Red, Green, Blue }\n * type ExtractedRed = ITSExtractEnumValue<Color, Color.Red>;\n * // type ExtractedRed = Color.Red\n */\nexport type ITSExtractEnumValue<Enum extends ITSEnumLike, U extends ITSValueOf<Enum>> = Extract<ITSValueOf<Enum>, U>;\n\n/**\n * 將數字列舉轉換為純數字類型\n * Convert number enum to pure number type\n *\n * @example\n * enum Status { Active = 1, Inactive = 2 }\n * type NumStatus = ITSNumberEnumToNumber<Status>;\n * // type NumStatus = 1 | 2\n */\nexport type ITSNumberEnumToNumber<T extends number> = ITSStringInferToNumber<ITSToStringLiteral<Extract<T, number>>>\n\n/**\n * 數字列舉與數字的聯合類型\n * Union type of number enum and number\n *\n * @example\n * enum Status { Active = 1, Inactive = 2 }\n * type StatusValue = ITSNumberEnumAndNumber<Status>;\n * // type StatusValue = 1 | 2 | number\n */\nexport type ITSNumberEnumAndNumber<T extends number> = Extract<T, number> | ITSNumberEnumToNumber<T>\n"]}
|