@ashetm/types 1.2.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 CHANGED
@@ -1,6 +1,13 @@
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, JSON, dot paths, string cases and branded types.
3
+ [![npm version](https://img.shields.io/npm/v/@ashetm/types)](https://www.npmjs.com/package/@ashetm/types)
4
+ [![npm downloads](https://img.shields.io/npm/dm/@ashetm/types)](https://www.npmjs.com/package/@ashetm/types)
5
+ [![CI](https://github.com/AsheTM/npm-types/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/AsheTM/npm-types/actions/workflows/ci.yml)
6
+ [![TypeScript](https://img.shields.io/badge/TypeScript-%3E%3D3.7-3178c6?logo=typescript&logoColor=white)](#typescript-support)
7
+ [![dependencies](https://img.shields.io/badge/dependencies-0-brightgreen)](package.json)
8
+ [![license](https://img.shields.io/npm/l/@ashetm/types)](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
 
@@ -8,8 +15,8 @@ It is **types only**: it adds nothing to your bundle. It works with any project
8
15
 
9
16
  | TypeScript | Angular | Types |
10
17
  |------------|-------------|-------------------------------------------------------------------------------------------|
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 |
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 |
13
20
  | 4.5+ | 13.1+ | all |
14
21
 
15
22
  Your TypeScript version picks the right types by itself (`typesVersions`): nothing to configure.
@@ -43,12 +50,14 @@ On TypeScript 3.7, use `import { ... }` (`import type` needs TypeScript 3.8).
43
50
  |--------------------|-------------------------------------------------------------------------|
44
51
  | `NonEmptyArray<T>` | an array with at least one item: `[T, ...T[]]`. |
45
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). |
46
54
 
47
55
  ```ts
48
56
  const ids: NonEmptyArray<number> = [1, 2];
49
57
  const none: NonEmptyArray<number> = []; // error
50
58
 
51
59
  type Role = ArrayItem<readonly ['admin', 'user']>; // 'admin' | 'user'
60
+ type Rgb = Tuple<number, 3>; // [number, number, number]
52
61
  ```
53
62
 
54
63
  ### Objects
@@ -64,6 +73,11 @@ type Role = ArrayItem<readonly ['admin', 'user']>; // 'admin' | 'user'
64
73
  | `RequireAtLeastOne<T, K>` | at least one of the keys `K` (all keys by default) must be set. |
65
74
  | `RequireExactlyOne<T, K>` | exactly one of the keys `K` (all keys by default) must be set, the others are not allowed. |
66
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`. |
67
81
 
68
82
  ```ts
69
83
  const Status = { Active: 'active', Archived: 'archived' } as const;
@@ -86,6 +100,13 @@ type Reachable = RequireAtLeastOne<Contact, 'email' | 'phone'>;
86
100
  const contact: Reachable = { name: 'A' }; // error: email or phone is needed
87
101
 
88
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])[]
89
110
  ```
90
111
 
91
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.
@@ -95,9 +116,13 @@ TypeScript can't split an intersection back into its members, so there is no `In
95
116
  | Type | Is |
96
117
  |--------------------------|-------------------------------------------------|
97
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`. |
98
120
 
99
121
  ```ts
100
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'
101
126
  ```
102
127
 
103
128
  ### Deep types
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './legacy.js';
2
2
  export * from './path.js';
3
3
  export * from './string.js';
4
+ export * from './tuple.js';
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './legacy.js';
2
2
  export * from './path.js';
3
3
  export * from './string.js';
4
+ export * from './tuple.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]>;
@@ -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
@@ -0,0 +1,2 @@
1
+ // TypeScript 4.1+ only (recursive conditional types): not exported from `legacy.ts`.
2
+ export {};
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.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.",
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",
@@ -22,20 +22,26 @@
22
22
  "deep-readonly",
23
23
  "deep-required",
24
24
  "dot-path",
25
+ "entries",
25
26
  "json",
26
27
  "kebab-case",
28
+ "literal-union",
27
29
  "maybe",
30
+ "merge",
28
31
  "mutable",
29
32
  "nominal-types",
30
33
  "non-empty-array",
31
34
  "nullable",
35
+ "optional-keys",
32
36
  "partial-by",
33
37
  "paths",
34
38
  "prettify",
35
39
  "require-at-least-one",
36
40
  "require-exactly-one",
37
41
  "snake-case",
42
+ "strict-omit",
38
43
  "template-literal-types",
44
+ "tuple",
39
45
  "ts",
40
46
  "type-utilities",
41
47
  "types",