@ashetm/types 1.0.0 → 1.2.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/README.md +112 -6
- package/dist/deep.d.ts +43 -0
- package/dist/index.d.ts +3 -5
- package/dist/index.js +3 -5
- package/dist/json.d.ts +23 -0
- package/dist/json.js +1 -0
- package/dist/legacy.d.ts +7 -0
- package/dist/legacy.js +9 -0
- package/dist/object.d.ts +101 -0
- package/dist/path.d.ts +49 -0
- package/dist/path.js +2 -0
- package/dist/string.d.ts +51 -0
- package/dist/string.js +3 -0
- package/dist/union.d.ts +9 -0
- package/dist/union.js +1 -0
- package/package.json +22 -2
package/README.md
CHANGED
|
@@ -1,9 +1,19 @@
|
|
|
1
1
|
# @ashetm/types
|
|
2
2
|
|
|
3
|
-
``@ashetm/types`` is a small, dependency-free library of TypeScript utility types: nullability, arrays, objects, deep partial / readonly and branded types.
|
|
3
|
+
``@ashetm/types`` is a small, dependency-free library of TypeScript utility types: nullability, arrays, objects, deep partial / readonly, JSON, dot paths, string cases and branded types.
|
|
4
4
|
|
|
5
5
|
It is **types only**: it adds nothing to your bundle. It works with any project (Angular, React, Node, ...) on **TypeScript 3.7 or later**.
|
|
6
6
|
|
|
7
|
+
## TypeScript support
|
|
8
|
+
|
|
9
|
+
| TypeScript | Angular | Types |
|
|
10
|
+
|------------|-------------|-------------------------------------------------------------------------------------------|
|
|
11
|
+
| 3.7 – 4.0 | 9 – 11.0 | all except [Paths](#paths) and [String cases](#string-cases) |
|
|
12
|
+
| 4.1 – 4.4 | 11.1 – 13.0 | all; string cases up to about 40 characters, `Paths` of self-referencing types up to 9 segments |
|
|
13
|
+
| 4.5+ | 13.1+ | all |
|
|
14
|
+
|
|
15
|
+
Your TypeScript version picks the right types by itself (`typesVersions`): nothing to configure.
|
|
16
|
+
|
|
7
17
|
## Install
|
|
8
18
|
|
|
9
19
|
You can install it with npm:
|
|
@@ -15,7 +25,7 @@ npm install @ashetm/types
|
|
|
15
25
|
## API
|
|
16
26
|
|
|
17
27
|
```ts
|
|
18
|
-
import type {
|
|
28
|
+
import type { DeepPartial, NonEmptyArray, Nullable, Prettify /* , ... */ } from '@ashetm/types';
|
|
19
29
|
```
|
|
20
30
|
|
|
21
31
|
On TypeScript 3.7, use `import { ... }` (`import type` needs TypeScript 3.8).
|
|
@@ -48,6 +58,12 @@ type Role = ArrayItem<readonly ['admin', 'user']>; // 'admin' | 'user'
|
|
|
48
58
|
| `ValueOf<T>` | the union of the value types of `T`. |
|
|
49
59
|
| `KeysOfType<T, V>` | the keys of `T` whose value is assignable to `V` (`undefined` is ignored). |
|
|
50
60
|
| `Mutable<T>` | `T` without `readonly` on its properties (one level, arrays and tuples too). |
|
|
61
|
+
| `Prettify<T>` | `T` with its intersections flattened, so hovers are readable (`readonly` and `?` kept). |
|
|
62
|
+
| `PartialBy<T, K>` | `T` with only the keys `K` optional. |
|
|
63
|
+
| `RequiredBy<T, K>` | `T` with only the keys `K` required. |
|
|
64
|
+
| `RequireAtLeastOne<T, K>` | at least one of the keys `K` (all keys by default) must be set. |
|
|
65
|
+
| `RequireExactlyOne<T, K>` | exactly one of the keys `K` (all keys by default) must be set, the others are not allowed. |
|
|
66
|
+
| `SplitKeys<T>` | a union with one object type per key of `T` (`readonly` and `?` kept). |
|
|
51
67
|
|
|
52
68
|
```ts
|
|
53
69
|
const Status = { Active: 'active', Archived: 'archived' } as const;
|
|
@@ -59,6 +75,29 @@ interface User {
|
|
|
59
75
|
nickname?: string;
|
|
60
76
|
}
|
|
61
77
|
type TextKeys = KeysOfType<User, string>; // 'name' | 'nickname'
|
|
78
|
+
type NewUser = PartialBy<User, 'id'>; // { name: string; nickname?: string; id?: number }
|
|
79
|
+
|
|
80
|
+
interface Contact {
|
|
81
|
+
name: string;
|
|
82
|
+
email?: string;
|
|
83
|
+
phone?: string;
|
|
84
|
+
}
|
|
85
|
+
type Reachable = RequireAtLeastOne<Contact, 'email' | 'phone'>;
|
|
86
|
+
const contact: Reachable = { name: 'A' }; // error: email or phone is needed
|
|
87
|
+
|
|
88
|
+
type Change = SplitKeys<{ name: string; age?: number }>; // { name: string } | { age?: number }
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
TypeScript can't split an intersection back into its members, so there is no `IntersectionToUnion`: `SplitKeys<A & B>` gives `A | B` only when `A` and `B` have one key each.
|
|
92
|
+
|
|
93
|
+
### Unions
|
|
94
|
+
|
|
95
|
+
| Type | Is |
|
|
96
|
+
|--------------------------|-------------------------------------------------|
|
|
97
|
+
| `UnionToIntersection<U>` | the intersection of all the members of `U`. |
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
type Both = UnionToIntersection<{ id: number } | { name: string }>; // { id: number } & { name: string }
|
|
62
101
|
```
|
|
63
102
|
|
|
64
103
|
### Deep types
|
|
@@ -67,11 +106,13 @@ type TextKeys = KeysOfType<User, string>; // 'name' | 'nickname'
|
|
|
67
106
|
|-------------------|--------------------------------------------------------|
|
|
68
107
|
| `DeepPartial<T>` | `T` with every property optional, at every level. |
|
|
69
108
|
| `DeepReadonly<T>` | `T` with every property `readonly`, at every level. |
|
|
109
|
+
| `DeepRequired<T>` | `T` with every property required, at every level. |
|
|
110
|
+
| `DeepMutable<T>` | `T` without `readonly`, at every level. |
|
|
70
111
|
|
|
71
|
-
|
|
112
|
+
All of them:
|
|
72
113
|
|
|
73
|
-
- go into arrays and tuples (tuples keep their length; `DeepReadonly`
|
|
74
|
-
- go into `Map` and `Set` values (`Map` keys are kept; `DeepReadonly`
|
|
114
|
+
- go into arrays and tuples (tuples keep their length; `DeepReadonly` / `DeepMutable` make them readonly / mutable; `DeepRequired` makes optional tuple items required),
|
|
115
|
+
- go into `Map` and `Set` values (`Map` keys are kept; `DeepReadonly` gives `ReadonlyMap` / `ReadonlySet`, `DeepMutable` gives `Map` / `Set`),
|
|
75
116
|
- keep primitives, functions, `Date`, `RegExp`, `Error`, `Promise`, `WeakMap` and `WeakSet` as they are.
|
|
76
117
|
|
|
77
118
|
```ts
|
|
@@ -87,6 +128,70 @@ settings.theme.color = 'blue'; // error
|
|
|
87
128
|
settings.tags.push('a'); // error
|
|
88
129
|
```
|
|
89
130
|
|
|
131
|
+
`DeepRequired` removes `?` with the `undefined` that came with it; an explicit `| undefined` is kept.
|
|
132
|
+
|
|
133
|
+
### JSON
|
|
134
|
+
|
|
135
|
+
| Type | Is |
|
|
136
|
+
|-----------------|------------------------------------------------------|
|
|
137
|
+
| `JsonPrimitive` | `string \| number \| boolean \| null` |
|
|
138
|
+
| `JsonValue` | any JSON value: what `JSON.parse` can return. |
|
|
139
|
+
| `JsonObject` | `{ [key: string]: JsonValue }` |
|
|
140
|
+
| `JsonArray` | `JsonValue[]` |
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
const payload: JsonValue = { id: 1, tags: ['a'], meta: null };
|
|
144
|
+
const invalid: JsonValue = { at: new Date() }; // error
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
An `interface` has no index signature, so a value typed with one is not assignable to `JsonObject`: use a `type` instead.
|
|
148
|
+
|
|
149
|
+
### Paths
|
|
150
|
+
|
|
151
|
+
Needs TypeScript 4.1+.
|
|
152
|
+
|
|
153
|
+
| Type | Is |
|
|
154
|
+
|-------------------|--------------------------------------------------------------------------------------------|
|
|
155
|
+
| `Paths<T>` | every dot path of `T` (string keys; arrays and tuples use `${number}`). |
|
|
156
|
+
| `PathValue<T, P>` | the type at the dot path `P`: with `undefined` when a step is optional, `never` when the path does not exist. |
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
interface Settings {
|
|
160
|
+
theme: { color: string };
|
|
161
|
+
owner?: { name: string };
|
|
162
|
+
tags: string[];
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
type Path = Paths<Settings>; // 'theme' | 'theme.color' | 'owner' | 'owner.name' | 'tags' | `tags.${number}`
|
|
166
|
+
type Owner = PathValue<Settings, 'owner.name'>; // string | undefined
|
|
167
|
+
|
|
168
|
+
declare const settings: Settings;
|
|
169
|
+
declare function get<T, P extends Paths<T>>(value: T, path: P): PathValue<T, P>;
|
|
170
|
+
get(settings, 'theme.color'); // string
|
|
171
|
+
get(settings, 'theme.size'); // error
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
Optional values are followed; `Date`, functions, `Map`, `Set` and other built-ins are not. Self-referencing types (`children: Tree[]`) stop at 9 segments.
|
|
175
|
+
|
|
176
|
+
### String cases
|
|
177
|
+
|
|
178
|
+
Needs TypeScript 4.1+.
|
|
179
|
+
|
|
180
|
+
| Type | Is |
|
|
181
|
+
|----------------|------------------------------------------------------------------------------------------|
|
|
182
|
+
| `CamelCase<S>` | `S` in camelCase: words split on `_`, `-` and spaces; all-uppercase words lowercased first. |
|
|
183
|
+
| `KebabCase<S>` | `S` in kebab-case: words split on `_`, `-`, spaces and lowercase → uppercase changes. |
|
|
184
|
+
| `SnakeCase<S>` | `S` in snake_case: same words as `KebabCase`. |
|
|
185
|
+
|
|
186
|
+
```ts
|
|
187
|
+
type A = CamelCase<'user_id'>; // 'userId'
|
|
188
|
+
type B = CamelCase<'USER_ID'>; // 'userId'
|
|
189
|
+
type C = KebabCase<'userID'>; // 'user-id'
|
|
190
|
+
type D = SnakeCase<'user-name id'>; // 'user_name_id'
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
`string` stays `string`, and a union is converted member by member.
|
|
194
|
+
|
|
90
195
|
### Branded types
|
|
91
196
|
|
|
92
197
|
`Brand<T, Name>` tags `T` with a name, so two types with the same shape can't be mixed up. A branded value is still a `T`; a plain `T` must be cast to get the brand.
|
|
@@ -113,7 +218,8 @@ npm run test:compat
|
|
|
113
218
|
```
|
|
114
219
|
|
|
115
220
|
- `npm test` type-checks the type tests (`src/*.spec-d.ts`): what must compile, and `// @ts-expect-error` for what must not. Nothing runs.
|
|
116
|
-
- `npm run test:compat` builds, then checks `
|
|
221
|
+
- `npm run test:compat` builds, then checks the package like a real install (`compat/`, linked as `@ashetm/types`) with TypeScript 3.7, 4.0, 4.1, 4.4, 5.0, 6 and the latest (downloaded with `npx`).
|
|
222
|
+
- `src/legacy.ts` holds the TypeScript 3.7 types: TypeScript < 4.1 reads only this file. Types that need TypeScript 4.1+ are exported from `src/index.ts` only.
|
|
117
223
|
|
|
118
224
|
## License
|
|
119
225
|
|
package/dist/deep.d.ts
CHANGED
|
@@ -43,4 +43,47 @@ export type DeepPartial<T> = T extends Map<infer K, infer V> ? Map<K, DeepPartia
|
|
|
43
43
|
export type DeepReadonly<T> = T extends ReadonlyMap<infer K, infer V> ? ReadonlyMap<K, DeepReadonly<V>> : T extends ReadonlySet<infer U> ? ReadonlySet<DeepReadonly<U>> : T extends Builtin ? T : {
|
|
44
44
|
readonly [K in keyof T]: DeepReadonly<T[K]>;
|
|
45
45
|
};
|
|
46
|
+
/**
|
|
47
|
+
* `T` with every property required, at every level: `?` is removed, with the `undefined` that came
|
|
48
|
+
* with it (an explicit `| undefined` is kept).
|
|
49
|
+
*
|
|
50
|
+
* - Arrays: items are deep required. Tuples: optional items become required.
|
|
51
|
+
* - `Map` and `Set` (readonly too): values are deep required, `Map` keys are kept.
|
|
52
|
+
* - Primitives, functions, `Date`, `RegExp`, `Error`, `Promise`, `WeakMap` and `WeakSet` are kept as they are.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* interface Options {
|
|
57
|
+
* retry?: { count?: number; delay?: number };
|
|
58
|
+
* }
|
|
59
|
+
*
|
|
60
|
+
* const defaults: DeepRequired<Options> = { retry: { count: 3, delay: 100 } };
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export type DeepRequired<T> = T extends Map<infer K, infer V> ? Map<K, DeepRequired<V>> : T extends ReadonlyMap<infer K, infer V> ? ReadonlyMap<K, DeepRequired<V>> : T extends Set<infer U> ? Set<DeepRequired<U>> : T extends ReadonlySet<infer U> ? ReadonlySet<DeepRequired<U>> : T extends Builtin ? T : T extends ReadonlyArray<unknown> ? number extends T['length'] ? {
|
|
64
|
+
[K in keyof T]: DeepRequired<T[K]>;
|
|
65
|
+
} : {
|
|
66
|
+
[K in keyof T]-?: DeepRequired<T[K]>;
|
|
67
|
+
} : {
|
|
68
|
+
[K in keyof T]-?: DeepRequired<T[K]>;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* `T` without `readonly`, at every level.
|
|
72
|
+
*
|
|
73
|
+
* - Readonly arrays and tuples become mutable, with deep mutable items.
|
|
74
|
+
* - `ReadonlyMap` and `ReadonlySet` become `Map` and `Set`, with deep mutable values (`Map` keys are kept).
|
|
75
|
+
* - Primitives, functions, `Date`, `RegExp`, `Error`, `Promise`, `WeakMap` and `WeakSet` are kept as they are.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* const defaults = { theme: { color: 'red' }, tags: ['a'] } as const;
|
|
80
|
+
*
|
|
81
|
+
* const settings: DeepMutable<typeof defaults> = { theme: { color: 'red' }, tags: ['a'] };
|
|
82
|
+
* settings.theme.color = 'red'; // allowed
|
|
83
|
+
* settings.tags.push('a'); // allowed
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export type DeepMutable<T> = T extends ReadonlyMap<infer K, infer V> ? Map<K, DeepMutable<V>> : T extends ReadonlySet<infer U> ? Set<DeepMutable<U>> : T extends Builtin ? T : {
|
|
87
|
+
-readonly [K in keyof T]: DeepMutable<T[K]>;
|
|
88
|
+
};
|
|
46
89
|
export {};
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/json.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** A JSON primitive: `string`, `number`, `boolean` or `null`. */
|
|
2
|
+
export type JsonPrimitive = string | number | boolean | null;
|
|
3
|
+
/**
|
|
4
|
+
* Any JSON value: what `JSON.parse` can return.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* const payload: JsonValue = { id: 1, tags: ['a'], meta: null };
|
|
9
|
+
* const invalid: JsonValue = { at: new Date() }; // error
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export type JsonValue = JsonPrimitive | JsonObject | JsonArray;
|
|
13
|
+
/**
|
|
14
|
+
* A JSON object.
|
|
15
|
+
*
|
|
16
|
+
* An `interface` has no index signature, so a value typed with one is not assignable to `JsonObject`:
|
|
17
|
+
* use a `type` instead. Optional properties are not allowed (`undefined` is not JSON).
|
|
18
|
+
*/
|
|
19
|
+
export type JsonObject = {
|
|
20
|
+
[key: string]: JsonValue;
|
|
21
|
+
};
|
|
22
|
+
/** A JSON array. */
|
|
23
|
+
export type JsonArray = JsonValue[];
|
package/dist/json.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/legacy.d.ts
ADDED
package/dist/legacy.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// The types that work on TypeScript 3.7+. TypeScript < 4.1 reads only this file (`typesVersions` in `package.json`):
|
|
2
|
+
// keep template literal types and other TypeScript 4.1+ syntax out of it.
|
|
3
|
+
export * from './array.js';
|
|
4
|
+
export * from './brand.js';
|
|
5
|
+
export * from './deep.js';
|
|
6
|
+
export * from './json.js';
|
|
7
|
+
export * from './nullable.js';
|
|
8
|
+
export * from './object.js';
|
|
9
|
+
export * from './union.js';
|
package/dist/object.d.ts
CHANGED
|
@@ -44,3 +44,104 @@ export type KeysOfType<T, V> = {
|
|
|
44
44
|
export type Mutable<T> = {
|
|
45
45
|
-readonly [K in keyof T]: T[K];
|
|
46
46
|
};
|
|
47
|
+
/**
|
|
48
|
+
* `T` with its intersections flattened into one object type, so hovers are readable.
|
|
49
|
+
* `readonly` and `?` are kept.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* type User = Prettify<{ id: number } & { name?: string }>; // { id: number; name?: string }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export type Prettify<T> = {
|
|
57
|
+
[K in keyof T]: T[K];
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* `T` with only the keys `K` optional.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* interface User {
|
|
65
|
+
* id: number;
|
|
66
|
+
* name: string;
|
|
67
|
+
* email: string;
|
|
68
|
+
* }
|
|
69
|
+
*
|
|
70
|
+
* type NewUser = PartialBy<User, 'id'>; // { name: string; email: string; id?: number }
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export type PartialBy<T, K extends keyof T> = Prettify<Omit<T, K> & Partial<Pick<T, K>>>;
|
|
74
|
+
/**
|
|
75
|
+
* `T` with only the keys `K` required.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* interface Options {
|
|
80
|
+
* url?: string;
|
|
81
|
+
* timeout?: number;
|
|
82
|
+
* }
|
|
83
|
+
*
|
|
84
|
+
* type UrlOptions = RequiredBy<Options, 'url'>; // { timeout?: number; url: string }
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export type RequiredBy<T, K extends keyof T> = Prettify<Omit<T, K> & Required<Pick<T, K>>>;
|
|
88
|
+
/**
|
|
89
|
+
* `T` where at least one of the keys `K` (all keys by default) must be set.
|
|
90
|
+
* The other keys of `K` stay optional; keys outside `K` are kept as they are.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* interface Contact {
|
|
95
|
+
* name: string;
|
|
96
|
+
* email?: string;
|
|
97
|
+
* phone?: string;
|
|
98
|
+
* }
|
|
99
|
+
*
|
|
100
|
+
* type ReachableContact = RequireAtLeastOne<Contact, 'email' | 'phone'>;
|
|
101
|
+
*
|
|
102
|
+
* const a: ReachableContact = { name: 'A', email: 'a@b.c' };
|
|
103
|
+
* const b: ReachableContact = { name: 'B', email: 'b@c.d', phone: '01' };
|
|
104
|
+
* const c: ReachableContact = { name: 'C' }; // error
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export type RequireAtLeastOne<T, K extends keyof T = keyof T> = {
|
|
108
|
+
[P in K]-?: Prettify<Omit<T, K> & Required<Pick<T, P>> & Partial<Pick<T, Exclude<K, P>>>>;
|
|
109
|
+
}[K];
|
|
110
|
+
/**
|
|
111
|
+
* `T` where exactly one of the keys `K` (all keys by default) must be set; the other keys of `K`
|
|
112
|
+
* are not allowed. Keys outside `K` are kept as they are.
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```ts
|
|
116
|
+
* interface Payment {
|
|
117
|
+
* amount: number;
|
|
118
|
+
* card?: string;
|
|
119
|
+
* iban?: string;
|
|
120
|
+
* }
|
|
121
|
+
*
|
|
122
|
+
* type SinglePayment = RequireExactlyOne<Payment, 'card' | 'iban'>;
|
|
123
|
+
*
|
|
124
|
+
* const a: SinglePayment = { amount: 1, card: '4242' };
|
|
125
|
+
* const b: SinglePayment = { amount: 1, card: '4242', iban: 'FR76' }; // error
|
|
126
|
+
* const c: SinglePayment = { amount: 1 }; // error
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
export type RequireExactlyOne<T, K extends keyof T = keyof T> = {
|
|
130
|
+
[P in K]-?: Prettify<Omit<T, K> & Required<Pick<T, P>> & {
|
|
131
|
+
[Q in Exclude<K, P>]?: never;
|
|
132
|
+
}>;
|
|
133
|
+
}[K];
|
|
134
|
+
/**
|
|
135
|
+
* A union with one object type per key of `T`. `readonly` and `?` are kept.
|
|
136
|
+
*
|
|
137
|
+
* TypeScript can't split an intersection back into its members, so
|
|
138
|
+
* `SplitKeys<A & B>` gives `A | B` only when `A` and `B` have one key each.
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```ts
|
|
142
|
+
* type Change = SplitKeys<{ name: string; age?: number }>; // { name: string } | { age?: number }
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
export type SplitKeys<T> = {
|
|
146
|
+
[K in keyof T]-?: Prettify<Pick<T, K>>;
|
|
147
|
+
}[keyof T];
|
package/dist/path.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
type Leaf = string | number | boolean | bigint | symbol | null | undefined | Function | Date | RegExp | Error | Promise<unknown> | WeakMap<object, unknown> | WeakSet<object> | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown>;
|
|
2
|
+
type Depth = [never, 0, 1, 2, 3, 4, 5, 6, 7];
|
|
3
|
+
type PathsAt<T, D extends number> = [D] extends [never] ? never : T extends Leaf ? never : T extends ReadonlyArray<infer U> ? `${number}` | `${number}.${PathsAt<NonNullable<U>, Depth[D]>}` : {
|
|
4
|
+
[K in keyof T & string]-?: K | `${K}.${PathsAt<NonNullable<T[K]>, Depth[D]>}`;
|
|
5
|
+
}[keyof T & string];
|
|
6
|
+
/**
|
|
7
|
+
* Every dot path of `T`: its keys, then the keys of its values, and so on.
|
|
8
|
+
*
|
|
9
|
+
* - String keys only. Arrays and tuples use `${number}`.
|
|
10
|
+
* - Optional values are followed. `Date`, functions, `Map`, `Set` and other built-ins are not.
|
|
11
|
+
* - At most 9 segments (`a.0.b` has 3), only reached by self-referencing types.
|
|
12
|
+
*
|
|
13
|
+
* Needs TypeScript 4.1+.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* interface Settings {
|
|
18
|
+
* theme: { color: string };
|
|
19
|
+
* tags: string[];
|
|
20
|
+
* }
|
|
21
|
+
*
|
|
22
|
+
* type Path = Paths<Settings>; // 'theme' | 'theme.color' | 'tags' | `tags.${number}`
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export type Paths<T> = PathsAt<T, 8>;
|
|
26
|
+
type Get<T, K extends string> = T extends null | undefined ? undefined : K extends keyof T ? T[K] : T extends ReadonlyArray<infer U> ? K extends `${number}` ? U : never : never;
|
|
27
|
+
/**
|
|
28
|
+
* The type at the dot path `P` of `T`. It includes `undefined` when a step of the path is optional,
|
|
29
|
+
* and is `never` when the path does not exist.
|
|
30
|
+
*
|
|
31
|
+
* Needs TypeScript 4.1+.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* interface Settings {
|
|
36
|
+
* theme: { color: string };
|
|
37
|
+
* owner?: { name: string };
|
|
38
|
+
* tags: string[];
|
|
39
|
+
* }
|
|
40
|
+
*
|
|
41
|
+
* type Color = PathValue<Settings, 'theme.color'>; // string
|
|
42
|
+
* type Owner = PathValue<Settings, 'owner.name'>; // string | undefined
|
|
43
|
+
* type Tag = PathValue<Settings, 'tags.0'>; // string
|
|
44
|
+
*
|
|
45
|
+
* function get<T, P extends Paths<T>>(value: T, path: P): PathValue<T, P> { ... }
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export type PathValue<T, P extends string> = P extends `${infer Head}.${infer Rest}` ? PathValue<Get<T, Head>, Rest> : Get<T, P>;
|
|
49
|
+
export {};
|
package/dist/path.js
ADDED
package/dist/string.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
type Delimiter = '_' | '-' | ' ';
|
|
2
|
+
type State = 'start' | 'lower' | 'upper' | 'delimiter';
|
|
3
|
+
type IsLower<C extends string> = C extends Lowercase<C> ? true : false;
|
|
4
|
+
type Piece<C extends string, Before extends State, Separator extends string> = C extends Delimiter ? '' : IsLower<C> extends true ? `${Before extends 'delimiter' ? Separator : ''}${C}` : `${Before extends 'delimiter' | 'lower' ? Separator : ''}${Lowercase<C>}`;
|
|
5
|
+
type NextState<C extends string, Before extends State> = C extends Delimiter ? Before extends 'start' ? 'start' : 'delimiter' : IsLower<C> extends true ? 'lower' : 'upper';
|
|
6
|
+
type Separate<S extends string, Separator extends string, Result extends string = '', Before extends State = 'start'> = S extends `${infer C}${infer Rest}` ? Separate<Rest, Separator, `${Result}${Piece<C, Before, Separator>}`, NextState<C, Before>> : Result;
|
|
7
|
+
/**
|
|
8
|
+
* `S` in kebab-case. Words are split on `_`, `-`, spaces and lowercase → uppercase changes.
|
|
9
|
+
* `string` stays `string`.
|
|
10
|
+
*
|
|
11
|
+
* Needs TypeScript 4.1+ (about 40 characters on TypeScript 4.1–4.4).
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* type A = KebabCase<'userId'>; // 'user-id'
|
|
16
|
+
* type B = KebabCase<'USER_ID'>; // 'user-id'
|
|
17
|
+
* type C = KebabCase<'UserID'>; // 'user-id'
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export type KebabCase<S extends string> = string extends S ? string : Separate<S, '-'>;
|
|
21
|
+
/**
|
|
22
|
+
* `S` in snake_case. Words are split on `_`, `-`, spaces and lowercase → uppercase changes.
|
|
23
|
+
* `string` stays `string`.
|
|
24
|
+
*
|
|
25
|
+
* Needs TypeScript 4.1+ (about 40 characters on TypeScript 4.1–4.4).
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* type A = SnakeCase<'userId'>; // 'user_id'
|
|
30
|
+
* type B = SnakeCase<'user-name id'>; // 'user_name_id'
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export type SnakeCase<S extends string> = string extends S ? string : Separate<S, '_'>;
|
|
34
|
+
type ToUnderscores<S extends string, Result extends string = ''> = S extends `${infer C}${infer Rest}` ? ToUnderscores<Rest, `${Result}${C extends Delimiter ? '_' : C}`> : Result;
|
|
35
|
+
type Word<W extends string> = W extends Uppercase<W> ? Capitalize<Lowercase<W>> : Capitalize<W>;
|
|
36
|
+
type JoinWords<S extends string, Result extends string = ''> = S extends `${infer W}_${infer Rest}` ? JoinWords<Rest, `${Result}${Word<W>}`> : `${Result}${Word<S>}`;
|
|
37
|
+
/**
|
|
38
|
+
* `S` in camelCase. Words are split on `_`, `-` and spaces; all-uppercase words are lowercased first.
|
|
39
|
+
* `string` stays `string`.
|
|
40
|
+
*
|
|
41
|
+
* Needs TypeScript 4.1+ (about 40 characters on TypeScript 4.1–4.4).
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* type A = CamelCase<'user_id'>; // 'userId'
|
|
46
|
+
* type B = CamelCase<'USER_ID'>; // 'userId'
|
|
47
|
+
* type C = CamelCase<'user-name id'>; // 'userNameId'
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export type CamelCase<S extends string> = string extends S ? string : Uncapitalize<JoinWords<ToUnderscores<S>>>;
|
|
51
|
+
export {};
|
package/dist/string.js
ADDED
package/dist/union.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The intersection of all the members of the union `U`.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* type Both = UnionToIntersection<{ id: number } | { name: string }>; // { id: number } & { name: string }
|
|
7
|
+
* ```
|
|
8
|
+
*/
|
|
9
|
+
export type UnionToIntersection<U> = (U extends unknown ? (value: U) => void : never) extends (value: infer I) => void ? I : never;
|
package/dist/union.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ashetm/types",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Small, dependency-free TypeScript utility types (Nullable,
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"description": "Small, dependency-free TypeScript utility types (Nullable, NonEmptyArray, DeepPartial, Brand, Prettify, JsonValue, Paths, CamelCase, ...). Types only: adds nothing to your bundle.",
|
|
5
5
|
"contributors": [
|
|
6
6
|
{
|
|
7
7
|
"email": "contact@ashetm.com",
|
|
@@ -17,17 +17,30 @@
|
|
|
17
17
|
"keywords": [
|
|
18
18
|
"angular",
|
|
19
19
|
"branded-types",
|
|
20
|
+
"camel-case",
|
|
20
21
|
"deep-partial",
|
|
21
22
|
"deep-readonly",
|
|
23
|
+
"deep-required",
|
|
24
|
+
"dot-path",
|
|
25
|
+
"json",
|
|
26
|
+
"kebab-case",
|
|
22
27
|
"maybe",
|
|
23
28
|
"mutable",
|
|
24
29
|
"nominal-types",
|
|
25
30
|
"non-empty-array",
|
|
26
31
|
"nullable",
|
|
32
|
+
"partial-by",
|
|
33
|
+
"paths",
|
|
34
|
+
"prettify",
|
|
35
|
+
"require-at-least-one",
|
|
36
|
+
"require-exactly-one",
|
|
37
|
+
"snake-case",
|
|
38
|
+
"template-literal-types",
|
|
27
39
|
"ts",
|
|
28
40
|
"type-utilities",
|
|
29
41
|
"types",
|
|
30
42
|
"typescript",
|
|
43
|
+
"union-to-intersection",
|
|
31
44
|
"utility-types"
|
|
32
45
|
],
|
|
33
46
|
"license": "MIT",
|
|
@@ -49,6 +62,13 @@
|
|
|
49
62
|
},
|
|
50
63
|
"module": "./dist/index.js",
|
|
51
64
|
"types": "./dist/index.d.ts",
|
|
65
|
+
"typesVersions": {
|
|
66
|
+
"<4.1": {
|
|
67
|
+
"*": [
|
|
68
|
+
"dist/legacy.d.ts"
|
|
69
|
+
]
|
|
70
|
+
}
|
|
71
|
+
},
|
|
52
72
|
"files": [
|
|
53
73
|
"dist"
|
|
54
74
|
],
|