@inzero/ts-ut 0.0.0-stage → 0.1.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 ADDED
@@ -0,0 +1,11 @@
1
+ # Changelog
2
+
3
+ ## [0.1.0] 2026-10-19
4
+
5
+ ### Added
6
+
7
+ - Added `Split<T, S>` and `Join<T, S>` string utility types.
8
+
9
+ ## [0.0.1] 2026-10-15
10
+
11
+ First release.
package/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright © 2026 InZero
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,26 @@
1
- # Temporary Holding Version
1
+ # @inzero/ts-ut
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A TypeScript utility types library.
4
+
5
+ ## Quick Examples
6
+
7
+ ```ts
8
+ import { Param } from "@inzero/ts-ut";
9
+ import { GetDeep } from "@inzero/ts-ut/dict"; // Or category-specific imports
10
+
11
+ type Handler = (foo: string, bar: number) => boolean;
12
+ type LastParam = Param<Handler, -1>; // number [!code highlight]
13
+
14
+ type Config = { a: 1; b: { c: 4 } };
15
+ type Target = GetDeep<Config, "b.c">; // 4 [!code highlight]
16
+ ```
17
+
18
+ ## Installation
19
+
20
+ ```sh
21
+ pnpm add -D @inzero/ts-ut
22
+ ```
23
+
24
+ ## Docs
25
+
26
+ Visit: https://ts-ut.vercel.app
@@ -0,0 +1,7 @@
1
+ import type { ROList } from "./list";
2
+ import type { Decrement, Increment } from "./number";
3
+ import type { Stringify } from "./string";
4
+ export type IsNegative<T extends string | number> = T extends T ? StartsWith<Stringify<T>, "-"> extends true ? true : false : never;
5
+ 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
+ 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;
@@ -0,0 +1,23 @@
1
+ import type { ROList } from "../list";
2
+ import type { IsNever } from "../other";
3
+ import type { Keyof, KeyofDeep } from "./keyof";
4
+ import type { Dict } from "./utils";
5
+ /**
6
+ * An alias for `Obj['Key']`.
7
+ *
8
+ * @example
9
+ * type R1 = Get<{ a: 1, b: 2 }, 'a'>; // 1
10
+ */
11
+ export type Get<T extends Dict, K extends Keyof<T>> = T extends T ? K extends keyof T ? T[K] : never : never;
12
+ /**
13
+ * Get a deeply nested value using dot-notation.
14
+ *
15
+ * @example
16
+ * type R1 = GetDeep<{ a: { b: { c: 1 } } }, 'a.b.c'>; // 1
17
+ */
18
+ export type GetDeep<T extends Dict | ROList, K extends KeyofDeep<T>> = T extends T ? K extends K ? Next<K> extends [infer N extends string, infer R] ? _GetDeep<T, N, R> : never : never : never;
19
+ type _GetDeep<T extends Dict | ROList, N extends string, R> = {
20
+ [P in keyof (IsNever<R> extends false ? Required<T> : T)]: P extends N ? IsNever<R> extends true ? T[P] : GetDeep<T[P], R> : never;
21
+ } extends infer R ? R extends ROList ? R[number] : R[keyof R] : never;
22
+ type Next<K extends string> = K extends `${infer N extends string}.${infer R extends string}` ? [N, R] : [K, never];
23
+ export {};
@@ -0,0 +1,8 @@
1
+ export * from "./get";
2
+ export * from "./keyof";
3
+ export * from "./merge";
4
+ export * from "./modifiers";
5
+ export * from "./pick";
6
+ export * from "./prefix";
7
+ export * from "./rename";
8
+ export * from "./utils";
@@ -0,0 +1,51 @@
1
+ import type { ROList } from "../list";
2
+ import type { Dict, Known } from "./utils";
3
+ /**
4
+ * Similar to `keyof Obj`, except it returns all possible keys when `T` is a union and only known keys when `T` has `[x: string]: unknown` keys.
5
+ *
6
+ * @example
7
+ * type Config = { foo: string, bar: number, [x: string]: unknown } | { c: 4 };
8
+ *
9
+ * type R1 = Keyof<Config>; // 'foo' | 'bar' | 'c'
10
+ */
11
+ export type Keyof<T extends Dict> = T extends T ? Known<T> extends infer T extends Dict ? keyof T : never : never;
12
+ /**
13
+ * Similar to `Obj[keyof Obj]`, except it returns all possible values when `T` is a union and only known keys' values when `T` has `[x: string]: unknown` keys.
14
+ *
15
+ * @example
16
+ * type Config = { foo: string, bar: boolean, [x: string]: unknown } | { c: 4 };
17
+ *
18
+ * type R1 = Valueof<Config>; // string | boolean | 4
19
+ */
20
+ export type Valueof<T extends Dict | ROList> = T extends T ? Known<T> extends infer T extends Dict ? T[keyof T] : never : never;
21
+ /**
22
+ * Returns `[Keyof<T>, Valueof<T>]`.
23
+ *
24
+ * @see {@linkcode Keyof} and {@linkcode Valueof}
25
+ */
26
+ export type EntryOf<T extends Dict> = T extends T ? Known<T> extends infer T extends Dict ? [keyof T, T[keyof T]] : never : never;
27
+ /**
28
+ * A deep version of {@linkcode Keyof}.
29
+ */
30
+ export type KeyofDeep<T extends Dict | ROList, R extends "leaf" | "parent" | "both" = "both"> = T extends T ? _KeyofDeep<T, R> : never;
31
+ type _KeyofDeep<T extends Dict | ROList, R extends "leaf" | "parent" | "both" = "both", _K extends string = ""> = {
32
+ [P in keyof T]: T[P] extends infer TK ? NextKey<P & string, _K> extends infer N extends string ? TK extends Dict | ROList ? (R extends "leaf" ? never : N) | _KeyofDeep<TK, R, N> : R extends "parent" ? never : N : never : never;
33
+ }[T extends ROList ? number & keyof T : keyof T];
34
+ /**
35
+ * A version of {@linkcode Keyof} that returns only keys with values of type `U`.
36
+ *
37
+ * @example
38
+ * type R1 = KeyofType<{ a: 1, b: "b", c: 4 }, number>; // "a" | "c"
39
+ */
40
+ export type KeyofType<T extends Dict, U> = T extends T ? keyof {
41
+ [P in keyof T as T[P] extends U ? P : never]: T[P];
42
+ } : never;
43
+ /**
44
+ * A deep version of {@linkcode KeyofType}.
45
+ */
46
+ export type KeyofTypeDeep<T extends Dict | ROList, U> = T extends T ? _KeyofTypeDeep<T, U> : never;
47
+ type _KeyofTypeDeep<T extends Dict | ROList, U, _K extends string = ""> = {
48
+ [P in keyof T as T extends ROList ? P extends `${number}` ? T[P] extends U | Dict | ROList ? P : never : never : T[P] extends U | Dict | ROList ? P : never]: T[P] extends infer TK ? NextKey<P & string, _K> extends infer N extends string ? TK extends Dict | ROList ? _KeyofTypeDeep<TK, U, N> : N : never : never;
49
+ } extends infer T ? T extends ROList ? T[number] : T[keyof T] : never;
50
+ type NextKey<K extends string, _K extends string> = _K extends "" ? K : `${_K}.${K}`;
51
+ export {};
@@ -0,0 +1,95 @@
1
+ import type { Dict, Prettify } from "./utils";
2
+ /**
3
+ * Merge two objects by keeping all keys from `A` and `B`, on conflict, `B` wins.
4
+ *
5
+ * @remarks If `P` is `true`, then on conflict `B` wins but `A`'s shape is used.
6
+ *
7
+ * @example
8
+ * type A = { readonly a: 1, b: 2 };
9
+ * type B = { a: "<>", c: 4 };
10
+ *
11
+ * type AB1 = MergeUnion<A, B>; // { a: "<>", b: 2, c: 4 }
12
+ * type AB2 = MergeUnion<A, B, true>; // { readonly a: "<>", b: 2, c: 4 }
13
+ */
14
+ export type MergeUnion<A extends Dict, B extends Dict, P extends boolean = false> = A extends A ? B extends B ? Prettify<P extends true ? _MergeUnionPreserve<A, B> : _MergeUnion<A, B>> : never : never;
15
+ type _MergeUnion<A extends Dict, B extends Dict> = {
16
+ [P in keyof B as P extends keyof A ? P : never]: B[P];
17
+ } & _MergeSymDiff<A, B>;
18
+ type _MergeUnionPreserve<A extends Dict, B extends Dict> = {
19
+ [P in keyof A as P extends keyof B ? P : never]: B[P & keyof B];
20
+ } & _MergeSymDiff<A, B>;
21
+ /**
22
+ * Merge two objects by keeping all keys from `A`, on conflict, `B` wins.
23
+ *
24
+ * @remarks If `P` is `true`, then on conflict `B` wins but `A`'s shape is used.
25
+ *
26
+ * @example
27
+ * type A = { readonly a: 1, b: 2 };
28
+ * type B = { a: "<>", c: 4 };
29
+ *
30
+ * type AB1 = MergePatch<A, B>; // { a: "<>", b: 2 }
31
+ * type AB2 = MergePatch<A, B, true>; // { readonly a: "<>", b: 2 }
32
+ */
33
+ export type MergePatch<A extends Dict, B extends Dict, P extends boolean = false> = A extends A ? B extends B ? Prettify<P extends true ? _MergePatchPreserve<A, B> : _MergePatch<A, B>> : never : never;
34
+ type _MergePatch<A extends Dict, B extends Dict> = {
35
+ [P in keyof B as P extends keyof A ? P : never]: B[P];
36
+ } & _MergeDiff<A, B>;
37
+ type _MergePatchPreserve<A extends Dict, B extends Dict> = {
38
+ [P in keyof A as P extends keyof B ? P : never]: B[P & keyof B];
39
+ } & _MergeDiff<A, B>;
40
+ /**
41
+ * Merge two objects by keeping all keys from `A` that exist in `B`, values are taken from `B`.
42
+ *
43
+ * @remarks If `P` is `true`, then values will be taken from 'B', but `A`'s shape will be used.
44
+ *
45
+ * @example
46
+ * type A = { readonly a: 1, b: 2 };
47
+ * type B = { a: "<>", c: 4 };
48
+ *
49
+ * type AB1 = MergeInt<A, B>; // { a: "<>" }
50
+ * type AB2 = MergeInt<A, B, true>; // { readonly a: "<>" }
51
+ */
52
+ export type MergeInt<A extends Dict, B extends Dict, P extends boolean = false> = A extends A ? B extends B ? Prettify<P extends true ? _MergeIntPreserve<A, B> : _MergeInt<A, B>> : never : never;
53
+ type _MergeInt<A extends Dict, B extends Dict> = {
54
+ [P in keyof B as P extends keyof A ? P : never]: B[P];
55
+ };
56
+ type _MergeIntPreserve<A extends Dict, B extends Dict> = {
57
+ [P in keyof A as P extends keyof B ? P : never]: B[P & keyof B];
58
+ };
59
+ /**
60
+ * Merge two objects by keeping all keys from `A` that `B` lacks.
61
+ *
62
+ * @example
63
+ * type A = { readonly a: 1, b: 2 };
64
+ * type B = { a: "<>", c: 4 };
65
+ *
66
+ * type AB = MergeDiff<A, B>; // { b: 2 }
67
+ * type BA = MergeDiff<B, A>; // { c: 4 }
68
+ */
69
+ export type MergeDiff<A extends Dict, B extends Dict> = A extends A ? B extends B ? Prettify<_MergeDiff<A, B>> : never : never;
70
+ type _MergeDiff<A extends Dict, B extends Dict> = {
71
+ [P in keyof A as P extends keyof B ? never : P]: A[P];
72
+ };
73
+ /**
74
+ * Merge two objects by keeping all non-shared keys from both `A` and `B`.
75
+ *
76
+ * @example
77
+ * type A = { readonly a: 1, b: 2 };
78
+ * type B = { a: "<>", c: 4 };
79
+ *
80
+ * type AB = MergeSymDiff<A, B>; // { b: 2, c: 4 }
81
+ * type BA = AB;
82
+ */
83
+ export type MergeSymDiff<A extends Dict, B extends Dict> = A extends A ? B extends B ? Prettify<_MergeSymDiff<A, B>> : never : never;
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
+ };
89
+ /**
90
+ * Merge two objects using the given method.
91
+ *
92
+ * @see {@linkcode MergeUnion}, {@linkcode MergePatch}, {@linkcode MergeInt}, {@linkcode MergeDiff} and {@linkcode MergeSymDiff}
93
+ */
94
+ export type Merge<A extends Dict, B extends Dict, M extends "patch" | "patch-preserve" | "union" | "union-preserve" | "int" | "int-preserve" | "diff" | "symDiff"> = M extends "union" ? MergeUnion<A, B> : M extends "union-preserve" ? MergeUnion<A, B, true> : M extends "int" ? MergeInt<A, B> : M extends "int-preserve" ? MergeInt<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 "symDiff" ? MergeSymDiff<A, B> : never;
95
+ export {};
@@ -0,0 +1,57 @@
1
+ import type { Keyof } from "./keyof";
2
+ import type { LooseOmit } from "./pick";
3
+ import type { Dict, Prettify } from "./utils";
4
+ /**
5
+ * Makes all properties of `T` mutable.
6
+ */
7
+ export type Mutable<T> = {
8
+ -readonly [P in keyof T]: T[P];
9
+ };
10
+ /**
11
+ * Makes all properties of `T` required (deep).
12
+ */
13
+ export type RequiredDeep<T> = {
14
+ [P in keyof T]-?: T[P] extends Dict ? RequiredDeep<T[P]> : T[P];
15
+ };
16
+ /**
17
+ * Makes all properties of `T` optional (deep).
18
+ */
19
+ export type PartialDeep<T> = {
20
+ [P in keyof T]?: T[P] extends Dict ? PartialDeep<T[P]> : T[P];
21
+ };
22
+ /**
23
+ * Makes all properties of `T` readonly (deep).
24
+ */
25
+ export type ReadonlyDeep<T> = {
26
+ readonly [P in keyof T]: T[P] extends Dict ? ReadonlyDeep<T[P]> : T[P];
27
+ };
28
+ /**
29
+ * Makes all properties of `T` mutable (deep).
30
+ */
31
+ export type MutableDeep<T> = {
32
+ -readonly [P in keyof T]: T[P] extends Dict ? MutableDeep<T[P]> : T[P];
33
+ };
34
+ /**
35
+ * Makes a property of `T` required.
36
+ */
37
+ export type RequiredKey<T extends Dict, K extends Keyof<T>> = Prettify<{
38
+ [P in keyof T as P extends K ? P : never]-?: T[P];
39
+ } & LooseOmit<T, K>>;
40
+ /**
41
+ * Makes a property of `T` optional.
42
+ */
43
+ export type PartialKey<T extends Dict, K extends Keyof<T>> = Prettify<{
44
+ [P in keyof T as P extends K ? P : never]?: T[P];
45
+ } & LooseOmit<T, K>>;
46
+ /**
47
+ * Makes a property of `T` readonly.
48
+ */
49
+ export type ReadonlyKey<T extends Dict, K extends Keyof<T>> = Prettify<{
50
+ readonly [P in keyof T as P extends K ? P : never]: T[P];
51
+ } & LooseOmit<T, K>>;
52
+ /**
53
+ * Makes a property of `T` mutable.
54
+ */
55
+ export type MutableKey<T extends Dict, K extends Keyof<T>> = Prettify<{
56
+ -readonly [P in keyof T as P extends K ? P : never]: T[P];
57
+ } & LooseOmit<T, K>>;
@@ -0,0 +1,49 @@
1
+ import type { Suggest } from "../string";
2
+ import type { Keyof } from "./keyof";
3
+ import type { Dict } from "./utils";
4
+ /**
5
+ * Similar to the built-in {@linkcode Pick} type, except it lets you pass any keys
6
+ * and it will only pick the keys that actually exist in `T`.
7
+ *
8
+ * @example
9
+ * type R1 = Pick<{a: 1}, "a" | "b">; // { a: 1, b: unknown }
10
+ * type R2 = LoosePick<{a: 1}, "a" | "b">; // { a: 1 }
11
+ */
12
+ export type LoosePick<T extends Dict, K extends Suggest<Keyof<T>>> = T extends T ? {
13
+ [P in keyof T as P extends K ? P : never]: T[P];
14
+ } : never;
15
+ /**
16
+ * Similar to the built-in {@linkcode Pick} type, except it works differently when `T` is a union of objects.
17
+ *
18
+ * @example
19
+ * type R1 = Pick<{a: 1} | {b: 2}, "a">; // { a: unknown } // + Error
20
+ * type R2 = StrictPick<{a: 1} | {b: 2}, "a">; // { a: 1 } | {}
21
+ */
22
+ export type StrictPick<T extends Dict, K extends Keyof<T>> = LoosePick<T, K>;
23
+ /**
24
+ * Similar to the built-in {@linkcode Omit} type, except it suggests possible keys
25
+ * and it works differently when `T` is a union of objects.
26
+ *
27
+ * @example
28
+ * type R1 = Omit<{a: 1, b: 2}, "a">; // { b: 2 }
29
+ * type R2 = LooseOmit<{a: 1, b: 2}, "a">; // { b: 2 } // + Auto-completion for 'a'
30
+ *
31
+ * type R3 = Omit<{a: 1} | {b: 2}, "a">; // {}
32
+ * type R4 = LooseOmit<{a: 1} | {b: 2}, "a">; // {} | {b: 2}
33
+ */
34
+ export type LooseOmit<T extends Dict, K extends Suggest<Keyof<T>>> = T extends T ? {
35
+ [P in keyof T as P extends K ? never : P]: T[P];
36
+ } : never;
37
+ /**
38
+ * Similar to the built-in {@linkcode Omit} type, except it enforces passing an existing key
39
+ * and it works differently when `T` is a union of objects.
40
+ *
41
+ * @example
42
+ * type R1 = Omit<{a: 1}, "z">; // {a: 1}
43
+ * type R2 = StrictOmit<{a: 1}, "z">; // {a: 1} // + Error saying 'z' is not an existing key
44
+ *
45
+ * type R3 = Omit<{a: 1} | {b: 2}, "a">; // {} // + Error
46
+ * type R4 = StrictOmit<{a: 1} | {b: 2}, "a">; // {} | {b: 2}
47
+ *
48
+ */
49
+ export type StrictOmit<T extends Dict, K extends Keyof<T>> = LooseOmit<T, K>;
@@ -0,0 +1,38 @@
1
+ import type { EnsurePrefix, EnsureSuffix, Prefix, Stringify, Suffix, UnPrefix, UnSuffix } from "../string";
2
+ import type { Dict } from "./utils";
3
+ /**
4
+ * Adds a prefix to all keys of `T`.
5
+ */
6
+ export type PrefixKeys<T extends Dict, P extends string> = {
7
+ [K in keyof T as K extends string | number ? Prefix<Stringify<K>, P> : K]: T[K];
8
+ };
9
+ /**
10
+ * Adds a prefix to all keys of `T`, but only if the key does not already start with the prefix.
11
+ */
12
+ export type PrefixKeysEnsure<T extends Dict, P extends string> = {
13
+ [K in keyof T as K extends string | number ? EnsurePrefix<Stringify<K>, P> : K]: T[K];
14
+ };
15
+ /**
16
+ * Removes a prefix from all keys of `T`, if it exists.
17
+ */
18
+ export type UnPrefixKeys<T extends Dict, P extends string> = {
19
+ [K in keyof T as K extends string | number ? UnPrefix<Stringify<K>, P> : K]: T[K];
20
+ };
21
+ /**
22
+ * Adds a suffix to all keys of `T`.
23
+ */
24
+ export type SuffixKeys<T extends Dict, S extends string> = {
25
+ [K in keyof T as K extends string | number ? Suffix<Stringify<K>, S> : K]: T[K];
26
+ };
27
+ /**
28
+ * Adds a suffix to all keys of `T`, but only if the key does not already end with the suffix.
29
+ */
30
+ export type SuffixKeysEnsure<T extends Dict, S extends string> = {
31
+ [K in keyof T as K extends string | number ? EnsureSuffix<Stringify<K>, S> : K]: T[K];
32
+ };
33
+ /**
34
+ * Removes a suffix from all keys of `T`, if it exists.
35
+ */
36
+ export type UnSuffixKeys<T extends Dict, S extends string> = {
37
+ [K in keyof T as K extends string | number ? UnSuffix<Stringify<K>, S> : K]: T[K];
38
+ };
@@ -0,0 +1,20 @@
1
+ import type { Keyof } from "./keyof";
2
+ import type { Dict, DictKey } from "./utils";
3
+ /**
4
+ * Renames a key of `T`.
5
+ *
6
+ * @example
7
+ * type R1 = RenameKey<{rgb: RGB, a: number}, "a", "alpha">; // { rgb: RGB, alpha: number }
8
+ */
9
+ export type RenameKey<T extends Dict, K extends Keyof<T>, N extends DictKey> = T extends T ? {
10
+ [P in keyof T as P extends K ? N : P]: T[P];
11
+ } : never;
12
+ /**
13
+ * Renames multiple keys of `T` using a key-to-new-key object.
14
+ *
15
+ * @example
16
+ * type R1 = RenameKeys<{f: string, b: string, c: 4}, {f: "foo", b: "bar"}>; // { foo: string, bar: string, c: 4 }
17
+ */
18
+ export type RenameKeys<T extends Dict, M extends Partial<Dict<Keyof<T>, DictKey>>> = T extends T ? M extends M ? {
19
+ [P in keyof T as P extends keyof M ? M[P] & DictKey : P]: T[P];
20
+ } : never : never;
@@ -0,0 +1,40 @@
1
+ import type { ROList } from "../list";
2
+ import type { HasType } from "../other";
3
+ import type { KnownTuple } from "../tuple/utils";
4
+ /**
5
+ * Types that can be used as keys for an object.
6
+ */
7
+ export type DictKey = string | number | symbol;
8
+ /**
9
+ * An alias for `Record<K, V>` with defaults.
10
+ */
11
+ export type Dict<K extends DictKey = string, V = unknown> = Record<K, V>;
12
+ /**
13
+ * Removes `[x: string]: unknown` from an object.
14
+ *
15
+ * @example
16
+ * type Config = { foo: string, bar: boolean, [x: string]: string };
17
+ * 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
+ */
22
+ export type Known<T extends Dict | ROList> = T extends T ? T extends ROList ? KnownTuple<T> : {
23
+ [P in keyof T as HasType<DictKey, P> extends true ? never : P]: T[P];
24
+ } : never;
25
+ /**
26
+ * Prettifies an object.
27
+ *
28
+ * @example
29
+ * type R1 = Readonly<{ a: 1 } | { b: 2 }>; // Readonly<{ a: 1 }> | Readonly<{ b: 2 }>
30
+ * type R2 = Prettify<R1>; // { readonly a: 1 } | { readonly b: 2 }
31
+ */
32
+ export type Prettify<T> = {
33
+ [P in keyof T]: T[P];
34
+ } & {};
35
+ /**
36
+ * A deep version of {@linkcode Prettify}.
37
+ */
38
+ export type PrettifyDeep<T> = {
39
+ [P in keyof T]: T[P] extends infer V ? (V extends Dict | ROList ? PrettifyDeep<V> : V) : never;
40
+ } & {};
@@ -0,0 +1 @@
1
+ export * from "./utils";
@@ -0,0 +1,39 @@
1
+ import type { ROList } from "../list";
2
+ import type { NormalizeIndex } from "../_internal";
3
+ /**
4
+ * Creates a function type with the given arguments and return type.
5
+ *
6
+ * @example
7
+ * type F1 = Func; // () => any
8
+ * type F2 = Func<[string]>; // (args_0: string) => any
9
+ * type F3 = Func<[], string>; // () => string
10
+ * type F4 = Func<[name: string], string>; // (name: string) => string
11
+ */
12
+ export type Func<A extends ROList = any[], R = any> = (...args: A) => R;
13
+ /**
14
+ * An alias for `T | Func<A, T>`.
15
+ *
16
+ * @see {@linkcode Func}
17
+ */
18
+ export type OrFunc<T, A extends ROList = []> = T | Func<A, T>;
19
+ /**
20
+ * An alias for `() => void`.
21
+ */
22
+ export type VoidFn = Func<[], void>;
23
+ /**
24
+ * An alias for {@linkcode Parameters<F>}.
25
+ */
26
+ export type Params<F extends Func> = Parameters<F>;
27
+ /**
28
+ * Gets the parameter at the given index. Negative indices are supported.
29
+ *
30
+ * @example
31
+ * type Fn = (a: string, b: number) => void;
32
+ * type R1 = Param<Fn, 0>; // string
33
+ * type R2 = Param<Fn, -1>; // number
34
+ */
35
+ export type Param<F extends Func, I extends number> = Params<F>[NormalizeIndex<I, Params<F>>];
36
+ /**
37
+ * An alias for {@linkcode ReturnType<F>}.
38
+ */
39
+ export type RT<F extends Func> = ReturnType<F>;
@@ -0,0 +1,7 @@
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";
@@ -0,0 +1 @@
1
+ export * from "./utils";
@@ -0,0 +1,16 @@
1
+ /**
2
+ * An alias for `T[]`.
3
+ */
4
+ export type List<T = unknown> = T[];
5
+ /**
6
+ * An alias for `readonly T[]`.
7
+ */
8
+ export type ROList<T = unknown> = readonly T[];
9
+ /**
10
+ * An alias for `T | T[]`.
11
+ */
12
+ export type OrList<T = unknown> = T | List<T>;
13
+ /**
14
+ * An alias for `T | readonly T[]`.
15
+ */
16
+ export type OrROList<T = unknown> = T | ROList<T>;
@@ -0,0 +1,81 @@
1
+ import type { IsNegative } from "../_internal";
2
+ import type { UnPrefix, Stringify } from "../string";
3
+ import type { ToNumber } from "./utils";
4
+ type Abs<T extends string | number> = UnPrefix<Stringify<T>, "-"> extends infer R extends string ? T extends string ? R : ToNumber<R> : never;
5
+ type IncrementMap = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
6
+ /**
7
+ * A type that represents the digits 0 through 9.
8
+ */
9
+ export type Digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
10
+ type IncrementSpecialCases = {
11
+ "9": "10";
12
+ "-1": "0";
13
+ };
14
+ type DecrementMap = {
15
+ 1: 0;
16
+ 2: 1;
17
+ 3: 2;
18
+ 4: 3;
19
+ 5: 4;
20
+ 6: 5;
21
+ 7: 6;
22
+ 8: 7;
23
+ 9: 8;
24
+ 0: 9;
25
+ };
26
+ type IncrementByOne<T extends string> = T extends keyof IncrementSpecialCases ? IncrementSpecialCases[T] : IsNegative<T> extends true ? `-${DecrementByOne<Abs<T>>}` : T extends `${infer First}${Digit}` ? T extends `${First}${infer D extends Digit}` ? D extends 9 ? `${IncrementByOne<First>}0` : `${First}${IncrementMap[D]}` : never : never;
27
+ type DecrementSpecialCases = {
28
+ "0": "-1";
29
+ };
30
+ type DecrementByOne<T extends string> = T extends keyof DecrementSpecialCases ? DecrementSpecialCases[T] : IsNegative<T> extends true ? `-${IncrementByOne<Abs<T>>}` : T extends `${infer First}${Digit}` ? T extends `${First}${infer D extends Digit}` ? D extends 0 ? First extends "1" ? "9" : `${DecrementByOne<First>}9` : `${First}${DecrementMap[D]}` : never : never;
31
+ type IncrementByTenSpecialCases = {
32
+ "-1": "9";
33
+ "-2": "8";
34
+ "-3": "7";
35
+ "-4": "6";
36
+ "-5": "5";
37
+ "-6": "4";
38
+ "-7": "3";
39
+ "-8": "2";
40
+ "-9": "1";
41
+ "-10": "0";
42
+ };
43
+ type IncrementByTen<T extends string> = T extends keyof IncrementByTenSpecialCases ? IncrementByTenSpecialCases[T] : IsNegative<T> extends true ? `-${DecrementByTen<Abs<T>>}` : T extends `${infer First}${Digit}` ? T extends `${First}${infer D extends Digit}` ? First extends "" | "-" ? `1${D}` : `${IncrementByOne<First>}${D}` : never : never;
44
+ type DecrementByTenSpecialCases = {
45
+ "1": "-9";
46
+ "2": "-8";
47
+ "3": "-7";
48
+ "4": "-6";
49
+ "5": "-5";
50
+ "6": "-4";
51
+ "7": "-3";
52
+ "8": "-2";
53
+ "9": "-1";
54
+ "10": "0";
55
+ };
56
+ type DecrementByTen<T extends string> = T extends keyof DecrementByTenSpecialCases ? DecrementByTenSpecialCases[T] : IsNegative<T> extends true ? `-${IncrementByTen<Abs<T>>}` : T extends `${infer First}${Digit}` ? T extends `${First}${infer D extends Digit}` ? First extends "" | "-" ? `9${D}` : First extends "1" ? `${D}` : `${DecrementByOne<First>}${D}` : never : never;
57
+ type _Increment<T extends string, C extends string> = C extends "0" ? T : C extends `${string}${Digit}${Digit}` ? _Increment<IncrementByTen<T>, DecrementByTen<C>> : C extends `${string}${Digit}` ? _Increment<IncrementByOne<T>, DecrementByOne<C>> : never;
58
+ type _Decrement<T extends string, C extends string> = C extends "0" ? T : C extends `${string}${Digit}${Digit}` ? _Decrement<DecrementByTen<T>, DecrementByTen<C>> : C extends `${string}${Digit}` ? _Decrement<DecrementByOne<T>, DecrementByOne<C>> : never;
59
+ /**
60
+ * Increments a number by a given amount.
61
+ *
62
+ * @example
63
+ * type R1 = Increment<1>; // 2
64
+ * type R2 = Increment<1, 2>; // 3
65
+ * type R3 = Increment<1, -1>; // 0
66
+ * type R4 = Increment<-1, 1>; // 0
67
+ * type R5 = Increment<-1, -1>; // -2
68
+ */
69
+ export type Increment<N extends number, C extends number = 1> = (IsNegative<C> extends true ? _Decrement<Stringify<N>, Abs<Stringify<C>>> : _Increment<Stringify<N>, Stringify<C>>) extends infer R extends string ? ToNumber<R> : never;
70
+ /**
71
+ * Decrements a number by a given amount.
72
+ *
73
+ * @example
74
+ * type R1 = Decrement<1>; // 0
75
+ * type R2 = Decrement<1, 2>; // -1
76
+ * type R3 = Decrement<1, -1>; // 2
77
+ * type R4 = Decrement<-1, 1>; // -2
78
+ * type R5 = Decrement<-1, -1>; // 0
79
+ */
80
+ export type Decrement<N extends number, C extends number = 1> = (IsNegative<C> extends true ? _Increment<Stringify<N>, Abs<Stringify<C>>> : _Decrement<Stringify<N>, Stringify<C>>) extends infer R extends string ? ToNumber<R> : never;
81
+ export {};
@@ -0,0 +1,2 @@
1
+ export * from "./increment";
2
+ export * from "./utils";
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Converts a number in string form to its number representation.
3
+ *
4
+ * @example
5
+ * type R1 = ToNumber<"123">; // 123
6
+ */
7
+ export type ToNumber<T extends string> = T extends `${infer N extends number}` ? N : never;
@@ -0,0 +1,2 @@
1
+ export * from "./utils";
2
+ export * from "./validation";
@@ -0,0 +1,39 @@
1
+ import type { AreEqual, IsAny, IsNever, IsSubtype, IsUnknown } from "./validation";
2
+ /**
3
+ * An alias for `any`.
4
+ */
5
+ export type Valid = any;
6
+ /**
7
+ * @example
8
+ * // Suppose you have a type like this
9
+ * type Token = "+" | "-" | "*" | "/";
10
+ *
11
+ * // And you want to give each member an alias:
12
+ * const tk = {
13
+ * plus: "+",
14
+ * minus: "-",
15
+ * times: "*",
16
+ * divide: "/",
17
+ * } satisfies Dict<string, Token>;
18
+ *
19
+ * // To make sure that all tokens have an alias, you can use something like this:
20
+ * {
21
+ * type Check = Assert<AreEqual<Valueof<typeof tk>, Token>>;
22
+ * }
23
+ * // You'll see an error if there's a missing/extra token
24
+ * // Note: AreEqual and Valueof are other utils provided by this package
25
+ */
26
+ export type Assert<T extends E, E = true> = T;
27
+ /**
28
+ * Negates a boolean.
29
+ */
30
+ export type Not<T extends boolean> = T extends true ? false : true;
31
+ /**
32
+ * Similar to the built-in {@linkcode Extract Extract<T, U>}, except it return `Extract<U, T>` (or simply `U`)
33
+ * when `T` is `unknown`, `any` or a **primitive type** (string, number, boolean).
34
+ */
35
+ export type SmartExtract<T, U> = T extends T ? IsUnknown<T> | IsAny<T> | IsPrimitive<T> extends false ? Extract<T, U> : Extract<U, T> : never;
36
+ type IsPrimitive<T> = IsAny<T> | IsUnknown<T> | IsNever<T> extends false ? _IsPrimitive<_Wrap<T>> : false;
37
+ type _IsPrimitive<T> = T extends T ? AreEqual<T, [string]> | AreEqual<T, [number]> | AreEqual<T, [boolean]> extends false ? false : true : never;
38
+ 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;
39
+ export {};
@@ -0,0 +1,57 @@
1
+ import type { Not } from "./utils";
2
+ /**
3
+ * Checks if a type is `unknown`.
4
+ */
5
+ export type IsUnknown<T> = IsAny<T> extends true ? false : unknown extends T ? true : false;
6
+ /**
7
+ * Checks if a type is `any`.
8
+ */
9
+ export type IsAny<T> = ":)" extends ":(" & T ? true : false;
10
+ /**
11
+ * Checks if a type is `never`.
12
+ */
13
+ export type IsNever<T> = [T] extends [never] ? true : false;
14
+ /**
15
+ * An alias for `T extends U ? true : false`.
16
+ *
17
+ * - Used to chain multiple checks together.
18
+ * - Useful to reduce nesting level for complex types.
19
+ * - Can require some thinking before using it correctly.
20
+ * - See the last comment in the example below.
21
+ * - This type is mostly for personal use :)
22
+ *
23
+ * @example
24
+ * // Instead of
25
+ * type Example<A, B> = A extends Dict ? B extends Dict ? "Do something" : never : never;
26
+ *
27
+ * // You can do
28
+ * type Version2<A, B> = IsSubtype<A, Dict> | IsSubtype<B, Dict> extends true ? "Do something" : never;
29
+ *
30
+ * // Note that TypeScript doesn't know about these checks, which will likely cause errors saying A and B are not objects inside the "Do Something" branch.
31
+ */
32
+ export type IsSubtype<T, U> = T extends U ? true : false;
33
+ /**
34
+ * A negated version of {@linkcode IsSubtype}.
35
+ */
36
+ export type IsNotSubtype<T, U> = Not<IsSubtype<T, U>>;
37
+ /**
38
+ * Checks whether a union contains a specific type.
39
+ *
40
+ * @example
41
+ * type R1 = HasType<"foo" | "bar", "foo">; // true
42
+ * type R2 = HasType<"foo" | "bar", "baz">; // false
43
+ */
44
+ export type HasType<U, T> = (U extends T ? true : false) extends infer R ? IsSubtype<R, true> | IsSubtype<[boolean], [R]> extends false ? false : true : never;
45
+ /**
46
+ * A negated version of {@linkcode HasType}.
47
+ */
48
+ export type LacksType<U, T> = Not<HasType<U, T>>;
49
+ /**
50
+ * Checks if two types are equal. Not just that they extend each other,
51
+ * but are exactly the same (at least until a bug proves otherwise).
52
+ */
53
+ export type AreEqual<A, B> = (<T>() => T extends A ? ":)" : ":(") extends <T>() => (T extends B ? ":)" : ":(") ? true : false;
54
+ /**
55
+ * A negated version of {@linkcode AreEqual}.
56
+ */
57
+ export type AreNotEqual<A, B> = Not<AreEqual<A, B>>;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Extracts from a union of strings those that start with a given prefix.
3
+ *
4
+ * @example
5
+ * type Keys = KeyofDeep<Config>; // 'defaults.a' | 'defaults.b' | 'messages.error' | 'messages.success'
6
+ * type R1 = ExtractIfStartsWith<Keys, 'defaults'>; // 'defaults.a' | 'defaults.b'
7
+ * type R2 = ExtractIfStartsWith<Keys, 'messages'>; // 'messages.error' | 'messages.success'
8
+ */
9
+ export type ExtractIfStartsWith<T extends string, P extends string> = T extends `${P}${string}` ? T : never;
10
+ /**
11
+ * Extracts from a union of strings those that end with a given suffix.
12
+ */
13
+ export type ExtractIfEndsWith<T extends string, P extends string> = T extends `${string}${P}` ? T : never;
14
+ /**
15
+ * Extracts from a union of strings those that contain a given substring.
16
+ */
17
+ export type ExtractIfContains<T extends string, P extends string> = T extends `${string}${P}${string}` ? T : never;
18
+ /**
19
+ * Excludes from a union of strings those that start with a given prefix.
20
+ */
21
+ export type ExcludeIfStartsWith<T extends string, S extends string> = T extends `${S}${string}` ? never : T;
22
+ /**
23
+ * Excludes from a union of strings those that end with a given suffix.
24
+ */
25
+ export type ExcludeIfEndsWith<T extends string, S extends string> = T extends `${string}${S}` ? never : T;
26
+ /**
27
+ * Excludes from a union of strings those that contain a given substring.
28
+ */
29
+ export type ExcludeIfContains<T extends string, S extends string> = T extends `${string}${S}${string}` ? never : T;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Returns the first character in a given string.
3
+ */
4
+ export type FirstChar<T extends string> = T extends `${infer U extends string}${string}` ? U : never;
5
+ /**
6
+ * Returns the last character in a given string.
7
+ *
8
+ * @remarks This type will run `T["length"]` times to reach the last character.
9
+ */
10
+ export type LastChar<T extends string> = T extends `${string}${infer U extends string}` ? U extends "" ? T : LastChar<U> : never;
@@ -0,0 +1,4 @@
1
+ export * from "./extract";
2
+ export * from "./prefix";
3
+ export * from "./firstChar";
4
+ export * from "./utils";
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Adds a prefix to a string.
3
+ */
4
+ export type Prefix<T extends string, P extends string> = `${P}${T}`;
5
+ /**
6
+ * Adds a prefix to a string, but only if the string does not already start with the prefix.
7
+ */
8
+ export type EnsurePrefix<T extends string, P extends string> = T extends `${P}${string}` ? T : Prefix<T, P>;
9
+ /**
10
+ * Removes a prefix from a string (if it exists).
11
+ */
12
+ export type UnPrefix<T extends string, P extends string> = T extends `${P}${infer U extends string}` ? U : T;
13
+ /**
14
+ * Adds a suffix to a string.
15
+ */
16
+ export type Suffix<T extends string, S extends string> = `${T}${S}`;
17
+ /**
18
+ * Adds a suffix to a string, but only if the string does not already end with the suffix.
19
+ */
20
+ export type EnsureSuffix<T extends string, S extends string> = T extends `${infer U extends string}${S}` ? U : Suffix<T, S>;
21
+ /**
22
+ * Removes a suffix from a string (if it exists).
23
+ */
24
+ export type UnSuffix<T extends string, S extends string> = T extends `${infer U extends string}${S}` ? U : T;
@@ -0,0 +1,6 @@
1
+ import type { ROList } from "../list";
2
+ export type Split<T extends string, S extends string> = T extends "" ? [] : T extends `${infer L}${S}${infer R}` ? [L, ...Split<R, S>] : [T];
3
+ export type Join<T extends ROList<string>, S extends string = ""> = T extends readonly [
4
+ infer F extends string,
5
+ ...infer R extends ROList<string>
6
+ ] ? R["length"] extends 0 ? F : `${F}${S}${Join<R, S>}` : "";
@@ -0,0 +1,26 @@
1
+ /**
2
+ * A type that represents values that can be converted to strings.
3
+ */
4
+ export type Stringable = string | number | boolean | bigint | null | undefined;
5
+ /**
6
+ * Generates a type that suggests a pre-defined list of choices while accepting any other string.
7
+ *
8
+ * @example
9
+ * type Fruit = Suggest<"Apple" | "Orange">;
10
+ *
11
+ * const f1: Fruit = "Apple"; // Valid + Auto-completion
12
+ * const f2: Fruit = "Banana"; // Valid
13
+ */
14
+ export type Suggest<T extends string> = T | (string & {});
15
+ /**
16
+ * Converts a {@linkcode Stringable} to its string representation.
17
+ *
18
+ * @example
19
+ * type R1 = Stringify<123>; // "123"
20
+ * type R2 = Stringify<{ a: 1 }>; // never
21
+ */
22
+ export type Stringify<T> = T extends Stringable ? `${T}` : never;
23
+ /**
24
+ * Concatenates two strings with an optional separator.
25
+ */
26
+ export type Concat<A extends string, B extends string, S extends string = ""> = `${A}${S}${B}`;
@@ -0,0 +1,2 @@
1
+ export * from "./push";
2
+ export * from "./utils";
@@ -0,0 +1,60 @@
1
+ import type { As, NormalizeIndex } from "../_internal";
2
+ import type { Mutable } from "../dict";
3
+ import type { List, ROList } from "../list";
4
+ import type { Increment, ToNumber } from "../number";
5
+ import type { IsSubtype } from "../other";
6
+ import type { EnsurePrefix, Stringify } from "../string";
7
+ import type { Tuple } from "./utils";
8
+ /**
9
+ * Pushes an element to the end of a tuple.
10
+ *
11
+ * @remarks The tuple's shape is preserved.
12
+ *
13
+ * @example
14
+ * type R1 = Push<[1, 2, 3], 4>; // [1, 2, 3, 4]
15
+ * type R2 = Push<[1, 2, 3?], 4>; // [1, 2, 3?, 4?]
16
+ */
17
+ export type Push<T extends ROList, E, S extends boolean = false> = [
18
+ ...Required<T>,
19
+ ...(IsSubtype<S, true> | IsSubtype<E, ROList> extends true ? As<ROList, E> : [E])
20
+ ] extends infer Result extends ROList ? MatchTuple<Result, T> : never;
21
+ /**
22
+ * Pops an element from the end of a tuple.
23
+ *
24
+ * @example
25
+ * type R1 = Pop<[1, 2, 3]>; // [1, 2]
26
+ * type R2 = Pop<[1, 2, 3?]>; // [1, 2]
27
+ */
28
+ export type Pop<T extends ROList, N extends number = 1> = Required<T> extends readonly [...infer Head extends ROList, ...Tuple<N>] ? MatchTuple<Head, T> : [];
29
+ /**
30
+ * Adds an element to the beginning of a tuple.
31
+ *
32
+ * @example
33
+ * type R1 = Unshift<[1, 2, 3], 4>; // [4, 1, 2, 3]
34
+ * type R2 = Unshift<[1, 2, 3?], 4>; // [4, 1, 2, 3?]
35
+ */
36
+ export type Unshift<T extends ROList, E, S extends boolean = false> = [
37
+ ...(IsSubtype<S, true> | IsSubtype<E, ROList> extends true ? As<ROList, E> : [E]),
38
+ ...Required<T>
39
+ ] extends infer Result extends ROList ? MatchTuple<Result, T, IsSubtype<S, true> | IsTuple<E> extends true ? As<ROList, E>["length"] : 1> : never;
40
+ /**
41
+ * Removes an element from the beginning of a tuple.
42
+ *
43
+ * @example
44
+ * type R1 = Shift<[1, 2, 3]>; // [2, 3]
45
+ * type R2 = Shift<[1, 2, 3?]>; // [2, 3?]
46
+ */
47
+ 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 {};
@@ -0,0 +1,45 @@
1
+ import type { List, ROList } from "../list";
2
+ /**
3
+ * Creates a tuple of the given length and parameter type.
4
+ *
5
+ * @example
6
+ * type R1 = Tuple<1>; // [unknown]
7
+ * type RGB = Tuple<3, number>; // [number, number, number]
8
+ */
9
+ export type Tuple<L extends number, T = unknown> = L extends L ? _Tuple<L, T> : never;
10
+ type _Tuple<L extends number, T, _L extends List = []> = _L["length"] extends L ? [] : [T, ..._Tuple<L, T, [..._L, any]>];
11
+ /**
12
+ * A readonly version of {@linkcode Tuple}.
13
+ */
14
+ export type ROTuple<L extends number = -1, T = unknown> = Readonly<Tuple<L, T>>;
15
+ /**
16
+ * A tuple with at least `L` elements.
17
+ *
18
+ * @example
19
+ * type R1 = NonEmptyTuple; // [unknown, ...unknown[]]
20
+ * type R2 = NonEmptyTuple<number>; // [number, ...number[]]
21
+ * type R3 = NonEmptyTuple<number, 2>; // [number, number, ...number[]]
22
+ */
23
+ export type NonEmptyTuple<T = unknown, L extends number = 1> = [...Tuple<L, T>, ...T[]];
24
+ /**
25
+ * A readonly version of {@linkcode NonEmptyTuple}.
26
+ */
27
+ export type NonEmptyROTuple<T = unknown, L extends number = 1> = [
28
+ ...ROTuple<L, T>,
29
+ ...(readonly T[])
30
+ ];
31
+ /**
32
+ * Returns the indexes of a tuple.
33
+ *
34
+ * @example
35
+ * type R1 = KeyofTuple<[1, 2, 3]>; // "0" | "1" | "2"
36
+ */
37
+ export type KeyofTuple<T extends readonly unknown[]> = Exclude<keyof T, keyof any[]>;
38
+ /**
39
+ * Returns the guaranteed elements of a tuple.
40
+ *
41
+ * @example
42
+ * type R1 = KnownTuple<[number, number, ...number[]]>; // [number, number]
43
+ */
44
+ export type KnownTuple<T extends ROList> = T extends readonly [infer F, ...infer R] ? [F, ...KnownTuple<R>] : [];
45
+ export {};
package/package.json CHANGED
@@ -1,6 +1,47 @@
1
1
  {
2
2
  "name": "@inzero/ts-ut",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "TypeScript Utility Types",
5
+ "keywords": [
6
+ "typescript",
7
+ "utility",
8
+ "types"
9
+ ],
10
+ "license": "MIT",
11
+ "author": "InZero",
12
+ "exports": {
13
+ ".": "./dist/index.d.ts",
14
+ "./string": "./dist/string/index.d.ts",
15
+ "./number": "./dist/number/index.d.ts",
16
+ "./func": "./dist/func/index.d.ts",
17
+ "./other": "./dist/other/index.d.ts",
18
+ "./list": "./dist/list/index.d.ts",
19
+ "./tuple": "./dist/tuple/index.d.ts",
20
+ "./dict": "./dist/dict/index.d.ts"
21
+ },
22
+ "main": "dist/index.d.ts",
23
+ "files": [
24
+ "dist",
25
+ "package.json",
26
+ "README.md",
27
+ "LICENSE",
28
+ "CHANGELOG.md"
29
+ ],
30
+ "devDependencies": {
31
+ "prettier": "^3.9.10",
32
+ "prettier-plugin-packagejson": "^3.0.2",
33
+ "typescript": "^7.0.2"
34
+ },
35
+ "devEngines": {
36
+ "packageManager": {
37
+ "name": "pnpm",
38
+ "version": "12.11.2",
39
+ "onFail": "download"
40
+ }
41
+ },
42
+ "scripts": {
43
+ "build": "tsc",
44
+ "dev": "tsc --watch",
45
+ "format": "prettier --write ."
46
+ }
6
47
  }