@inzero/ts-ut 0.1.2 → 0.3.0
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 +47 -1
- package/README.md +2 -2
- package/dist/_internal.d.ts +24 -4
- package/dist/dict/extract.d.ts +27 -0
- package/dist/dict/get.d.ts +50 -10
- package/dist/dict/index.d.ts +9 -8
- package/dist/dict/keyof.d.ts +66 -26
- package/dist/dict/merge.d.ts +117 -39
- package/dist/dict/modifiers.d.ts +10 -10
- package/dist/dict/pick.d.ts +11 -15
- package/dist/dict/prefix.d.ts +13 -14
- package/dist/dict/rename.d.ts +4 -4
- package/dist/dict/utils.d.ts +7 -8
- package/dist/func/index.d.ts +1 -1
- package/dist/func/utils.d.ts +2 -2
- package/dist/index.d.ts +7 -7
- package/dist/list/index.d.ts +1 -1
- package/dist/number/index.d.ts +2 -2
- package/dist/other/index.d.ts +2 -2
- package/dist/other/utils.d.ts +33 -8
- package/dist/string/index.d.ts +5 -5
- package/dist/string/utils.d.ts +1 -1
- package/dist/tuple/index.d.ts +2 -2
- package/dist/tuple/push.d.ts +5 -21
- package/package.json +12 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,51 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.3.0] 2026-11-10
|
|
4
|
+
|
|
5
|
+
This update focuses on:
|
|
6
|
+
|
|
7
|
+
- Loosening types: Allowing more flexible inputs.
|
|
8
|
+
- separation of concern: Drop support for tuples in dict utils.
|
|
9
|
+
- Bug fixes: Fixing various bugs and inconsistencies.
|
|
10
|
+
|
|
11
|
+
### Removed
|
|
12
|
+
|
|
13
|
+
- Removed dict keyof utils: `Keyof<T>`, `Valueof<T>`, `EntryOf<T>`, `KeyofDeep<T>`, `KeyofType<T, U>`, `KeyofTypeDeep<T, U>`.
|
|
14
|
+
- Removed `As<U, T>` internal utility.
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
|
|
18
|
+
- Added dict extract utils: `ExtractByValue<T, M>` and `ExtractByExactValue<T, M>`.
|
|
19
|
+
- Added dict keyof utils: `Keys<T>`, `Values<T>`, `Entries<T>`, `KeysDeep<T>`, `KeysDeep<T>`, `KeysOfType<T, U>`, `KeysWithType<T, U>`, `KeysWithExactType<T, U>`, `KeysOfTypeDeep<T, U>`, `KeysWithTypeDeep<T, U>`, `KeysWithExactType<T, U>`.
|
|
20
|
+
- Added dict merge utils: `MergeUnionDeep<A, B, P>`, `MergeIntDeep<A, B, P>`, `MergePatchDeep<A, B, P>`, `MergeDiffDeep<A, B>`, `MergeSymDiffDeep<A, B>`.
|
|
21
|
+
- Added dict `Set<T, K, V>` and `SetDeep<T, K, V>`.
|
|
22
|
+
- Added `Range`, `NonNullish<T>`, `Nullish<T>`, `Self<T>` other utils.
|
|
23
|
+
- Added `IsNumber<T>` internal utility.
|
|
24
|
+
|
|
25
|
+
### Changed
|
|
26
|
+
|
|
27
|
+
- Drop support for tuples in `PrettifyDeep<T>`.
|
|
28
|
+
- Loosen up `Known<T>` and drop support for tuples.
|
|
29
|
+
- Loosen up existing dict `Merge` utils.
|
|
30
|
+
- Rename merge method `"symDiff"` to `"sym-diff"` in `Merge<A, B, M>`.
|
|
31
|
+
- Loosen up `RequiredKey<T, K>`, `PartialKey<T, K>`, `ReadonlyKey<T, K>`, `PartialKey<T, K>`.
|
|
32
|
+
- Loosen up `LoosePick<T, K>`, `StrictPick<T, K>`, `LooseOmit<T, K>`, `StrictOmit<T, K>`.
|
|
33
|
+
- Loosen up dict prefix utils and drop support for prefixing with numbers.
|
|
34
|
+
- Loosen up `RenameKey<T, K, N>` and `RenameKeys<T, M>`.
|
|
35
|
+
- Loosen up `Get<T, K>` and `GetDeep<T, K, O>` and drop support for tuples, and add `O` template to choose whether to preserve `undefined` if one of the parents is optional..
|
|
36
|
+
- Loosen up `Suggest<T>`.
|
|
37
|
+
|
|
38
|
+
### Fixed
|
|
39
|
+
|
|
40
|
+
- Loosen up `RequiredDeep<T>`, `PartialDeep<T>`, `ReadonlyDeep<T>`, `MutableDeep<T>` so the recursion doesn't break on unions.
|
|
41
|
+
- Fixed `LoosePick<T, K>` and `StrictPick<T, K>` not picking keys when `T` has an index signature.
|
|
42
|
+
|
|
43
|
+
## [0.2.0] 2026-10-19
|
|
44
|
+
|
|
45
|
+
### Added
|
|
46
|
+
|
|
47
|
+
- Expose internal types through `@inzero/ts-ut/_internal`
|
|
48
|
+
|
|
3
49
|
## [0.1.2] 2026-10-19
|
|
4
50
|
|
|
5
51
|
### Fixed
|
|
@@ -20,4 +66,4 @@
|
|
|
20
66
|
|
|
21
67
|
## [0.0.1] 2026-10-15
|
|
22
68
|
|
|
23
|
-
First release.
|
|
69
|
+
First release.
|
package/README.md
CHANGED
|
@@ -9,10 +9,10 @@ import { Param } from "@inzero/ts-ut";
|
|
|
9
9
|
import { GetDeep } from "@inzero/ts-ut/dict"; // Or category-specific imports
|
|
10
10
|
|
|
11
11
|
type Handler = (foo: string, bar: number) => boolean;
|
|
12
|
-
type LastParam = Param<Handler, -1>; // number
|
|
12
|
+
type LastParam = Param<Handler, -1>; // number
|
|
13
13
|
|
|
14
14
|
type Config = { a: 1; b: { c: 4 } };
|
|
15
|
-
type Target = GetDeep<Config, "b.c">; // 4
|
|
15
|
+
type Target = GetDeep<Config, "b.c">; // 4
|
|
16
16
|
```
|
|
17
17
|
|
|
18
18
|
## Installation
|
package/dist/_internal.d.ts
CHANGED
|
@@ -1,7 +1,27 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
1
|
+
import type { Mutable } from "./dict";
|
|
2
|
+
import type { List, ROList } from "./list";
|
|
3
|
+
import type { Decrement, Increment, ToNumber } from "./number";
|
|
4
|
+
import type { AreEqual, IsAny, IsNever, IsSubtype, IsUnknown } from "./other";
|
|
5
|
+
import type { EnsurePrefix, Stringify } from "./string";
|
|
4
6
|
export type IsNegative<T extends string | number> = T extends T ? StartsWith<Stringify<T>, "-"> extends true ? true : false : never;
|
|
7
|
+
export type IsNumber<T extends string> = ToNumber<T> extends number ? true : false;
|
|
5
8
|
export type StartsWith<T extends string, P extends string> = T extends `${P}${string}` ? true : false;
|
|
6
|
-
export type As<T, U> = Extract<U, T>;
|
|
7
9
|
export type NormalizeIndex<I extends number, L extends number | ROList> = (L extends ROList ? L["length"] : L) extends infer Length extends number ? IsNegative<I> extends false ? IsNegative<Decrement<I, Length>> extends false ? Decrement<Length> : I : Increment<Length, I> extends infer Result extends number ? IsNegative<Result> extends true ? 0 : Result : never : never;
|
|
10
|
+
export type NegAbs<T extends number> = T extends 0 ? 0 : ToNumber<EnsurePrefix<Stringify<T>, "-">>;
|
|
11
|
+
export type FOE<T extends ROList, _C extends List = []> = T extends Required<T> ? -1 : T extends readonly [unknown, ...infer R extends ROList] ? FOE<R, [..._C, any]> : _C["length"];
|
|
12
|
+
export type SetFOE<T extends ROList, I extends number> = _SetFOE<T, NormalizeIndex<I, T["length"]>>;
|
|
13
|
+
export type _SetFOE<T extends ROList, I extends number, _I extends List = []> = T extends readonly [
|
|
14
|
+
infer F,
|
|
15
|
+
...infer R
|
|
16
|
+
] ? I extends _I["length"] ? [F?, ...Partial<R>] : [F, ..._SetFOE<R, I, [..._I, any]>] : T;
|
|
17
|
+
export type MatchTuple<T extends ROList, R extends ROList, D extends number = 0> = MatchRO<MatchFOI<T, R, D>, R>;
|
|
18
|
+
export type MatchFOI<T extends ROList, R extends ROList, D extends number = 0> = FOE<R> extends -1 ? BetterRequired<T> : SetFOE<BetterRequired<T>, Increment<FOE<R>, D>> extends infer R extends ROList ? R : never;
|
|
19
|
+
export type MatchRO<T extends ROList, R extends ROList> = Readonly<R> extends R ? Readonly<T> : Mutable<T>;
|
|
20
|
+
type BetterRequired<T> = T extends T ? Required<T> : never;
|
|
21
|
+
export type IsTuple<T> = T extends T ? Required<T> extends readonly [] | readonly [unknown, ...unknown[]] ? true : false : never;
|
|
22
|
+
export type IsPrimitive<T> = IsAny<T> | IsUnknown<T> | IsNever<T> extends false ? _IsPrimitive<_Wrap<T>> : false;
|
|
23
|
+
type _IsPrimitive<T> = T extends T ? AreEqual<T, [string]> | AreEqual<T, [number]> | AreEqual<T, [boolean]> extends false ? false : true : never;
|
|
24
|
+
type _Wrap<T> = (T extends T ? [T] : never) extends infer R ? IsSubtype<[true], R> | IsSubtype<[false], R> extends true ? [boolean] | Exclude<R, [boolean]> : R : never;
|
|
25
|
+
declare const __TS_UT: unique symbol;
|
|
26
|
+
export type TS_UT = typeof __TS_UT;
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { AreEqual, HasType } from "../other";
|
|
2
|
+
import type { Keys, Values } from "./keyof";
|
|
3
|
+
import type { Dict } from "./utils";
|
|
4
|
+
/**
|
|
5
|
+
* Extracts union members whose specified properties contain at least one of the specified value types.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* type Obj = { a: string } | { a: number | boolean } | { a: null };
|
|
9
|
+
*
|
|
10
|
+
* type R1 = Extract<Obj, { a: string | number }>; // { a: string }
|
|
11
|
+
* type R2 = ExtractByValue<Obj, { a: string | number }>; // { a: string } | { a: number | boolean }
|
|
12
|
+
*/
|
|
13
|
+
export type ExtractByValue<T, M extends Partial<Dict<Keys<T>>>> = T extends T ? Values<{
|
|
14
|
+
[P in keyof M]: P extends keyof T ? HasType<T[P], M[P]> : false;
|
|
15
|
+
}> extends true ? T : never : never;
|
|
16
|
+
/**
|
|
17
|
+
* Extracts union members whose specified properties exactly match the specified value types.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* type Obj = { a: string } | { a: string | boolean } | { a: null };
|
|
21
|
+
*
|
|
22
|
+
* type R1 = Extract<Obj, { a: string | boolean }>; // { a: string } | { a: string | boolean }
|
|
23
|
+
* type R2 = ExtractByExactValue<Obj, { a: string | boolean }>; // { a: string | boolean }
|
|
24
|
+
*/
|
|
25
|
+
export type ExtractByExactValue<T, M extends Partial<Dict<Keys<T>>>> = T extends T ? Values<{
|
|
26
|
+
[P in keyof M]: P extends keyof T ? AreEqual<T[P], M[P]> : false;
|
|
27
|
+
}> extends true ? T : never : never;
|
package/dist/dict/get.d.ts
CHANGED
|
@@ -1,23 +1,63 @@
|
|
|
1
|
-
import type { ROList } from "../list";
|
|
2
1
|
import type { IsNever } from "../other";
|
|
3
|
-
import type {
|
|
4
|
-
import type {
|
|
2
|
+
import type { Stringify } from "../string";
|
|
3
|
+
import type { Keys, KeysDeep, Values } from "./keyof";
|
|
4
|
+
import type { Merge } from "./merge";
|
|
5
|
+
import type { Dict, Known, Prettify, SuggestKey } from "./utils";
|
|
5
6
|
/**
|
|
6
7
|
* An alias for `Obj['Key']`.
|
|
7
8
|
*
|
|
8
9
|
* @example
|
|
9
10
|
* type R1 = Get<{ a: 1, b: 2 }, 'a'>; // 1
|
|
10
11
|
*/
|
|
11
|
-
export type Get<T
|
|
12
|
+
export type Get<T, K extends SuggestKey<Keys<Known<T>>>> = T extends T ? K extends keyof T ? T[K] : never : never;
|
|
13
|
+
/**
|
|
14
|
+
* Set a value in an object.
|
|
15
|
+
*
|
|
16
|
+
* @remarks If the key doesn't exist, it will be added.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* type R1 = Set<{ readonly c?: 1 }, 'c', 4>; // { readonly c?: 4 }
|
|
20
|
+
*
|
|
21
|
+
* type R2 = Set<{ a: 1 }, 'b', 2>; // { a: 1, b: 2 }
|
|
22
|
+
*/
|
|
23
|
+
export type Set<T, K extends SuggestKey<Keys<Known<T>>>, V> = T extends T ? Prettify<{
|
|
24
|
+
[P in K]: V;
|
|
25
|
+
} & {
|
|
26
|
+
[P in keyof T]: P extends K ? V : T[P];
|
|
27
|
+
}> : never;
|
|
12
28
|
/**
|
|
13
29
|
* Get a deeply nested value using dot-notation.
|
|
14
30
|
*
|
|
31
|
+
* @remarks Set `O` to `true` to preserve `undefined` if one of the parents is optional.
|
|
32
|
+
*
|
|
15
33
|
* @example
|
|
16
|
-
* type
|
|
34
|
+
* type Obj = { a: { b?: { c: 1 } } };
|
|
35
|
+
*
|
|
36
|
+
* type R1 = GetDeep<Obj, 'a.b.c'>; // 1
|
|
37
|
+
* type R2 = GetDeep<Obj, 'a.b.c', true>; // 1 | undefined
|
|
38
|
+
*/
|
|
39
|
+
export type GetDeep<T, K extends SuggestKey<KeysDeep<Known<T>>, true>, O extends boolean = false> = T extends T ? K extends K ? Next<K> extends [infer N extends string, infer R] ? _GetDeep<T, N, R, O> : never : never : never;
|
|
40
|
+
type _GetDeep<T, N extends string, R, O extends boolean> = Values<{
|
|
41
|
+
[P in keyof (O extends true ? T : Required<T>)]: P extends N ? IsNever<R> extends true ? T[P & keyof T] : GetDeep<T[P & keyof T], R & string, O> : never;
|
|
42
|
+
}>;
|
|
43
|
+
/**
|
|
44
|
+
* Set a deeply nested value using dot-notation.
|
|
45
|
+
*
|
|
46
|
+
* @remarks
|
|
47
|
+
* - If a parent key doesn't exist, it will be created as an empty object.
|
|
48
|
+
* - If a parent key is not an object type, it won't be modified.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* type R1 = SetDeep<{ a: { b: { c: 1 } } }, 'a.b.c', 4>; // { a: { b: { c: 4 } } }
|
|
52
|
+
*
|
|
53
|
+
* type R2 = SetDeep<{}, 'a.b.c', 4>; // { a: { b: { c: 4 } } }
|
|
54
|
+
* type R3 = SetDeep<{ a: boolean }, 'a.b.c', 4>; // { a: boolean }
|
|
17
55
|
*/
|
|
18
|
-
export type
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
56
|
+
export type SetDeep<T, K extends SuggestKey<KeysDeep<Known<T>>, true>, V> = T extends T ? K extends K ? Next<K> extends [infer N extends string, infer R] ? Prettify<_SetDeep<Merge<{
|
|
57
|
+
[P in N]: IsNever<R> extends true ? V : {};
|
|
58
|
+
}, T>, N, R, V>> : never : never : never;
|
|
59
|
+
type _SetDeep<T, N extends string, R, V> = {
|
|
60
|
+
[P in keyof T]: P extends N ? IsNever<R> extends true ? V : Exclude<T[P], Dict> | SetDeep<Extract<T[P], Dict>, R & string, V> : T[P];
|
|
61
|
+
};
|
|
62
|
+
type Next<K extends string | number> = Stringify<K> extends `${infer N extends string}.${infer R extends string}` ? [N, R] : [K, never];
|
|
23
63
|
export {};
|
package/dist/dict/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
7
|
-
export * from
|
|
8
|
-
export * from
|
|
1
|
+
export * from './extract';
|
|
2
|
+
export * from './get';
|
|
3
|
+
export * from './keyof';
|
|
4
|
+
export * from './merge';
|
|
5
|
+
export * from './modifiers';
|
|
6
|
+
export * from './pick';
|
|
7
|
+
export * from './prefix';
|
|
8
|
+
export * from './rename';
|
|
9
|
+
export * from './utils';
|
package/dist/dict/keyof.d.ts
CHANGED
|
@@ -1,51 +1,91 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { AreEqual, HasType } from "../other";
|
|
2
|
+
import type { Stringify } from "../string";
|
|
3
|
+
import type { Dict } from "./utils";
|
|
3
4
|
/**
|
|
4
|
-
*
|
|
5
|
+
* Returns all possible keys.
|
|
5
6
|
*
|
|
6
7
|
* @example
|
|
7
|
-
* type Config = { foo: string, bar: number
|
|
8
|
+
* type Config = { foo: string, bar: number } | { c: 4 };
|
|
8
9
|
*
|
|
9
|
-
* type R1 =
|
|
10
|
+
* type R1 = keyof Config; // never
|
|
11
|
+
* type R2 = Keys<Config>; // 'foo' | 'bar' | 'c'
|
|
10
12
|
*/
|
|
11
|
-
export type
|
|
13
|
+
export type Keys<T> = T extends T ? keyof T : never;
|
|
12
14
|
/**
|
|
13
|
-
*
|
|
15
|
+
* Returns all possible values.
|
|
14
16
|
*
|
|
15
17
|
* @example
|
|
16
|
-
* type Config = { foo: string
|
|
18
|
+
* type Config = { foo: string } | { c: 4 };
|
|
17
19
|
*
|
|
18
|
-
* type R1 =
|
|
20
|
+
* type R1 = Values<Config>; // string | 4
|
|
19
21
|
*/
|
|
20
|
-
export type
|
|
22
|
+
export type Values<T> = T extends T ? T[keyof T] : never;
|
|
21
23
|
/**
|
|
22
|
-
* Returns
|
|
24
|
+
* Returns all possible entries.
|
|
23
25
|
*
|
|
24
|
-
* @
|
|
26
|
+
* @example
|
|
27
|
+
* type Config = { foo: string, bar: number } | { c: 4 };
|
|
28
|
+
*
|
|
29
|
+
* type R1 = Entries<Config>; // ["foo" | "bar", string | number] | ["c", 4]
|
|
25
30
|
*/
|
|
26
|
-
export type
|
|
31
|
+
export type Entries<T> = T extends T ? [Keys<T>, Values<T>] : never;
|
|
27
32
|
/**
|
|
28
|
-
* A deep version of {@linkcode
|
|
33
|
+
* A deep version of {@linkcode Keys}.
|
|
34
|
+
*
|
|
35
|
+
* @remarks `R` controls whether to return leaf keys, parent keys, or both (default).
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* type Config = { foo: string, bar: { baz: number } };
|
|
39
|
+
*
|
|
40
|
+
* type R1 = KeysDeep<Config> // "foo" | "bar" | "bar.baz"
|
|
41
|
+
* type R2 = KeysDeep<Config, "leaf"> // "foo" | "bar.baz"
|
|
42
|
+
* type R3 = KeysDeep<Config, "parent"> // "bar"
|
|
29
43
|
*/
|
|
30
|
-
export type
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}[T extends ROList ? number & keyof T : keyof T];
|
|
44
|
+
export type KeysDeep<T, R extends "leaf" | "parent" | "both" = "both", _K extends string = ""> = T extends T ? Values<{
|
|
45
|
+
[P in keyof T as P extends string | number ? P : never]-?: T[P] extends infer TP ? Next<P & (string | number), _K> extends infer N extends string ? TP extends Dict ? (R extends "leaf" ? never : N) | KeysDeep<TP, R, N> : R extends "parent" ? never : N : never : never;
|
|
46
|
+
}> : never;
|
|
34
47
|
/**
|
|
35
|
-
*
|
|
48
|
+
* Returns the keys of `T` whose values are assignable to `U`.
|
|
36
49
|
*
|
|
37
50
|
* @example
|
|
38
51
|
* type R1 = KeyofType<{ a: 1, b: "b", c: 4 }, number>; // "a" | "c"
|
|
52
|
+
* type R2 = KeyofType<{ a: 1, [x: string]: number }, number>; // string | number
|
|
39
53
|
*/
|
|
40
|
-
export type
|
|
54
|
+
export type KeysOfType<T, U> = T extends T ? keyof {
|
|
41
55
|
[P in keyof T as T[P] extends U ? P : never]: T[P];
|
|
42
56
|
} : never;
|
|
43
57
|
/**
|
|
44
|
-
*
|
|
58
|
+
* Returns the keys of `T` whose values have one of the types in `U`.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* type Obj = { a: number, b: boolean | number };
|
|
62
|
+
*
|
|
63
|
+
* type R1 = KeysOfType<Obj, number>; // "a"
|
|
64
|
+
* type R2 = KeysWithType<Obj, number>; // "a" | "b"
|
|
65
|
+
*/
|
|
66
|
+
export type KeysWithType<T, U> = T extends T ? keyof {
|
|
67
|
+
[P in keyof T as HasType<T[P], U> extends true ? P : never]: T[P];
|
|
68
|
+
} : never;
|
|
69
|
+
export type KeysWithExactType<T, U> = T extends T ? keyof {
|
|
70
|
+
[P in keyof T as AreEqual<T[P], U> extends true ? P : never]: T[P];
|
|
71
|
+
} : never;
|
|
72
|
+
/**
|
|
73
|
+
* A deep version of {@linkcode KeysOfType}.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* type Config = { foo: string, bar: { baz: number } };
|
|
77
|
+
*
|
|
78
|
+
* type R1 = KeysOfTypeDeep<Config, string> // "foo"
|
|
79
|
+
* type R2 = KeysOfTypeDeep<Config, number> // "bar.baz"
|
|
45
80
|
*/
|
|
46
|
-
export type
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
81
|
+
export type KeysOfTypeDeep<T, U, _K extends string = ""> = T extends T ? Values<{
|
|
82
|
+
[P in keyof T as T[P] extends U ? P : HasType<T[P], Dict> extends true ? P : never]-?: T[P] extends infer TK ? Next<P & string, _K> extends infer N extends string ? HasType<TK, Dict> extends true ? KeysOfTypeDeep<Extract<TK, Dict>, U, N> : N : never : never;
|
|
83
|
+
}> : never;
|
|
84
|
+
export type KeysWithTypeDeep<T, U, _K extends string = ""> = T extends T ? Values<{
|
|
85
|
+
[P in keyof T as HasType<T[P], U | Dict> extends true ? P : never]-?: T[P] extends infer TK ? Next<P & string, _K> extends infer N extends string ? HasType<TK, Dict> extends true ? KeysWithTypeDeep<Extract<TK, Dict>, U, N> : N : never : never;
|
|
86
|
+
}> : never;
|
|
87
|
+
export type KeysWithExactTypeDeep<T, U, _K extends string = ""> = T extends T ? Values<{
|
|
88
|
+
[P in keyof T as AreEqual<T[P], U> extends true ? P : HasType<T[P], Dict> extends true ? P : never]-?: T[P] extends infer TK ? Next<P & string, _K> extends infer N extends string ? HasType<TK, Dict> extends true ? KeysWithExactTypeDeep<Extract<TK, Dict>, U, N> : N : never : never;
|
|
89
|
+
}> : never;
|
|
90
|
+
type Next<K extends string | number, _K extends string> = _K extends "" ? Stringify<K> : `${_K}.${K}`;
|
|
51
91
|
export {};
|
package/dist/dict/merge.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { HasType } from "../other";
|
|
2
|
+
import type { Dict, Prettify, PrettifyDeep } from "./utils";
|
|
2
3
|
/**
|
|
3
|
-
* Merge two objects
|
|
4
|
+
* Merge two objects, keeping all keys from both. On conflict, `B` wins.
|
|
4
5
|
*
|
|
5
|
-
* @remarks
|
|
6
|
+
* @remarks Set `P` to true to use `A`'s modifiers.
|
|
6
7
|
*
|
|
7
8
|
* @example
|
|
8
9
|
* type A = { readonly a: 1, b: 2 };
|
|
@@ -11,36 +12,62 @@ import type { Dict, Prettify } from "./utils";
|
|
|
11
12
|
* type AB1 = MergeUnion<A, B>; // { a: "<>", b: 2, c: 4 }
|
|
12
13
|
* type AB2 = MergeUnion<A, B, true>; // { readonly a: "<>", b: 2, c: 4 }
|
|
13
14
|
*/
|
|
14
|
-
export type MergeUnion<A
|
|
15
|
-
type _MergeUnion<A
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
export type MergeUnion<A, B, P extends boolean = false> = A extends A ? B extends B ? Prettify<P extends true ? _MergeUnionPreserve<A, B> : _MergeUnion<A, B>> : never : never;
|
|
16
|
+
type _MergeUnion<A, B> = _MergeInt<A, B> & MergeSymDiff<A, B>;
|
|
17
|
+
type _MergeUnionPreserve<A, B> = _MergeIntPreserve<A, B> & MergeSymDiff<A, B>;
|
|
18
|
+
/**
|
|
19
|
+
* Merge two objects deeply, keeping all keys from both. On conflict, values are recursively merged if both are objects, otherwise `B` wins.
|
|
20
|
+
*
|
|
21
|
+
* @remarks
|
|
22
|
+
* - Set `P` to true to use `A`'s modifiers.
|
|
23
|
+
* - Non-object members of `A` are discarded while those of `B` are preserved.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* type A = { readonly a: 1, b: { c: 1, d: "b" } };
|
|
27
|
+
* type B = { a: 99, b: { c: 4 } };
|
|
28
|
+
*
|
|
29
|
+
* type AB1 = MergeUnion<A, B>; // { a: 99, b: { c: 4 } }
|
|
30
|
+
* type AB2 = MergeUnionDeep<A, B>; // { a: 99, b: { c: 4, d: "b" } }
|
|
31
|
+
*/
|
|
32
|
+
export type MergeUnionDeep<A, B, P extends boolean = false> = A extends A ? B extends B ? PrettifyDeep<P extends true ? _MergeUnionDeepPreserve<A, B> : _MergeUnionDeep<A, B>> : never : never;
|
|
33
|
+
type _MergeUnionDeep<A, B> = _MergeIntDeep<A, B> & MergeSymDiffDeep<A, B>;
|
|
34
|
+
type _MergeUnionDeepPreserve<A, B> = _MergeIntDeepPreserve<A, B> & MergeSymDiffDeep<A, B>;
|
|
21
35
|
/**
|
|
22
|
-
* Merge two objects
|
|
36
|
+
* Merge two objects, keeping all keys from `A`. On conflict, `B` wins.
|
|
23
37
|
*
|
|
24
|
-
* @remarks
|
|
38
|
+
* @remarks Set `P` to true to use `A`'s modifiers.
|
|
25
39
|
*
|
|
26
40
|
* @example
|
|
27
41
|
* type A = { readonly a: 1, b: 2 };
|
|
28
|
-
* type B = { a: "<>",
|
|
42
|
+
* type B = { a: "<>", extra: "ignored" };
|
|
29
43
|
*
|
|
30
44
|
* type AB1 = MergePatch<A, B>; // { a: "<>", b: 2 }
|
|
31
45
|
* type AB2 = MergePatch<A, B, true>; // { readonly a: "<>", b: 2 }
|
|
32
46
|
*/
|
|
33
|
-
export type MergePatch<A
|
|
34
|
-
type _MergePatch<A
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
47
|
+
export type MergePatch<A, B, P extends boolean = false> = A extends A ? B extends B ? Prettify<P extends true ? _MergePatchPreserve<A, B> : _MergePatch<A, B>> : never : never;
|
|
48
|
+
type _MergePatch<A, B> = _MergeInt<A, B> & MergeDiff<A, B>;
|
|
49
|
+
type _MergePatchPreserve<A, B> = _MergeIntPreserve<A, B> & MergeDiff<A, B>;
|
|
50
|
+
/**
|
|
51
|
+
* Merge two objects deeply, keeping all keys from `A`. On conflict, values are recursively merged if both are objects, otherwise `B` wins.
|
|
52
|
+
*
|
|
53
|
+
* @remarks
|
|
54
|
+
* - Set `P` to true to use `A`'s modifiers.
|
|
55
|
+
* - Non-object members of `A` are discarded while those of `B` are preserved.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* type A = { a: 1, b: { c: 1, d: "b" } };
|
|
59
|
+
* type B = { a: 99, extra: "key", b: { c: 4, extra: "key" } };
|
|
60
|
+
*
|
|
61
|
+
* type AB1 = MergePatch<A, B>; // { a: 99, b: { c: 4, extra: "key" } }
|
|
62
|
+
* type AB2 = MergePatchDeep<A, B>; // { a: 99, b: { c: 4, d: "b" } }
|
|
63
|
+
*/
|
|
64
|
+
export type MergePatchDeep<A, B, P extends boolean = false> = A extends A ? B extends B ? PrettifyDeep<P extends true ? _MergePatchDeepPreserve<A, B> : _MergePatchDeep<A, B>> : never : never;
|
|
65
|
+
type _MergePatchDeep<A, B> = _MergeIntDeep<A, B> & MergeDiffDeep<A, B>;
|
|
66
|
+
type _MergePatchDeepPreserve<A, B> = _MergeIntDeepPreserve<A, B> & MergeDiffDeep<A, B>;
|
|
40
67
|
/**
|
|
41
|
-
* Merge two objects
|
|
68
|
+
* Merge two objects, keeping all keys from `A` that exist in `B`. Values are taken from `B`.
|
|
42
69
|
*
|
|
43
|
-
* @remarks
|
|
70
|
+
* @remarks Set `P` to true to use `A`'s modifiers.
|
|
44
71
|
*
|
|
45
72
|
* @example
|
|
46
73
|
* type A = { readonly a: 1, b: 2 };
|
|
@@ -49,15 +76,42 @@ type _MergePatchPreserve<A extends Dict, B extends Dict> = {
|
|
|
49
76
|
* type AB1 = MergeInt<A, B>; // { a: "<>" }
|
|
50
77
|
* type AB2 = MergeInt<A, B, true>; // { readonly a: "<>" }
|
|
51
78
|
*/
|
|
52
|
-
export type MergeInt<A
|
|
53
|
-
type _MergeInt<A
|
|
79
|
+
export type MergeInt<A, B, P extends boolean = false> = A extends A ? B extends B ? Prettify<P extends true ? _MergeIntPreserve<A, B> : _MergeInt<A, B>> : never : never;
|
|
80
|
+
type _MergeInt<A, B> = {
|
|
54
81
|
[P in keyof B as P extends keyof A ? P : never]: B[P];
|
|
55
82
|
};
|
|
56
|
-
type _MergeIntPreserve<A
|
|
57
|
-
[P in keyof A as P extends keyof B ? P : never]: B[P & keyof B];
|
|
83
|
+
type _MergeIntPreserve<A, B> = {
|
|
84
|
+
[P in keyof A as P extends keyof B ? P : never]: Required<B>[P & keyof B];
|
|
58
85
|
};
|
|
59
86
|
/**
|
|
60
|
-
* Merge two objects
|
|
87
|
+
* Merge two objects deeply, keeping all keys from `A` that exist in `B`. Values are recursively merged if both are objects, otherwise they are taken from `B`.
|
|
88
|
+
*
|
|
89
|
+
* @remarks
|
|
90
|
+
* - Set `P` to true to use `A`'s modifiers.
|
|
91
|
+
* - Non-object members of `A` are discarded while those of `B` are preserved.
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* type A = { a: 1, b: { c: 1, d: "b" } };
|
|
95
|
+
* type B = { a: 9, b: { c: 4 } };
|
|
96
|
+
*
|
|
97
|
+
* type AB1 = MergeInt<A, B>; // { a: 9, b: { c: 4 } }
|
|
98
|
+
* type AB2 = MergeIntDeep<A, B>; // { a: 9, b: { c: 4, d: "b" } }
|
|
99
|
+
*/
|
|
100
|
+
export type MergeIntDeep<A, B, P extends boolean = false> = A extends A ? B extends B ? PrettifyDeep<P extends true ? _MergeIntDeepPreserve<A, B> : _MergeIntDeep<A, B>> : never : never;
|
|
101
|
+
type _MergeIntDeep<A, B> = {
|
|
102
|
+
[P in keyof B as P extends keyof A ? P : never]: [
|
|
103
|
+
Required<A>[P & keyof A],
|
|
104
|
+
Required<B>[P]
|
|
105
|
+
] extends [infer AP, infer BP] ? HasType<AP, Dict> | HasType<BP, Dict> extends true ? Exclude<BP, Dict> | _MergeIntDeep<Extract<AP, Dict>, Extract<BP, Dict>> : BP : never;
|
|
106
|
+
};
|
|
107
|
+
type _MergeIntDeepPreserve<A, B> = {
|
|
108
|
+
[P in keyof A as P extends keyof B ? P : never]: [
|
|
109
|
+
Required<A>[P],
|
|
110
|
+
Required<B>[P & keyof B]
|
|
111
|
+
] extends [infer AP, infer BP] ? HasType<AP, Dict> | HasType<BP, Dict> extends true ? Exclude<BP, Dict> | _MergeIntDeepPreserve<Extract<AP, Dict>, Extract<BP, Dict>> : BP : never;
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* Merge two objects, keeping all keys that exist only in `A`.
|
|
61
115
|
*
|
|
62
116
|
* @example
|
|
63
117
|
* type A = { readonly a: 1, b: 2 };
|
|
@@ -66,12 +120,24 @@ type _MergeIntPreserve<A extends Dict, B extends Dict> = {
|
|
|
66
120
|
* type AB = MergeDiff<A, B>; // { b: 2 }
|
|
67
121
|
* type BA = MergeDiff<B, A>; // { c: 4 }
|
|
68
122
|
*/
|
|
69
|
-
export type MergeDiff<A
|
|
70
|
-
type _MergeDiff<A extends Dict, B extends Dict> = {
|
|
123
|
+
export type MergeDiff<A, B> = A extends A ? B extends B ? {
|
|
71
124
|
[P in keyof A as P extends keyof B ? never : P]: A[P];
|
|
72
|
-
};
|
|
125
|
+
} : never : never;
|
|
126
|
+
/**
|
|
127
|
+
* Merge two objects deeply, keeping all keys that exist only in `A` and recursively merging keys that exist in both when both values are objects.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* type A = { a: 1, b: { c: 1, d: "b" } };
|
|
131
|
+
* type B = { b: { d: "b" } };
|
|
132
|
+
*
|
|
133
|
+
* type AB1 = MergeDiff<A, B>; // { a: 1 }
|
|
134
|
+
* type AB2 = MergeDiffDeep<A, B>; // { a: 1, b: { c: 1 } }
|
|
135
|
+
*/
|
|
136
|
+
export type MergeDiffDeep<A, B> = A extends A ? B extends B ? {
|
|
137
|
+
[P in keyof A as P extends keyof B ? (HasType<B[P], Dict> extends true ? P : never) : P]: P extends keyof B ? HasType<B[P], Dict> | HasType<A[P], Dict> extends true ? MergeDiffDeep<A[P], B[P]> : A[P] : A[P];
|
|
138
|
+
} : never : never;
|
|
73
139
|
/**
|
|
74
|
-
* Merge two objects
|
|
140
|
+
* Merge two objects, keeping all non-shared keys.
|
|
75
141
|
*
|
|
76
142
|
* @example
|
|
77
143
|
* type A = { readonly a: 1, b: 2 };
|
|
@@ -80,16 +146,28 @@ type _MergeDiff<A extends Dict, B extends Dict> = {
|
|
|
80
146
|
* type AB = MergeSymDiff<A, B>; // { b: 2, c: 4 }
|
|
81
147
|
* type BA = AB;
|
|
82
148
|
*/
|
|
83
|
-
export type MergeSymDiff<A
|
|
84
|
-
type _MergeSymDiff<A extends Dict, B extends Dict> = {
|
|
85
|
-
[P in keyof A as P extends keyof B ? never : P]: A[P];
|
|
86
|
-
} & {
|
|
87
|
-
[P in keyof B as P extends keyof A ? never : P]: B[P];
|
|
88
|
-
};
|
|
149
|
+
export type MergeSymDiff<A, B> = A extends A ? B extends B ? Prettify<MergeDiff<A, B> & MergeDiff<B, A>> : never : never;
|
|
89
150
|
/**
|
|
90
|
-
* Merge two objects
|
|
151
|
+
* Merge two objects deeply, keeping all non-shared keys and recursively merges shared keys when both are objects.
|
|
152
|
+
*
|
|
153
|
+
* @example
|
|
154
|
+
* type A = { a: 1, b: { c: 1, d: "b" } };
|
|
155
|
+
* type B = { b: { d: "b" } };
|
|
156
|
+
*
|
|
157
|
+
* type AB1 = MergeDiff<A, B>; // { a: 1 }
|
|
158
|
+
* type AB2 = MergeDiffDeep<A, B>; // { a: 1, b: { c: 1 } }
|
|
159
|
+
*/
|
|
160
|
+
export type MergeSymDiffDeep<A, B> = A extends A ? B extends B ? PrettifyDeep<MergeDiffDeep<A, B> & MergeDiffDeep<B, A>> : never : never;
|
|
161
|
+
/**
|
|
162
|
+
* Merge two objects using the given method. (default: "union")
|
|
91
163
|
*
|
|
92
164
|
* @see {@linkcode MergeUnion}, {@linkcode MergePatch}, {@linkcode MergeInt}, {@linkcode MergeDiff} and {@linkcode MergeSymDiff}
|
|
93
165
|
*/
|
|
94
|
-
export type Merge<A
|
|
166
|
+
export type Merge<A, B, M extends "int" | "int-preserve" | "union" | "union-preserve" | "patch" | "patch-preserve" | "diff" | "sym-diff" = "union"> = M extends "int" ? MergeInt<A, B> : M extends "int-preserve" ? MergeInt<A, B, true> : M extends "union" ? MergeUnion<A, B> : M extends "union-preserve" ? MergeUnion<A, B, true> : M extends "patch" ? MergePatch<A, B> : M extends "patch-preserve" ? MergePatch<A, B, true> : M extends "diff" ? MergeDiff<A, B> : M extends "sym-diff" ? MergeSymDiff<A, B> : never;
|
|
167
|
+
/**
|
|
168
|
+
* Merge two objects deeply using the specified method. (default: "union")
|
|
169
|
+
*
|
|
170
|
+
* @see {@linkcode MergeUnionDeep}, {@linkcode MergePatchDeep}, {@linkcode MergeIntDeep}, {@linkcode MergeDiffDeep} and {@linkcode MergeSymDiffDeep}
|
|
171
|
+
*/
|
|
172
|
+
export type MergeDeep<A, B, M extends "patch" | "patch-preserve" | "union" | "union-preserve" | "int" | "int-preserve" | "diff" | "sym-diff" = "union"> = M extends "int" ? MergeIntDeep<A, B> : M extends "int-preserve" ? MergeIntDeep<A, B, true> : M extends "union" ? MergeUnionDeep<A, B> : M extends "union-deep" ? MergeUnionDeep<A, B, true> : M extends "patch" ? MergePatchDeep<A, B> : M extends "patch-preserve" ? MergePatchDeep<A, B, true> : M extends "diff" ? MergeDiffDeep<A, B> : M extends "sym-diff" ? MergeSymDiffDeep<A, B> : never;
|
|
95
173
|
export {};
|
package/dist/dict/modifiers.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Keys } from "./keyof";
|
|
2
2
|
import type { LooseOmit } from "./pick";
|
|
3
|
-
import type {
|
|
3
|
+
import type { Known, Prettify } from "./utils";
|
|
4
4
|
/**
|
|
5
5
|
* Makes all properties of `T` mutable.
|
|
6
6
|
*/
|
|
@@ -11,47 +11,47 @@ export type Mutable<T> = {
|
|
|
11
11
|
* Makes all properties of `T` required (deep).
|
|
12
12
|
*/
|
|
13
13
|
export type RequiredDeep<T> = {
|
|
14
|
-
[P in keyof T]-?:
|
|
14
|
+
[P in keyof T]-?: RequiredDeep<T[P]>;
|
|
15
15
|
};
|
|
16
16
|
/**
|
|
17
17
|
* Makes all properties of `T` optional (deep).
|
|
18
18
|
*/
|
|
19
19
|
export type PartialDeep<T> = {
|
|
20
|
-
[P in keyof T]?:
|
|
20
|
+
[P in keyof T]?: PartialDeep<T[P]>;
|
|
21
21
|
};
|
|
22
22
|
/**
|
|
23
23
|
* Makes all properties of `T` readonly (deep).
|
|
24
24
|
*/
|
|
25
25
|
export type ReadonlyDeep<T> = {
|
|
26
|
-
readonly [P in keyof T]:
|
|
26
|
+
readonly [P in keyof T]: ReadonlyDeep<T[P]>;
|
|
27
27
|
};
|
|
28
28
|
/**
|
|
29
29
|
* Makes all properties of `T` mutable (deep).
|
|
30
30
|
*/
|
|
31
31
|
export type MutableDeep<T> = {
|
|
32
|
-
-readonly [P in keyof T]:
|
|
32
|
+
-readonly [P in keyof T]: MutableDeep<T[P]>;
|
|
33
33
|
};
|
|
34
34
|
/**
|
|
35
35
|
* Makes a property of `T` required.
|
|
36
36
|
*/
|
|
37
|
-
export type RequiredKey<T
|
|
37
|
+
export type RequiredKey<T, K extends Keys<Known<T>>> = Prettify<{
|
|
38
38
|
[P in keyof T as P extends K ? P : never]-?: T[P];
|
|
39
39
|
} & LooseOmit<T, K>>;
|
|
40
40
|
/**
|
|
41
41
|
* Makes a property of `T` optional.
|
|
42
42
|
*/
|
|
43
|
-
export type PartialKey<T
|
|
43
|
+
export type PartialKey<T, K extends Keys<Known<T>>> = Prettify<{
|
|
44
44
|
[P in keyof T as P extends K ? P : never]?: T[P];
|
|
45
45
|
} & LooseOmit<T, K>>;
|
|
46
46
|
/**
|
|
47
47
|
* Makes a property of `T` readonly.
|
|
48
48
|
*/
|
|
49
|
-
export type ReadonlyKey<T
|
|
49
|
+
export type ReadonlyKey<T, K extends Keys<Known<T>>> = Prettify<{
|
|
50
50
|
readonly [P in keyof T as P extends K ? P : never]: T[P];
|
|
51
51
|
} & LooseOmit<T, K>>;
|
|
52
52
|
/**
|
|
53
53
|
* Makes a property of `T` mutable.
|
|
54
54
|
*/
|
|
55
|
-
export type MutableKey<T
|
|
55
|
+
export type MutableKey<T, K extends Keys<Known<T>>> = Prettify<{
|
|
56
56
|
-readonly [P in keyof T as P extends K ? P : never]: T[P];
|
|
57
57
|
} & LooseOmit<T, K>>;
|
package/dist/dict/pick.d.ts
CHANGED
|
@@ -1,28 +1,25 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import type { Dict } from "./utils";
|
|
1
|
+
import type { Keys } from "./keyof";
|
|
2
|
+
import type { Known, SuggestKey } from "./utils";
|
|
4
3
|
/**
|
|
5
|
-
*
|
|
6
|
-
* and it will only pick the keys that actually exist in `T`.
|
|
4
|
+
* A distributive/loose version of the built-in {@linkcode Pick}.
|
|
7
5
|
*
|
|
8
6
|
* @example
|
|
9
7
|
* type R1 = Pick<{a: 1}, "a" | "b">; // { a: 1, b: unknown }
|
|
10
8
|
* type R2 = LoosePick<{a: 1}, "a" | "b">; // { a: 1 }
|
|
11
9
|
*/
|
|
12
|
-
export type LoosePick<T
|
|
13
|
-
[P in keyof T as P extends K ? P : never]: T[P];
|
|
10
|
+
export type LoosePick<T, K extends SuggestKey<Keys<Known<T>>>> = T extends T ? {
|
|
11
|
+
[P in keyof T as P extends K ? P : string extends P ? K : never]: T[P];
|
|
14
12
|
} : never;
|
|
15
13
|
/**
|
|
16
|
-
*
|
|
14
|
+
* A distributive version of the built-in {@linkcode Pick}.
|
|
17
15
|
*
|
|
18
16
|
* @example
|
|
19
17
|
* type R1 = Pick<{a: 1} | {b: 2}, "a">; // { a: unknown } // + Error
|
|
20
18
|
* type R2 = StrictPick<{a: 1} | {b: 2}, "a">; // { a: 1 } | {}
|
|
21
19
|
*/
|
|
22
|
-
export type StrictPick<T
|
|
20
|
+
export type StrictPick<T, K extends Keys<T>> = LoosePick<T, K>;
|
|
23
21
|
/**
|
|
24
|
-
*
|
|
25
|
-
* and it works differently when `T` is a union of objects.
|
|
22
|
+
* A distributive version of the built-in {@linkcode Omit}.
|
|
26
23
|
*
|
|
27
24
|
* @example
|
|
28
25
|
* type R1 = Omit<{a: 1, b: 2}, "a">; // { b: 2 }
|
|
@@ -31,12 +28,11 @@ export type StrictPick<T extends Dict, K extends Keyof<T>> = LoosePick<T, K>;
|
|
|
31
28
|
* type R3 = Omit<{a: 1} | {b: 2}, "a">; // {}
|
|
32
29
|
* type R4 = LooseOmit<{a: 1} | {b: 2}, "a">; // {} | {b: 2}
|
|
33
30
|
*/
|
|
34
|
-
export type LooseOmit<T
|
|
31
|
+
export type LooseOmit<T, K extends SuggestKey<Keys<Known<T>>>> = T extends T ? {
|
|
35
32
|
[P in keyof T as P extends K ? never : P]: T[P];
|
|
36
33
|
} : never;
|
|
37
34
|
/**
|
|
38
|
-
*
|
|
39
|
-
* and it works differently when `T` is a union of objects.
|
|
35
|
+
* A distributive/strict version of the built-in {@linkcode Omit}.
|
|
40
36
|
*
|
|
41
37
|
* @example
|
|
42
38
|
* type R1 = Omit<{a: 1}, "z">; // {a: 1}
|
|
@@ -46,4 +42,4 @@ export type LooseOmit<T extends Dict, K extends Suggest<Keyof<T>>> = T extends T
|
|
|
46
42
|
* type R4 = StrictOmit<{a: 1} | {b: 2}, "a">; // {} | {b: 2}
|
|
47
43
|
*
|
|
48
44
|
*/
|
|
49
|
-
export type StrictOmit<T
|
|
45
|
+
export type StrictOmit<T, K extends Keys<T>> = LooseOmit<T, K>;
|
package/dist/dict/prefix.d.ts
CHANGED
|
@@ -1,38 +1,37 @@
|
|
|
1
|
-
import type { EnsurePrefix, EnsureSuffix, Prefix,
|
|
2
|
-
import type { Dict } from "./utils";
|
|
1
|
+
import type { EnsurePrefix, EnsureSuffix, Prefix, Suffix, UnPrefix, UnSuffix } from "../string";
|
|
3
2
|
/**
|
|
4
3
|
* Adds a prefix to all keys of `T`.
|
|
5
4
|
*/
|
|
6
|
-
export type PrefixKeys<T
|
|
7
|
-
[K in keyof T as K extends string
|
|
5
|
+
export type PrefixKeys<T, P extends string> = {
|
|
6
|
+
[K in keyof T as K extends string ? Prefix<K, P> : K]: T[K];
|
|
8
7
|
};
|
|
9
8
|
/**
|
|
10
9
|
* Adds a prefix to all keys of `T`, but only if the key does not already start with the prefix.
|
|
11
10
|
*/
|
|
12
|
-
export type PrefixKeysEnsure<T
|
|
13
|
-
[K in keyof T as K extends string
|
|
11
|
+
export type PrefixKeysEnsure<T, P extends string> = {
|
|
12
|
+
[K in keyof T as K extends string ? EnsurePrefix<K, P> : K]: T[K];
|
|
14
13
|
};
|
|
15
14
|
/**
|
|
16
15
|
* Removes a prefix from all keys of `T`, if it exists.
|
|
17
16
|
*/
|
|
18
|
-
export type UnPrefixKeys<T
|
|
19
|
-
[K in keyof T as K extends string
|
|
17
|
+
export type UnPrefixKeys<T, P extends string> = {
|
|
18
|
+
[K in keyof T as K extends string ? UnPrefix<K, P> : K]: T[K];
|
|
20
19
|
};
|
|
21
20
|
/**
|
|
22
21
|
* Adds a suffix to all keys of `T`.
|
|
23
22
|
*/
|
|
24
|
-
export type SuffixKeys<T
|
|
25
|
-
[K in keyof T as K extends string
|
|
23
|
+
export type SuffixKeys<T, S extends string> = {
|
|
24
|
+
[K in keyof T as K extends string ? Suffix<K, S> : K]: T[K];
|
|
26
25
|
};
|
|
27
26
|
/**
|
|
28
27
|
* Adds a suffix to all keys of `T`, but only if the key does not already end with the suffix.
|
|
29
28
|
*/
|
|
30
|
-
export type SuffixKeysEnsure<T
|
|
31
|
-
[K in keyof T as K extends string
|
|
29
|
+
export type SuffixKeysEnsure<T, S extends string> = {
|
|
30
|
+
[K in keyof T as K extends string ? EnsureSuffix<K, S> : K]: T[K];
|
|
32
31
|
};
|
|
33
32
|
/**
|
|
34
33
|
* Removes a suffix from all keys of `T`, if it exists.
|
|
35
34
|
*/
|
|
36
|
-
export type UnSuffixKeys<T
|
|
37
|
-
[K in keyof T as K extends string
|
|
35
|
+
export type UnSuffixKeys<T, S extends string> = {
|
|
36
|
+
[K in keyof T as K extends string ? UnSuffix<K, S> : K]: T[K];
|
|
38
37
|
};
|
package/dist/dict/rename.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { Dict, DictKey } from "./utils";
|
|
1
|
+
import type { Keys } from "./keyof";
|
|
2
|
+
import type { Dict, DictKey, Known } from "./utils";
|
|
3
3
|
/**
|
|
4
4
|
* Renames a key of `T`.
|
|
5
5
|
*
|
|
6
6
|
* @example
|
|
7
7
|
* type R1 = RenameKey<{rgb: RGB, a: number}, "a", "alpha">; // { rgb: RGB, alpha: number }
|
|
8
8
|
*/
|
|
9
|
-
export type RenameKey<T
|
|
9
|
+
export type RenameKey<T, K extends Keys<Known<T>>, N extends DictKey> = T extends T ? {
|
|
10
10
|
[P in keyof T as P extends K ? N : P]: T[P];
|
|
11
11
|
} : never;
|
|
12
12
|
/**
|
|
@@ -15,6 +15,6 @@ export type RenameKey<T extends Dict, K extends Keyof<T>, N extends DictKey> = T
|
|
|
15
15
|
* @example
|
|
16
16
|
* type R1 = RenameKeys<{f: string, b: string, c: 4}, {f: "foo", b: "bar"}>; // { foo: string, bar: string, c: 4 }
|
|
17
17
|
*/
|
|
18
|
-
export type RenameKeys<T
|
|
18
|
+
export type RenameKeys<T, M extends Partial<Dict<Keys<Known<T>>, DictKey>>> = T extends T ? M extends M ? {
|
|
19
19
|
[P in keyof T as P extends keyof M ? M[P] & DictKey : P]: T[P];
|
|
20
20
|
} : never : never;
|
package/dist/dict/utils.d.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import type { ROList } from "../list";
|
|
2
1
|
import type { HasType } from "../other";
|
|
3
|
-
import type { KnownTuple } from "../tuple/utils";
|
|
4
2
|
/**
|
|
5
3
|
* Types that can be used as keys for an object.
|
|
6
4
|
*/
|
|
@@ -10,18 +8,19 @@ export type DictKey = string | number | symbol;
|
|
|
10
8
|
*/
|
|
11
9
|
export type Dict<K extends DictKey = string, V = unknown> = Record<K, V>;
|
|
12
10
|
/**
|
|
13
|
-
* Removes
|
|
11
|
+
* Removes the index signature from an object.
|
|
14
12
|
*
|
|
15
13
|
* @example
|
|
16
14
|
* type Config = { foo: string, bar: boolean, [x: string]: string };
|
|
17
15
|
* type KnownConfig = Known<Config>; // { foo: string, bar: boolean }
|
|
18
|
-
*
|
|
19
|
-
* // Can also be used on tuples to get the guaranteed elements
|
|
20
|
-
* type R = Known<[string, number?, ...number[]]>; // [string]
|
|
21
16
|
*/
|
|
22
|
-
export type Known<T
|
|
17
|
+
export type Known<T> = T extends T ? {
|
|
23
18
|
[P in keyof T as HasType<DictKey, P> extends true ? never : P]: T[P];
|
|
24
19
|
} : never;
|
|
20
|
+
/**
|
|
21
|
+
* Generates a type that suggests a pre-defined list of dictionary keys while accepting any other string or number.
|
|
22
|
+
*/
|
|
23
|
+
export type SuggestKey<T, S extends boolean = false> = Extract<T, DictKey> | (string & {}) | (number & {}) | (S extends true ? never : symbol & {});
|
|
25
24
|
/**
|
|
26
25
|
* Prettifies an object.
|
|
27
26
|
*
|
|
@@ -36,5 +35,5 @@ export type Prettify<T> = {
|
|
|
36
35
|
* A deep version of {@linkcode Prettify}.
|
|
37
36
|
*/
|
|
38
37
|
export type PrettifyDeep<T> = {
|
|
39
|
-
[P in keyof T]: T[P] extends infer V ? (V extends Dict
|
|
38
|
+
[P in keyof T]: T[P] extends infer V ? (V extends Dict ? PrettifyDeep<V> : V) : never;
|
|
40
39
|
} & {};
|
package/dist/func/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from
|
|
1
|
+
export * from './utils';
|
package/dist/func/utils.d.ts
CHANGED
|
@@ -21,7 +21,7 @@ export type OrFunc<T, A extends ROList = []> = T | Func<A, T>;
|
|
|
21
21
|
*/
|
|
22
22
|
export type VoidFn = Func<[], void>;
|
|
23
23
|
/**
|
|
24
|
-
* An alias for {@linkcode Parameters
|
|
24
|
+
* An alias for {@linkcode Parameters}.
|
|
25
25
|
*/
|
|
26
26
|
export type Params<F extends Func> = Parameters<F>;
|
|
27
27
|
/**
|
|
@@ -34,6 +34,6 @@ export type Params<F extends Func> = Parameters<F>;
|
|
|
34
34
|
*/
|
|
35
35
|
export type Param<F extends Func, I extends number> = Params<F>[NormalizeIndex<I, Params<F>>];
|
|
36
36
|
/**
|
|
37
|
-
* An alias for {@linkcode ReturnType
|
|
37
|
+
* An alias for {@linkcode ReturnType}.
|
|
38
38
|
*/
|
|
39
39
|
export type RT<F extends Func> = ReturnType<F>;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
7
|
-
export * from
|
|
1
|
+
export * from './dict';
|
|
2
|
+
export * from './func';
|
|
3
|
+
export * from './list';
|
|
4
|
+
export * from './number';
|
|
5
|
+
export * from './other';
|
|
6
|
+
export * from './string';
|
|
7
|
+
export * from './tuple';
|
package/dist/list/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from
|
|
1
|
+
export * from './utils';
|
package/dist/number/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
1
|
+
export * from './increment';
|
|
2
|
+
export * from './utils';
|
package/dist/other/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
1
|
+
export * from './utils';
|
|
2
|
+
export * from './validation';
|
package/dist/other/utils.d.ts
CHANGED
|
@@ -1,8 +1,24 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { IsPrimitive, TS_UT } from "../_internal";
|
|
2
|
+
import type { IsAny, IsUnknown } from "./validation";
|
|
2
3
|
/**
|
|
3
4
|
* An alias for `any`.
|
|
4
5
|
*/
|
|
5
6
|
export type Valid = any;
|
|
7
|
+
/**
|
|
8
|
+
* A range type.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const ranges = elements.map<Range>((ele) => [ele.getStart(), ele.getEnd()]);
|
|
12
|
+
*/
|
|
13
|
+
export type Range = [start: number, end: number];
|
|
14
|
+
/**
|
|
15
|
+
* Removes `undefined` and `null` from a type.
|
|
16
|
+
*/
|
|
17
|
+
export type NonNullish<T> = Exclude<T, undefined | null>;
|
|
18
|
+
/**
|
|
19
|
+
* Adds `undefined` and `null` to a type.
|
|
20
|
+
*/
|
|
21
|
+
export type Nullish<T> = T | undefined | null;
|
|
6
22
|
/**
|
|
7
23
|
* @example
|
|
8
24
|
* // Suppose you have a type like this
|
|
@@ -18,10 +34,10 @@ export type Valid = any;
|
|
|
18
34
|
*
|
|
19
35
|
* // To make sure that all tokens have an alias, you can use something like this:
|
|
20
36
|
* {
|
|
21
|
-
* type Check = Assert<AreEqual<
|
|
37
|
+
* type Check = Assert<AreEqual<Values<typeof tk>, Token>>;
|
|
22
38
|
* }
|
|
23
39
|
* // You'll see an error if there's a missing/extra token
|
|
24
|
-
* // Note: AreEqual and
|
|
40
|
+
* // Note: AreEqual and Values are other utils provided by this package
|
|
25
41
|
*/
|
|
26
42
|
export type Assert<T extends E, E = true> = T;
|
|
27
43
|
/**
|
|
@@ -29,11 +45,20 @@ export type Assert<T extends E, E = true> = T;
|
|
|
29
45
|
*/
|
|
30
46
|
export type Not<T extends boolean> = T extends true ? false : true;
|
|
31
47
|
/**
|
|
32
|
-
* Similar to the built-in {@linkcode Extract
|
|
48
|
+
* Similar to the built-in {@linkcode Extract}, except it return `Extract<U, T>` (or simply `U`)
|
|
33
49
|
* when `T` is `unknown`, `any` or a **primitive type** (string, number, boolean).
|
|
34
50
|
*/
|
|
35
51
|
export type SmartExtract<T, U> = T extends T ? IsUnknown<T> | IsAny<T> | IsPrimitive<T> extends false ? Extract<T, U> : Extract<U, T> : never;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
export {
|
|
52
|
+
/**
|
|
53
|
+
* Useful for debugging.
|
|
54
|
+
*/
|
|
55
|
+
export type Self<T, I1 = TS_UT, I2 = TS_UT, I3 = TS_UT, I4 = TS_UT, I5 = TS_UT> = {
|
|
56
|
+
T: T;
|
|
57
|
+
1: I1;
|
|
58
|
+
2: I2;
|
|
59
|
+
3: I3;
|
|
60
|
+
4: I4;
|
|
61
|
+
5: I5;
|
|
62
|
+
} extends infer R ? {
|
|
63
|
+
[P in keyof R as R[P] extends TS_UT ? never : P]: R[P];
|
|
64
|
+
} : never;
|
package/dist/string/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
1
|
+
export * from './extract';
|
|
2
|
+
export * from './firstChar';
|
|
3
|
+
export * from './prefix';
|
|
4
|
+
export * from './split';
|
|
5
|
+
export * from './utils';
|
package/dist/string/utils.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ export type Stringable = string | number | boolean | bigint | null | undefined;
|
|
|
11
11
|
* const f1: Fruit = "Apple"; // Valid + Auto-completion
|
|
12
12
|
* const f2: Fruit = "Banana"; // Valid
|
|
13
13
|
*/
|
|
14
|
-
export type Suggest<T
|
|
14
|
+
export type Suggest<T> = Extract<T, string> | (string & {});
|
|
15
15
|
/**
|
|
16
16
|
* Converts a {@linkcode Stringable} to its string representation.
|
|
17
17
|
*
|
package/dist/tuple/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
1
|
+
export * from './push';
|
|
2
|
+
export * from './utils';
|
package/dist/tuple/push.d.ts
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import type { List, ROList } from "../list";
|
|
4
|
-
import type { Increment, ToNumber } from "../number";
|
|
1
|
+
import type { IsTuple, MatchTuple, NegAbs } from "../_internal";
|
|
2
|
+
import type { ROList } from "../list";
|
|
5
3
|
import type { IsSubtype } from "../other";
|
|
6
|
-
import type { EnsurePrefix, Stringify } from "../string";
|
|
7
4
|
import type { Tuple } from "./utils";
|
|
8
5
|
/**
|
|
9
6
|
* Pushes an element to the end of a tuple.
|
|
@@ -16,7 +13,7 @@ import type { Tuple } from "./utils";
|
|
|
16
13
|
*/
|
|
17
14
|
export type Push<T extends ROList, E, S extends boolean = false> = [
|
|
18
15
|
...Required<T>,
|
|
19
|
-
...(IsSubtype<S, true> | IsSubtype<E, ROList> extends true ?
|
|
16
|
+
...(IsSubtype<S, true> | IsSubtype<E, ROList> extends true ? Extract<E, ROList> : [E])
|
|
20
17
|
] extends infer Result extends ROList ? MatchTuple<Result, T> : never;
|
|
21
18
|
/**
|
|
22
19
|
* Pops an element from the end of a tuple.
|
|
@@ -34,9 +31,9 @@ export type Pop<T extends ROList, N extends number = 1> = Required<T> extends re
|
|
|
34
31
|
* type R2 = Unshift<[1, 2, 3?], 4>; // [4, 1, 2, 3?]
|
|
35
32
|
*/
|
|
36
33
|
export type Unshift<T extends ROList, E, S extends boolean = false> = [
|
|
37
|
-
...(IsSubtype<S, true> | IsSubtype<E, ROList> extends true ?
|
|
34
|
+
...(IsSubtype<S, true> | IsSubtype<E, ROList> extends true ? Extract<E, ROList> : [E]),
|
|
38
35
|
...Required<T>
|
|
39
|
-
] extends infer Result extends ROList ? MatchTuple<Result, T, IsSubtype<S, true> | IsTuple<E> extends true ?
|
|
36
|
+
] extends infer Result extends ROList ? MatchTuple<Result, T, IsSubtype<S, true> | IsTuple<E> extends true ? Extract<E, ROList>["length"] : 1> : never;
|
|
40
37
|
/**
|
|
41
38
|
* Removes an element from the beginning of a tuple.
|
|
42
39
|
*
|
|
@@ -45,16 +42,3 @@ export type Unshift<T extends ROList, E, S extends boolean = false> = [
|
|
|
45
42
|
* type R2 = Shift<[1, 2, 3?]>; // [2, 3?]
|
|
46
43
|
*/
|
|
47
44
|
export type Shift<T extends ROList, N extends number = 1> = Required<T> extends readonly [...Tuple<N>, ...infer Rest extends ROList] ? MatchTuple<Rest, T, NegAbs<N>> : [];
|
|
48
|
-
type NegAbs<T extends number> = T extends 0 ? 0 : ToNumber<EnsurePrefix<Stringify<T>, "-">>;
|
|
49
|
-
type FOE<T extends ROList, _C extends List = []> = T extends Required<T> ? -1 : T extends readonly [unknown, ...infer R extends ROList] ? FOE<R, [..._C, any]> : _C["length"];
|
|
50
|
-
type SetFOE<T extends ROList, I extends number> = _SetFOE<T, NormalizeIndex<I, T["length"]>>;
|
|
51
|
-
type _SetFOE<T extends ROList, I extends number, _I extends List = []> = T extends readonly [
|
|
52
|
-
infer F,
|
|
53
|
-
...infer R
|
|
54
|
-
] ? I extends _I["length"] ? [F?, ...Partial<R>] : [F, ..._SetFOE<R, I, [..._I, any]>] : T;
|
|
55
|
-
type MatchTuple<T extends ROList, R extends ROList, D extends number = 0> = MatchRO<MatchFOI<T, R, D>, R>;
|
|
56
|
-
type MatchFOI<T extends ROList, R extends ROList, D extends number = 0> = FOE<R> extends -1 ? BetterRequired<T> : SetFOE<BetterRequired<T>, Increment<FOE<R>, D>> extends infer R extends ROList ? R : never;
|
|
57
|
-
type MatchRO<T extends ROList, R extends ROList> = Readonly<R> extends R ? Readonly<T> : Mutable<T>;
|
|
58
|
-
type BetterRequired<T> = T extends T ? Required<T> : never;
|
|
59
|
-
type IsTuple<T> = T extends T ? Required<T> extends readonly [] | readonly [unknown, ...unknown[]] ? true : false : never;
|
|
60
|
-
export {};
|
package/package.json
CHANGED
|
@@ -1,16 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inzero/ts-ut",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "TypeScript Utility Types",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
7
7
|
"utility",
|
|
8
8
|
"types"
|
|
9
9
|
],
|
|
10
|
+
"homepage": "https://ts-ut.vercel.app",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/InZeroDev/ts-ut",
|
|
14
|
+
"directory": "package"
|
|
15
|
+
},
|
|
10
16
|
"license": "MIT",
|
|
11
17
|
"author": "InZero",
|
|
12
18
|
"exports": {
|
|
13
19
|
".": "./dist/index.d.ts",
|
|
20
|
+
"./_internal": "./dist/_internal.d.ts",
|
|
14
21
|
"./string": "./dist/string/index.d.ts",
|
|
15
22
|
"./number": "./dist/number/index.d.ts",
|
|
16
23
|
"./func": "./dist/func/index.d.ts",
|
|
@@ -28,6 +35,7 @@
|
|
|
28
35
|
"CHANGELOG.md"
|
|
29
36
|
],
|
|
30
37
|
"devDependencies": {
|
|
38
|
+
"@inzero/barrel": "0.0.2",
|
|
31
39
|
"prettier": "^3.9.10",
|
|
32
40
|
"prettier-plugin-packagejson": "^3.0.2",
|
|
33
41
|
"typescript": "^7.0.2"
|
|
@@ -42,6 +50,8 @@
|
|
|
42
50
|
"scripts": {
|
|
43
51
|
"build": "tsc",
|
|
44
52
|
"dev": "tsc --watch",
|
|
45
|
-
"format": "prettier --write ."
|
|
53
|
+
"format": "prettier --write .",
|
|
54
|
+
"gen-barrel": "barrel src/index.ts src/*/index.ts",
|
|
55
|
+
"prepublish": "pnpm gen-barrel; pnpm build"
|
|
46
56
|
}
|
|
47
57
|
}
|