@ktarmyshov/typesafe-utilities 0.2.2
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/LICENSE +7 -0
- package/README.md +256 -0
- package/dist/filter.d.ts +15 -0
- package/dist/filter.js +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/partial.d.ts +5 -0
- package/dist/partial.js +1 -0
- package/dist/paths.d.ts +8 -0
- package/dist/paths.js +1 -0
- package/dist/record.d.ts +1 -0
- package/dist/record.js +1 -0
- package/package.json +63 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2025 [Contributors](https://github.com/kt-public/typesafe-utilities/graphs/contributors)
|
|
2
|
+
|
|
3
|
+
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:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
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
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
[](https://github.com/kt-public/typesafe-utilities/actions/workflows/ci.yml)
|
|
2
|
+
[](https://sonarcloud.io/summary/new_code?id=kt-public_typesafe-utilities)
|
|
3
|
+
[](https://sonarcloud.io/summary/new_code?id=kt-public_typesafe-utilities)
|
|
4
|
+
[](https://sonarcloud.io/summary/new_code?id=kt-public_typesafe-utilities)
|
|
5
|
+
[](https://sonarcloud.io/summary/new_code?id=kt-public_typesafe-utilities)
|
|
6
|
+
|
|
7
|
+
# typesafe-utilities
|
|
8
|
+
|
|
9
|
+
Some very specific type-safe utilities that are not available in [type-fest (very extensive)](https://www.npmjs.com/package/type-fest).
|
|
10
|
+
|
|
11
|
+
# Usage
|
|
12
|
+
|
|
13
|
+
## Property paths
|
|
14
|
+
|
|
15
|
+
Difference to `type-fest`:
|
|
16
|
+
|
|
17
|
+
- Array property path can be specified like `prop1[].prop2` (without index)
|
|
18
|
+
- Does not support tuples
|
|
19
|
+
- Supports only `Record<string, unknown>`
|
|
20
|
+
- Somewhat better auto-complete for nested arrays of objects
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
24
|
+
import { PropertyPaths } from '../src/paths';
|
|
25
|
+
|
|
26
|
+
type TestType = {
|
|
27
|
+
prop1: string;
|
|
28
|
+
prop2: number;
|
|
29
|
+
propArr: string[];
|
|
30
|
+
propArr2: {
|
|
31
|
+
propArr3: string[];
|
|
32
|
+
};
|
|
33
|
+
propArr4: {
|
|
34
|
+
propArr5: string[];
|
|
35
|
+
}[];
|
|
36
|
+
prop3: {
|
|
37
|
+
prop4: string;
|
|
38
|
+
prop5: {
|
|
39
|
+
prop6: string[];
|
|
40
|
+
};
|
|
41
|
+
}[];
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
type TestTypePropertyPaths = PropertyPaths<TestType>;
|
|
45
|
+
|
|
46
|
+
const test1: TestTypePropertyPaths = 'prop1';
|
|
47
|
+
const test2: TestTypePropertyPaths = 'prop2';
|
|
48
|
+
const test3: TestTypePropertyPaths = 'propArr';
|
|
49
|
+
const test4: TestTypePropertyPaths = 'propArr[]';
|
|
50
|
+
const test5: TestTypePropertyPaths = 'propArr[0]';
|
|
51
|
+
const test6: TestTypePropertyPaths = 'propArr2.propArr3';
|
|
52
|
+
const test7: TestTypePropertyPaths = 'propArr2.propArr3[]';
|
|
53
|
+
const test8: TestTypePropertyPaths = 'propArr2.propArr3[0]';
|
|
54
|
+
const test9: TestTypePropertyPaths = 'propArr4';
|
|
55
|
+
const test10: TestTypePropertyPaths = 'propArr4[]';
|
|
56
|
+
const test11: TestTypePropertyPaths = 'propArr4[0]';
|
|
57
|
+
const test12: TestTypePropertyPaths = 'propArr4[].propArr5';
|
|
58
|
+
const test13: TestTypePropertyPaths = 'propArr4[].propArr5[]';
|
|
59
|
+
const test14: TestTypePropertyPaths = 'propArr4[].propArr5[0]';
|
|
60
|
+
const test15: TestTypePropertyPaths = 'propArr4[0].propArr5';
|
|
61
|
+
const test16: TestTypePropertyPaths = 'propArr4[0].propArr5[]';
|
|
62
|
+
const test17: TestTypePropertyPaths = 'propArr4[0].propArr5[0]';
|
|
63
|
+
const test18: TestTypePropertyPaths = 'prop3';
|
|
64
|
+
const test19: TestTypePropertyPaths = 'prop3[]';
|
|
65
|
+
const test21: TestTypePropertyPaths = 'prop3[].prop4';
|
|
66
|
+
const test22: TestTypePropertyPaths = 'prop3[].prop5';
|
|
67
|
+
const test23: TestTypePropertyPaths = 'prop3[].prop5.prop6';
|
|
68
|
+
const test24: TestTypePropertyPaths = 'prop3[].prop5.prop6[]';
|
|
69
|
+
const test25: TestTypePropertyPaths = 'prop3[0]';
|
|
70
|
+
const test26: TestTypePropertyPaths = 'prop3[0].prop4';
|
|
71
|
+
const test27: TestTypePropertyPaths = 'prop3[0].prop5';
|
|
72
|
+
const test28: TestTypePropertyPaths = 'prop3[0].prop5.prop6';
|
|
73
|
+
const test29: TestTypePropertyPaths = 'prop3[0].prop5.prop6[]';
|
|
74
|
+
const test30: TestTypePropertyPaths = 'prop3[0].prop5.prop6[0]';
|
|
75
|
+
|
|
76
|
+
// @ts-expect-error: Invalid property path
|
|
77
|
+
const test_e1: TestTypePropertyPaths = ''; // should be error
|
|
78
|
+
// @ts-expect-error: Invalid property path
|
|
79
|
+
const test_e2: TestTypePropertyPaths = 'prop1.prop2'; // should be error
|
|
80
|
+
// @ts-expect-error: Invalid property path
|
|
81
|
+
const test_e3: TestTypePropertyPaths = 'prop1[]'; // should be error
|
|
82
|
+
// @ts-expect-error: Invalid property path
|
|
83
|
+
const test_e4: TestTypePropertyPaths = 'prop1[0]'; // should be error
|
|
84
|
+
// @ts-expect-error: Invalid property path
|
|
85
|
+
const test_e5: TestTypePropertyPaths = 'propArr.'; // should be error
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## DeepPartial
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
92
|
+
import { type DeepPartial } from '../src/partial';
|
|
93
|
+
|
|
94
|
+
type TestType = {
|
|
95
|
+
prop1: string;
|
|
96
|
+
prop2: number;
|
|
97
|
+
propArr: string[];
|
|
98
|
+
propArr2: {
|
|
99
|
+
propArr3: string[];
|
|
100
|
+
};
|
|
101
|
+
propArr4: {
|
|
102
|
+
propArr5: string[];
|
|
103
|
+
}[];
|
|
104
|
+
prop3: {
|
|
105
|
+
prop4: string;
|
|
106
|
+
prop5: {
|
|
107
|
+
prop6: string[];
|
|
108
|
+
};
|
|
109
|
+
}[];
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
type TestTypeDeepPartial = DeepPartial<TestType>;
|
|
113
|
+
const test1: TestTypeDeepPartial = {
|
|
114
|
+
prop1: 'test',
|
|
115
|
+
prop2: 123
|
|
116
|
+
};
|
|
117
|
+
const test2: TestTypeDeepPartial = {
|
|
118
|
+
propArr: ['test1', 'test2'],
|
|
119
|
+
propArr2: {}
|
|
120
|
+
};
|
|
121
|
+
const test3: TestTypeDeepPartial = {
|
|
122
|
+
propArr2: {
|
|
123
|
+
propArr3: ['test1', 'test2']
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
const test4: TestTypeDeepPartial = {
|
|
127
|
+
propArr4: [{}]
|
|
128
|
+
};
|
|
129
|
+
const test5: TestTypeDeepPartial = {
|
|
130
|
+
propArr4: [
|
|
131
|
+
{
|
|
132
|
+
propArr5: ['test1', 'test2']
|
|
133
|
+
}
|
|
134
|
+
]
|
|
135
|
+
};
|
|
136
|
+
const test6: TestTypeDeepPartial = {
|
|
137
|
+
prop3: [{}]
|
|
138
|
+
};
|
|
139
|
+
const test7: TestTypeDeepPartial = {
|
|
140
|
+
prop3: [
|
|
141
|
+
{
|
|
142
|
+
prop4: 'test',
|
|
143
|
+
prop5: {}
|
|
144
|
+
}
|
|
145
|
+
]
|
|
146
|
+
};
|
|
147
|
+
const test8: TestTypeDeepPartial = {
|
|
148
|
+
prop3: [
|
|
149
|
+
{
|
|
150
|
+
prop4: 'test',
|
|
151
|
+
prop5: {
|
|
152
|
+
prop6: ['test1', 'test2']
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
]
|
|
156
|
+
};
|
|
157
|
+
const test9: TestTypeDeepPartial = {
|
|
158
|
+
prop3: [
|
|
159
|
+
{
|
|
160
|
+
prop4: 'test',
|
|
161
|
+
prop5: {
|
|
162
|
+
prop6: ['test1', 'test2']
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
{}
|
|
166
|
+
]
|
|
167
|
+
};
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Filter & DeepFilter
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
174
|
+
import {
|
|
175
|
+
DeepFilter,
|
|
176
|
+
type FilterInclude,
|
|
177
|
+
type FilterIncludeKeys,
|
|
178
|
+
type NonNeverKeys,
|
|
179
|
+
type OmitNever
|
|
180
|
+
} from '../src/filter';
|
|
181
|
+
import { type Equal, type Expect } from './helpers';
|
|
182
|
+
|
|
183
|
+
type TestType = {
|
|
184
|
+
a: string;
|
|
185
|
+
b: number;
|
|
186
|
+
c: boolean;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
type asserts_FilterKeys = [
|
|
190
|
+
Expect<Equal<FilterIncludeKeys<TestType, string | number>, 'a' | 'b'>>,
|
|
191
|
+
Expect<Equal<FilterIncludeKeys<TestType, string>, 'a'>>,
|
|
192
|
+
Expect<Equal<FilterIncludeKeys<TestType, number>, 'b'>>,
|
|
193
|
+
Expect<Equal<FilterIncludeKeys<TestType, boolean>, 'c'>>,
|
|
194
|
+
Expect<Equal<FilterIncludeKeys<TestType, unknown>, 'a' | 'b' | 'c'>>
|
|
195
|
+
];
|
|
196
|
+
|
|
197
|
+
type asserts_Filter = [
|
|
198
|
+
Expect<Equal<FilterInclude<TestType, string | number>, { a: string; b: number }>>,
|
|
199
|
+
Expect<Equal<FilterInclude<TestType, string>, { a: string }>>,
|
|
200
|
+
Expect<Equal<FilterInclude<TestType, number>, { b: number }>>,
|
|
201
|
+
Expect<Equal<FilterInclude<TestType, boolean>, { c: boolean }>>,
|
|
202
|
+
Expect<Equal<FilterInclude<TestType, unknown>, TestType>>
|
|
203
|
+
];
|
|
204
|
+
|
|
205
|
+
type TestTypeNever = {
|
|
206
|
+
a: string;
|
|
207
|
+
b: number;
|
|
208
|
+
c: boolean;
|
|
209
|
+
d: never;
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
type asserts_NeverKeys = [
|
|
213
|
+
Expect<Equal<NonNeverKeys<TestType>, 'a' | 'b' | 'c'>>,
|
|
214
|
+
Expect<Equal<NonNeverKeys<TestTypeNever>, 'a' | 'b' | 'c'>>
|
|
215
|
+
];
|
|
216
|
+
|
|
217
|
+
type asserts_OmitNever = [
|
|
218
|
+
Expect<Equal<OmitNever<TestType>, TestType>>,
|
|
219
|
+
Expect<Equal<OmitNever<TestTypeNever>, { a: string; b: number; c: boolean }>>
|
|
220
|
+
];
|
|
221
|
+
|
|
222
|
+
type TestTypeDeep = {
|
|
223
|
+
a: string;
|
|
224
|
+
b: number;
|
|
225
|
+
c: boolean;
|
|
226
|
+
d: {
|
|
227
|
+
e: string;
|
|
228
|
+
f: number;
|
|
229
|
+
g: boolean;
|
|
230
|
+
};
|
|
231
|
+
h: {
|
|
232
|
+
i: string;
|
|
233
|
+
j: number;
|
|
234
|
+
k: boolean;
|
|
235
|
+
}[];
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
type asserts_DeepFilter = [
|
|
239
|
+
Expect<
|
|
240
|
+
Equal<DeepFilter<TestTypeDeep, string>, { a: string; d: { e: string }; h: { i: string }[] }>
|
|
241
|
+
>,
|
|
242
|
+
Expect<
|
|
243
|
+
Equal<DeepFilter<TestTypeDeep, number>, { b: number; d: { f: number }; h: { j: number }[] }>
|
|
244
|
+
>,
|
|
245
|
+
Expect<
|
|
246
|
+
Equal<DeepFilter<TestTypeDeep, boolean>, { c: boolean; d: { g: boolean }; h: { k: boolean }[] }>
|
|
247
|
+
>,
|
|
248
|
+
Expect<
|
|
249
|
+
Equal<
|
|
250
|
+
DeepFilter<TestTypeDeep, string | number>,
|
|
251
|
+
{ a: string; b: number; d: { e: string; f: number }; h: { i: string; j: number }[] }
|
|
252
|
+
>
|
|
253
|
+
>,
|
|
254
|
+
Expect<Equal<DeepFilter<TestTypeDeep, unknown>, TestTypeDeep>>
|
|
255
|
+
];
|
|
256
|
+
```
|
package/dist/filter.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type FilterIncludeKeys<T, IncludeType> = {
|
|
2
|
+
[K in keyof T]: T[K] extends IncludeType ? K : never;
|
|
3
|
+
}[keyof T];
|
|
4
|
+
export type FilterInclude<T, IncludeType> = {
|
|
5
|
+
[K in FilterIncludeKeys<T, IncludeType>]: T[K];
|
|
6
|
+
};
|
|
7
|
+
export type NonNeverKeys<T> = {
|
|
8
|
+
[K in keyof T]-?: [T[K]] extends [never] ? never : K;
|
|
9
|
+
}[keyof T];
|
|
10
|
+
export type OmitNever<T> = {
|
|
11
|
+
[K in Extract<keyof T, NonNeverKeys<T>>]: T[K];
|
|
12
|
+
};
|
|
13
|
+
export type DeepFilter<T, IncludeType> = OmitNever<{
|
|
14
|
+
[K in keyof T]: T[K] extends IncludeType ? T[K] : T[K] extends Record<string, unknown> ? DeepFilter<T[K], IncludeType> : T[K] extends (infer U)[] ? DeepFilter<U, IncludeType>[] : never;
|
|
15
|
+
}>;
|
package/dist/filter.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/partial.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/paths.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
type ArraySuffix = `[${number | ''}]`;
|
|
2
|
+
type FieldSuffix<T> = T extends unknown[] ? `${ArraySuffix}` : '';
|
|
3
|
+
type PropertyPathsImplRecord<T extends Record<string, unknown>, Prefix> = {
|
|
4
|
+
[K in keyof T]: `${string & Prefix}${string & K}` | `${string & Prefix}${string & K}${FieldSuffix<T[K]>}` | PropertyPathsImpl<T[K], `${string & Prefix}${string & K}${FieldSuffix<T[K]>}.`>;
|
|
5
|
+
}[keyof T];
|
|
6
|
+
type PropertyPathsImpl<T, Prefix> = T extends Record<string, unknown> ? PropertyPathsImplRecord<T, Prefix> : T extends (infer U)[] ? PropertyPathsImpl<U, Prefix> : never;
|
|
7
|
+
export type PropertyPaths<T extends Record<string, unknown>> = PropertyPathsImpl<T, ''>;
|
|
8
|
+
export {};
|
package/dist/paths.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/record.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type UnknownStringRecord = Record<string, unknown>;
|
package/dist/record.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ktarmyshov/typesafe-utilities",
|
|
3
|
+
"description": "Type-safe utilities for working in TypeScript. For example, a type-safe property path type, deep partial type, etc.",
|
|
4
|
+
"version": "0.2.2",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Konstantin Tarmyshov",
|
|
8
|
+
"email": "k.tarmyshov@googlemail.com"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"typescript",
|
|
12
|
+
"type-safe",
|
|
13
|
+
"typesafe",
|
|
14
|
+
"property",
|
|
15
|
+
"path",
|
|
16
|
+
"string",
|
|
17
|
+
"property-path",
|
|
18
|
+
"deep partial"
|
|
19
|
+
],
|
|
20
|
+
"homepage": "https://github.com/kt-public/typesafe-utilities#readme",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/kt-public/typesafe-utilities.git"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "rimraf ./dist && tsc && npm run prepack",
|
|
27
|
+
"build:release": "rimraf ./dist && tsc --project tsconfig-release.json && npm run prepack",
|
|
28
|
+
"prepare": "echo This script is called after 'npm ci' or 'npm install'",
|
|
29
|
+
"prepack": "publint",
|
|
30
|
+
"format": "prettier --write .",
|
|
31
|
+
"lint": "prettier --check . && eslint .",
|
|
32
|
+
"check": "tsc --skipLibCheck",
|
|
33
|
+
"check:test": "tsc --project tsconfig-test.json --skipLibCheck",
|
|
34
|
+
"test": "npm run check:test",
|
|
35
|
+
"coverage": "echo 'No tests for now' && exit 0",
|
|
36
|
+
"changeset:version": "changeset version && git add --all",
|
|
37
|
+
"changeset:publish": "changeset publish"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist",
|
|
41
|
+
"!dist/**/*.test.*",
|
|
42
|
+
"!dist/**/*.spec.*",
|
|
43
|
+
"!tests"
|
|
44
|
+
],
|
|
45
|
+
"type": "module",
|
|
46
|
+
"types": "dist/index.d.ts",
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@changesets/cli": "^2.28.1",
|
|
49
|
+
"@eslint/compat": "^1.2.7",
|
|
50
|
+
"@types/node": "^22.0.0",
|
|
51
|
+
"eslint": "^9.22.0",
|
|
52
|
+
"eslint-config-prettier": "^10.1.1",
|
|
53
|
+
"prettier": "^3.5.3",
|
|
54
|
+
"publint": "^0.3.9",
|
|
55
|
+
"rimraf": "^6.0.1",
|
|
56
|
+
"type-fest": "^4.40.0",
|
|
57
|
+
"typescript": "^5.8.2",
|
|
58
|
+
"typescript-eslint": "^8.26.1"
|
|
59
|
+
},
|
|
60
|
+
"engines": {
|
|
61
|
+
"node": ">=20.0.0"
|
|
62
|
+
}
|
|
63
|
+
}
|