@inzero/ts-ut 0.3.0 → 0.4.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 CHANGED
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.4.0] 2026-11-14
4
+
5
+ ### Changed
6
+
7
+ - `MutableDeep`, `ReadonlyDeep`, `PartialDeep` and `RequiredDeep` now return prettified objects.
8
+
9
+ ### Added
10
+
11
+ - Added `PickMutable`, `PickReadonly`, `PickOptional`, `PickRequired` and their deep versions.
12
+ - Added internal utils `IsOptionalKey` and `IsReadonlyKey`.
13
+
3
14
  ## [0.3.0] 2026-11-10
4
15
 
5
16
  This update focuses on:
@@ -1,7 +1,7 @@
1
- import type { Mutable } from "./dict";
1
+ import type { DictKey, Mutable } from "./dict";
2
2
  import type { List, ROList } from "./list";
3
3
  import type { Decrement, Increment, ToNumber } from "./number";
4
- import type { AreEqual, IsAny, IsNever, IsSubtype, IsUnknown } from "./other";
4
+ import type { AreEqual, AreNotEqual, IsAny, IsNever, IsSubtype, IsUnknown } from "./other";
5
5
  import type { EnsurePrefix, Stringify } from "./string";
6
6
  export type IsNegative<T extends string | number> = T extends T ? StartsWith<Stringify<T>, "-"> extends true ? true : false : never;
7
7
  export type IsNumber<T extends string> = ToNumber<T> extends number ? true : false;
