@ashetm/types 1.1.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +84 -2
- package/dist/index.d.ts +4 -7
- package/dist/index.js +4 -7
- package/dist/legacy.d.ts +7 -0
- package/dist/legacy.js +9 -0
- package/dist/object.d.ts +64 -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/tuple.d.ts +15 -0
- package/dist/tuple.js +2 -0
- package/dist/union.d.ts +14 -0
- package/package.json +21 -2
package/README.md
CHANGED
|
@@ -1,9 +1,26 @@
|
|
|
1
1
|
# @ashetm/types
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@ashetm/types)
|
|
4
|
+
[](https://www.npmjs.com/package/@ashetm/types)
|
|
5
|
+
[](https://github.com/AsheTM/npm-types/actions/workflows/ci.yml)
|
|
6
|
+
[](#typescript-support)
|
|
7
|
+
[](package.json)
|
|
8
|
+
[](LICENSE)
|
|
9
|
+
|
|
10
|
+
``@ashetm/types`` is a small, dependency-free library of TypeScript utility types: nullability, arrays and tuples, objects, unions, deep partial / readonly, JSON, dot paths, string cases and branded types.
|
|
4
11
|
|
|
5
12
|
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
13
|
|
|
14
|
+
## TypeScript support
|
|
15
|
+
|
|
16
|
+
| TypeScript | Angular | Types |
|
|
17
|
+
|------------|-------------|-------------------------------------------------------------------------------------------|
|
|
18
|
+
| 3.7 – 4.0 | 9 – 11.0 | all except `Tuple`, [Paths](#paths) and [String cases](#string-cases) |
|
|
19
|
+
| 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, `Tuple` up to 42 items |
|
|
20
|
+
| 4.5+ | 13.1+ | all |
|
|
21
|
+
|
|
22
|
+
Your TypeScript version picks the right types by itself (`typesVersions`): nothing to configure.
|
|
23
|
+
|
|
7
24
|
## Install
|
|
8
25
|
|
|
9
26
|
You can install it with npm:
|
|
@@ -33,12 +50,14 @@ On TypeScript 3.7, use `import { ... }` (`import type` needs TypeScript 3.8).
|
|
|
33
50
|
|--------------------|-------------------------------------------------------------------------|
|
|
34
51
|
| `NonEmptyArray<T>` | an array with at least one item: `[T, ...T[]]`. |
|
|
35
52
|
| `ArrayItem<T>` | the item type of an array or a tuple (readonly too). |
|
|
53
|
+
| `Tuple<T, N>` | a tuple of `N` items of type `T` (TypeScript 4.1+, up to 998 items). |
|
|
36
54
|
|
|
37
55
|
```ts
|
|
38
56
|
const ids: NonEmptyArray<number> = [1, 2];
|
|
39
57
|
const none: NonEmptyArray<number> = []; // error
|
|
40
58
|
|
|
41
59
|
type Role = ArrayItem<readonly ['admin', 'user']>; // 'admin' | 'user'
|
|
60
|
+
type Rgb = Tuple<number, 3>; // [number, number, number]
|
|
42
61
|
```
|
|
43
62
|
|
|
44
63
|
### Objects
|
|
@@ -54,6 +73,11 @@ type Role = ArrayItem<readonly ['admin', 'user']>; // 'admin' | 'user'
|
|
|
54
73
|
| `RequireAtLeastOne<T, K>` | at least one of the keys `K` (all keys by default) must be set. |
|
|
55
74
|
| `RequireExactlyOne<T, K>` | exactly one of the keys `K` (all keys by default) must be set, the others are not allowed. |
|
|
56
75
|
| `SplitKeys<T>` | a union with one object type per key of `T` (`readonly` and `?` kept). |
|
|
76
|
+
| `Merge<A, B>` | `A` with the properties of `B` on top: `B` wins on shared keys. |
|
|
77
|
+
| `StrictOmit<T, K>` | `Omit` that only accepts keys of `T`, so a typo is an error. |
|
|
78
|
+
| `OptionalKeys<T>` | the keys of `T` marked `?`. |
|
|
79
|
+
| `RequiredKeys<T>` | the keys of `T` not marked `?`. |
|
|
80
|
+
| `Entries<T>` | the `[key, value]` pairs of `T` (string keys), to type `Object.entries`. |
|
|
57
81
|
|
|
58
82
|
```ts
|
|
59
83
|
const Status = { Active: 'active', Archived: 'archived' } as const;
|
|
@@ -76,6 +100,13 @@ type Reachable = RequireAtLeastOne<Contact, 'email' | 'phone'>;
|
|
|
76
100
|
const contact: Reachable = { name: 'A' }; // error: email or phone is needed
|
|
77
101
|
|
|
78
102
|
type Change = SplitKeys<{ name: string; age?: number }>; // { name: string } | { age?: number }
|
|
103
|
+
|
|
104
|
+
type Row = Merge<User, { id: string }>; // { name: string; nickname?: string; id: string }
|
|
105
|
+
type Draft = StrictOmit<User, 'idd'>; // error: 'idd' is not a key of User
|
|
106
|
+
type Optional = OptionalKeys<User>; // 'nickname'
|
|
107
|
+
|
|
108
|
+
declare const user: User;
|
|
109
|
+
const entries = Object.entries(user) as Entries<User>; // (['id', number] | ['name', string] | ['nickname', string | undefined])[]
|
|
79
110
|
```
|
|
80
111
|
|
|
81
112
|
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.
|
|
@@ -85,9 +116,13 @@ TypeScript can't split an intersection back into its members, so there is no `In
|
|
|
85
116
|
| Type | Is |
|
|
86
117
|
|--------------------------|-------------------------------------------------|
|
|
87
118
|
| `UnionToIntersection<U>` | the intersection of all the members of `U`. |
|
|
119
|
+
| `LiteralUnion<T, Base>` | `T` or any other `Base` (`string` by default), keeping autocomplete for `T`. |
|
|
88
120
|
|
|
89
121
|
```ts
|
|
90
122
|
type Both = UnionToIntersection<{ id: number } | { name: string }>; // { id: number } & { name: string }
|
|
123
|
+
|
|
124
|
+
type Size = LiteralUnion<'sm' | 'md' | 'lg'>;
|
|
125
|
+
const size: Size = 'xl'; // allowed, and the editor still suggests 'sm', 'md' and 'lg'
|
|
91
126
|
```
|
|
92
127
|
|
|
93
128
|
### Deep types
|
|
@@ -136,6 +171,52 @@ const invalid: JsonValue = { at: new Date() }; // error
|
|
|
136
171
|
|
|
137
172
|
An `interface` has no index signature, so a value typed with one is not assignable to `JsonObject`: use a `type` instead.
|
|
138
173
|
|
|
174
|
+
### Paths
|
|
175
|
+
|
|
176
|
+
Needs TypeScript 4.1+.
|
|
177
|
+
|
|
178
|
+
| Type | Is |
|
|
179
|
+
|-------------------|--------------------------------------------------------------------------------------------|
|
|
180
|
+
| `Paths<T>` | every dot path of `T` (string keys; arrays and tuples use `${number}`). |
|
|
181
|
+
| `PathValue<T, P>` | the type at the dot path `P`: with `undefined` when a step is optional, `never` when the path does not exist. |
|
|
182
|
+
|
|
183
|
+
```ts
|
|
184
|
+
interface Settings {
|
|
185
|
+
theme: { color: string };
|
|
186
|
+
owner?: { name: string };
|
|
187
|
+
tags: string[];
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
type Path = Paths<Settings>; // 'theme' | 'theme.color' | 'owner' | 'owner.name' | 'tags' | `tags.${number}`
|
|
191
|
+
type Owner = PathValue<Settings, 'owner.name'>; // string | undefined
|
|
192
|
+
|
|
193
|
+
declare const settings: Settings;
|
|
194
|
+
declare function get<T, P extends Paths<T>>(value: T, path: P): PathValue<T, P>;
|
|
195
|
+
get(settings, 'theme.color'); // string
|
|
196
|
+
get(settings, 'theme.size'); // error
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
Optional values are followed; `Date`, functions, `Map`, `Set` and other built-ins are not. Self-referencing types (`children: Tree[]`) stop at 9 segments.
|
|
200
|
+
|
|
201
|
+
### String cases
|
|
202
|
+
|
|
203
|
+
Needs TypeScript 4.1+.
|
|
204
|
+
|
|
205
|
+
| Type | Is |
|
|
206
|
+
|----------------|------------------------------------------------------------------------------------------|
|
|
207
|
+
| `CamelCase<S>` | `S` in camelCase: words split on `_`, `-` and spaces; all-uppercase words lowercased first. |
|
|
208
|
+
| `KebabCase<S>` | `S` in kebab-case: words split on `_`, `-`, spaces and lowercase → uppercase changes. |
|
|
209
|
+
| `SnakeCase<S>` | `S` in snake_case: same words as `KebabCase`. |
|
|
210
|
+
|
|
211
|
+
```ts
|
|
212
|
+
type A = CamelCase<'user_id'>; // 'userId'
|
|
213
|
+
type B = CamelCase<'USER_ID'>; // 'userId'
|
|
214
|
+
type C = KebabCase<'userID'>; // 'user-id'
|
|
215
|
+
type D = SnakeCase<'user-name id'>; // 'user_name_id'
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
`string` stays `string`, and a union is converted member by member.
|
|
219
|
+
|
|
139
220
|
### Branded types
|
|
140
221
|
|
|
141
222
|
`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.
|
|
@@ -162,7 +243,8 @@ npm run test:compat
|
|
|
162
243
|
```
|
|
163
244
|
|
|
164
245
|
- `npm test` type-checks the type tests (`src/*.spec-d.ts`): what must compile, and `// @ts-expect-error` for what must not. Nothing runs.
|
|
165
|
-
- `npm run test:compat` builds, then checks `
|
|
246
|
+
- `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`).
|
|
247
|
+
- `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.
|
|
166
248
|
|
|
167
249
|
## License
|
|
168
250
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './
|
|
4
|
-
export * from './
|
|
5
|
-
export * from './nullable.js';
|
|
6
|
-
export * from './object.js';
|
|
7
|
-
export * from './union.js';
|
|
1
|
+
export * from './legacy.js';
|
|
2
|
+
export * from './path.js';
|
|
3
|
+
export * from './string.js';
|
|
4
|
+
export * from './tuple.js';
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './
|
|
4
|
-
export * from './
|
|
5
|
-
export * from './nullable.js';
|
|
6
|
-
export * from './object.js';
|
|
7
|
-
export * from './union.js';
|
|
1
|
+
export * from './legacy.js';
|
|
2
|
+
export * from './path.js';
|
|
3
|
+
export * from './string.js';
|
|
4
|
+
export * from './tuple.js';
|
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
|
@@ -145,3 +145,67 @@ export type RequireExactlyOne<T, K extends keyof T = keyof T> = {
|
|
|
145
145
|
export type SplitKeys<T> = {
|
|
146
146
|
[K in keyof T]-?: Prettify<Pick<T, K>>;
|
|
147
147
|
}[keyof T];
|
|
148
|
+
/**
|
|
149
|
+
* `A` with the properties of `B` on top: `B` wins on shared keys.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```ts
|
|
153
|
+
* type Row = Merge<{ id: number; name: string }, { id: string }>; // { name: string; id: string }
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
export type Merge<A, B> = Prettify<Omit<A, keyof B> & B>;
|
|
157
|
+
/**
|
|
158
|
+
* `Omit` that only accepts keys of `T`, so a typo or a renamed key is an error.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```ts
|
|
162
|
+
* interface User {
|
|
163
|
+
* id: number;
|
|
164
|
+
* name: string;
|
|
165
|
+
* }
|
|
166
|
+
*
|
|
167
|
+
* type Draft = StrictOmit<User, 'id'>; // { name: string }
|
|
168
|
+
* type Typo = StrictOmit<User, 'idd'>; // error
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
export type StrictOmit<T, K extends keyof T> = Omit<T, K>;
|
|
172
|
+
/**
|
|
173
|
+
* The keys of `T` marked `?`.
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```ts
|
|
177
|
+
* type Keys = OptionalKeys<{ id: number; name?: string }>; // 'name'
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
export type OptionalKeys<T> = {
|
|
181
|
+
[K in keyof T]-?: Record<never, never> extends Pick<T, K> ? K : never;
|
|
182
|
+
}[keyof T];
|
|
183
|
+
/**
|
|
184
|
+
* The keys of `T` not marked `?` (a value that can be `undefined` is still required).
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* ```ts
|
|
188
|
+
* type Keys = RequiredKeys<{ id: number; name?: string }>; // 'id'
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
191
|
+
export type RequiredKeys<T> = {
|
|
192
|
+
[K in keyof T]-?: Record<never, never> extends Pick<T, K> ? never : K;
|
|
193
|
+
}[keyof T];
|
|
194
|
+
/**
|
|
195
|
+
* The `[key, value]` pairs of the object `T`, to type `Object.entries`. String keys only,
|
|
196
|
+
* like `Object.entries`.
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* ```ts
|
|
200
|
+
* interface User {
|
|
201
|
+
* id: number;
|
|
202
|
+
* name: string;
|
|
203
|
+
* }
|
|
204
|
+
*
|
|
205
|
+
* declare const user: User;
|
|
206
|
+
* const entries = Object.entries(user) as Entries<User>; // (['id', number] | ['name', string])[]
|
|
207
|
+
* ```
|
|
208
|
+
*/
|
|
209
|
+
export type Entries<T> = Array<{
|
|
210
|
+
[K in keyof T]-?: K extends string ? [K, T[K]] : never;
|
|
211
|
+
}[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/tuple.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
type BuildTuple<T, N extends number, Result extends unknown[] = []> = Result['length'] extends N ? Result : BuildTuple<T, N, [T, ...Result]>;
|
|
2
|
+
/**
|
|
3
|
+
* A tuple of `N` items of type `T`. `number` gives `T[]`; a negative or decimal `N` gives `never`;
|
|
4
|
+
* a union of lengths gives a union of tuples.
|
|
5
|
+
*
|
|
6
|
+
* Needs TypeScript 4.1+ (`N` up to 42 on TypeScript 4.1–4.4, 998 on TypeScript 4.5+).
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* type Rgb = Tuple<number, 3>; // [number, number, number]
|
|
11
|
+
* type Pair = Tuple<string, 2>; // [string, string]
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export type Tuple<T, N extends number> = number extends N ? T[] : N extends unknown ? `${N}` extends `-${string}` | `${string}.${string}` ? never : BuildTuple<T, N> : never;
|
|
15
|
+
export {};
|
package/dist/tuple.js
ADDED
package/dist/union.d.ts
CHANGED
|
@@ -7,3 +7,17 @@
|
|
|
7
7
|
* ```
|
|
8
8
|
*/
|
|
9
9
|
export type UnionToIntersection<U> = (U extends unknown ? (value: U) => void : never) extends (value: infer I) => void ? I : never;
|
|
10
|
+
/**
|
|
11
|
+
* `T` or any other `Base` (`string` by default), keeping editor autocomplete for the members of `T`.
|
|
12
|
+
* A plain `'sm' | 'md' | string` collapses to `string` and loses the suggestions.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* type Size = LiteralUnion<'sm' | 'md' | 'lg'>;
|
|
17
|
+
*
|
|
18
|
+
* const a: Size = 'md'; // suggested
|
|
19
|
+
* const b: Size = 'xl'; // allowed
|
|
20
|
+
* const c: Size = 1; // error
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export type LiteralUnion<T extends Base, Base = string> = T | (Base & Record<never, never>);
|
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, NonEmptyArray, DeepPartial,
|
|
3
|
+
"version": "1.3.0",
|
|
4
|
+
"description": "Small, dependency-free TypeScript utility types (Nullable, NonEmptyArray, DeepPartial, Brand, Prettify, LiteralUnion, Merge, JsonValue, Paths, CamelCase, Tuple, ...). Types only: adds nothing to your bundle.",
|
|
5
5
|
"contributors": [
|
|
6
6
|
{
|
|
7
7
|
"email": "contact@ashetm.com",
|
|
@@ -17,19 +17,31 @@
|
|
|
17
17
|
"keywords": [
|
|
18
18
|
"angular",
|
|
19
19
|
"branded-types",
|
|
20
|
+
"camel-case",
|
|
20
21
|
"deep-partial",
|
|
21
22
|
"deep-readonly",
|
|
22
23
|
"deep-required",
|
|
24
|
+
"dot-path",
|
|
25
|
+
"entries",
|
|
23
26
|
"json",
|
|
27
|
+
"kebab-case",
|
|
28
|
+
"literal-union",
|
|
24
29
|
"maybe",
|
|
30
|
+
"merge",
|
|
25
31
|
"mutable",
|
|
26
32
|
"nominal-types",
|
|
27
33
|
"non-empty-array",
|
|
28
34
|
"nullable",
|
|
35
|
+
"optional-keys",
|
|
29
36
|
"partial-by",
|
|
37
|
+
"paths",
|
|
30
38
|
"prettify",
|
|
31
39
|
"require-at-least-one",
|
|
32
40
|
"require-exactly-one",
|
|
41
|
+
"snake-case",
|
|
42
|
+
"strict-omit",
|
|
43
|
+
"template-literal-types",
|
|
44
|
+
"tuple",
|
|
33
45
|
"ts",
|
|
34
46
|
"type-utilities",
|
|
35
47
|
"types",
|
|
@@ -56,6 +68,13 @@
|
|
|
56
68
|
},
|
|
57
69
|
"module": "./dist/index.js",
|
|
58
70
|
"types": "./dist/index.d.ts",
|
|
71
|
+
"typesVersions": {
|
|
72
|
+
"<4.1": {
|
|
73
|
+
"*": [
|
|
74
|
+
"dist/legacy.d.ts"
|
|
75
|
+
]
|
|
76
|
+
}
|
|
77
|
+
},
|
|
59
78
|
"files": [
|
|
60
79
|
"dist"
|
|
61
80
|
],
|