@@ -24,4 +24,11 @@ type _IsPrimitive<T> = T extends T ? AreEqual<T, [string]> | AreEqual<T, [number
24
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
25
  declare const __TS_UT: unique symbol;
26
26
  export type TS_UT = typeof __TS_UT;
27
+ export type IsOptionalKey<T, K extends DictKey> = T extends T ? K extends keyof T ? {} extends Pick<T, K> ? true : false : never : never;
28
+ export type IsReadonlyKey<T, K extends DictKey> = T extends T ? K extends keyof T ? _IsReadonlyKey<T, K> : never : never;
29
+ type _IsReadonlyKey<T, K extends keyof T> = AreNotEqual<{
30
+ [P in K]: T[P];
31
+ }, {
32
+ -readonly [P in K]: T[P];
33
+ }>;
27
34
  export {};
@@ -12,25 +12,25 @@ export type Mutable<T> = {
12
12
  */
13
13
  export type RequiredDeep<T> = {
14
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
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
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
32
  -readonly [P in keyof T]: MutableDeep<T[P]>;
33
- };
33
+ } & {};
34
34
  /**
35
35
  * Makes a property of `T` required.
36
36
  */
@@ -1,5 +1,7 @@
1
+ import type { IsOptionalKey, IsReadonlyKey } from "../_internal";
2
+ import type { HasType, Not } from "../other";
1
3
  import type { Keys } from "./keyof";
2
- import type { Known, SuggestKey } from "./utils";
4
+ import type { Dict, Known, SuggestKey } from "./utils";
3
5
  /**
4
6
  * A distributive/loose version of the built-in {@linkcode Pick}.
5
7
  *
@@ -43,3 +45,87 @@ export type LooseOmit<T, K extends SuggestKey<Keys<Known<T>>>> = T extends T ? {
43
45
  *
44
46
  */
45
47
  export type StrictOmit<T, K extends Keys<T>> = LooseOmit<T, K>;
48
+ /**
49
+ * Pick optional properties of `T`.
50
+ *
51
+ * @example
52
+ * type R1 = PickOptional<{ a: 1, b: 2, c?: 4 }>; // { c?: 4 }
53
+ */
54
+ export type PickOptional<T> = {
55
+ [P in keyof T as IsOptionalKey<T, P> extends true ? P : never]: T[P];
56
+ };
57
+ /**
58
+ * Recursively pick optional properties of `T`.
59
+ *
60
+ * @example
61
+ * type Input = { a?: 1, b: { c?: 4, d: 5 } }
62
+ *
63
+ * type R1 = PickOptional<Input>; // { a?: 1 }
64
+ * type R2 = PickOptionalDeep<Input>; // { a?: 2, b: { c?: 4 } }
65
+ */
66
+ export type PickOptionalDeep<T> = {
67
+ [P in keyof T as IsOptionalKey<T, P> | HasType<T[P], Dict> extends false ? never : P]: T[P] extends infer TP ? (TP extends Dict ? PickOptionalDeep<TP> : TP) : never;
68
+ } & {};
69
+ /**
70
+ * Pick required properties of `T`.
71
+ *
72
+ * @example
73
+ * type R1 = PickRequired<{ a: 1, b: 2, c?: 4 }>; // { a: 1, b: 2 }
74
+ */
75
+ export type PickRequired<T> = {
76
+ [P in keyof T as IsOptionalKey<T, P> extends true ? never : P]: T[P];
77
+ };
78
+ /**
79
+ * Recursively pick required properties of `T`.
80
+ *
81
+ * @example
82
+ * type Input = { a?: 1, b: { c?: 4, d: 5 } }
83
+ *
84
+ * type R1 = PickRequired<Input>; // { b: { c?: 4, d: 5 } }
85
+ * type R2 = PickRequiredDeep<Input>; // { b: { d: 5 } }
86
+ */
87
+ export type PickRequiredDeep<T> = {
88
+ [P in keyof T as Not<IsOptionalKey<T, P>> | HasType<T[P], Dict> extends false ? never : P]: T[P] extends infer TP ? (TP extends Dict ? PickRequiredDeep<TP> : TP) : never;
89
+ } & {};
90
+ /**
91
+ * Pick readonly properties of `T`.
92
+ *
93
+ * @example
94
+ * type R1 = PickReadonly<{ a: 1, readonly c: 4 }>; // { readonly c: 4 }
95
+ */
96
+ export type PickReadonly<T> = {
97
+ [P in keyof T as IsReadonlyKey<T, P> extends true ? P : never]: T[P];
98
+ };
99
+ /**
100
+ * Recursively pick readonly properties of `T`.
101
+ *
102
+ * @example
103
+ * type Input = { readonly a: 1; b: { readonly c: 4, d: 5 }, e: 6 };
104
+ *
105
+ * type R1 = PickReadonly<Input>; // { readonly a: 1 }
106
+ * type R2 = PickReadonlyDeep<Input>; // { readonly a: 1, b: { readonly c: 4 } }
107
+ */
108
+ export type PickReadonlyDeep<T> = {
109
+ [P in keyof T as IsReadonlyKey<T, P> | HasType<T[P], Dict> extends false ? never : P]: T[P] extends infer TP ? (TP extends Dict ? PickReadonlyDeep<TP> : TP) : never;
110
+ } & {};
111
+ /**
112
+ * Pick mutable properties of `T`.
113
+ *
114
+ * @example
115
+ * type R1 = PickMutable<{ a: 1, readonly c: 4 }>; // { a: 1 }
116
+ */
117
+ export type PickMutable<T> = {
118
+ [P in keyof T as IsReadonlyKey<T, P> extends true ? never : P]: T[P];
119
+ };
120
+ /**
121
+ * Recursively pick mutable properties of `T`.
122
+ *
123
+ * @example
124
+ * type Input = { readonly a: 1; b: { readonly c: 4, d: 5 }, e: 6 };
125
+ *
126
+ * type R1 = PickMutable<Input>; // { b: { readonly c: 4, d: 5 }, e: 6 }
127
+ * type R2 = PickMutableDeep<Input>; // { b: { d: 5 }, e: 6 }
128
+ */
129
+ export type PickMutableDeep<T> = {
130
+ [P in keyof T as Not<IsReadonlyKey<T, P>> | HasType<T[P], Dict> extends false ? never : P]: T[P] extends infer TP ? (TP extends Dict ? PickMutableDeep<TP> : TP) : never;
131
+ } & {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inzero/ts-ut",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "TypeScript Utility Types",
5
5
  "keywords": [
6
6
  "typescript",