ts-data-forge 12.0.0 → 12.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.
@@ -3,6 +3,7 @@ import {
3
3
  type FixedLengthArray,
4
4
  type MaxLengthArray,
5
5
  type MinLengthArray,
6
+ type NonEmptyArray,
6
7
  type SupportedLength,
7
8
  } from 'ts-type-forge';
8
9
  import {
@@ -22,6 +23,9 @@ import {
22
23
  * structural tuple prefix (in-range indexed access without `undefined` under
23
24
  * `noUncheckedIndexedAccess`).
24
25
  *
26
+ * Supports a curried overload: calling it with only `length` returns a
27
+ * function that casts an array, which is convenient for `pipe`/`map`.
28
+ *
25
29
  * @example
26
30
  *
27
31
  * ```ts
@@ -30,6 +34,10 @@ import {
30
34
  * const atMost5: MaxLengthArray<5, number> = rgb; // OK (3 <= 5)
31
35
  * const red: number = rgb[0]; // OK — no `undefined`
32
36
  *
37
+ * // curried version
38
+ * const asRgb = Arr.asFixedLengthArray(3);
39
+ * const green = asRgb([0, 255, 0]);
40
+ *
33
41
  * // Arr.asFixedLengthArray([255, 128], 3); // throws TypeError
34
42
  * ```
35
43
  *
@@ -40,21 +48,43 @@ import {
40
48
  * @returns `xs` typed as `FixedLengthArray<Length, Xs[number]> & Xs`.
41
49
  * @throws {TypeError} If `xs.length !== length`.
42
50
  */
43
- export const asFixedLengthArray = <
51
+ export function asFixedLengthArray<
44
52
  Xs extends readonly unknown[],
45
53
  Length extends SupportedLength,
46
- >(
47
- xs: Xs,
54
+ >(xs: Xs, length: Length): FixedLengthArray<Length, Xs[number]> & Xs;
55
+
56
+ // Curried version
57
+ export function asFixedLengthArray<Length extends SupportedLength>(
48
58
  length: Length,
49
- ): FixedLengthArray<Length, Xs[number]> & Xs => {
50
- if (!isFixedLengthArray(xs, length)) {
51
- throw new TypeError(
52
- `Expected an array of length ${length}, got an array of length ${xs.length}`,
53
- );
54
- }
59
+ ): <Xs extends readonly unknown[]>(
60
+ xs: Xs,
61
+ ) => FixedLengthArray<Length, Xs[number]> & Xs;
55
62
 
56
- return xs;
57
- };
63
+ export function asFixedLengthArray<E>(
64
+ ...args:
65
+ | readonly [xs: readonly E[], length: SupportedLength]
66
+ | readonly [length: SupportedLength]
67
+ ): readonly E[] | ((xs: readonly E[]) => readonly E[]) {
68
+ switch (args.length) {
69
+ case 2: {
70
+ const [xs, length] = args;
71
+
72
+ if (!isFixedLengthArray(xs, length)) {
73
+ throw new TypeError(
74
+ `Expected an array of length ${length}, got an array of length ${xs.length}`,
75
+ );
76
+ }
77
+
78
+ return xs;
79
+ }
80
+
81
+ case 1: {
82
+ const [length] = args;
83
+
84
+ return (xs) => asFixedLengthArray(xs, length);
85
+ }
86
+ }
87
+ }
58
88
 
59
89
  /**
60
90
  * Casts an array to `MinLengthArray<MinLength, E>` after checking that it has
@@ -66,6 +96,9 @@ export const asFixedLengthArray = <
66
96
  * structural tuple prefix (indexed access below `min(N, 10)` without
67
97
  * `undefined` under `noUncheckedIndexedAccess`).
68
98
  *
99
+ * Supports a curried overload: calling it with only `minLength` returns a
100
+ * function that casts an array, which is convenient for `pipe`/`map`.
101
+ *
69
102
  * @example
70
103
  *
71
104
  * ```ts
@@ -74,6 +107,10 @@ export const asFixedLengthArray = <
74
107
  * const nonEmpty: MinLengthArray<1, number> = history; // OK (3 >= 1)
75
108
  * const first: number = history[0]; // OK — no `undefined`
76
109
  *
110
+ * // curried version
111
+ * const asHistory = Arr.asMinLengthArray(3);
112
+ * const next = asHistory([4, 5, 6, 7]);
113
+ *
77
114
  * // Arr.asMinLengthArray([0], 3); // throws TypeError
78
115
  * ```
79
116
  *
@@ -84,21 +121,43 @@ export const asFixedLengthArray = <
84
121
  * @returns `xs` typed as `MinLengthArray<MinLength, Xs[number]> & Xs`.
85
122
  * @throws {TypeError} If `xs.length < minLength`.
86
123
  */
87
- export const asMinLengthArray = <
124
+ export function asMinLengthArray<
88
125
  Xs extends readonly unknown[],
89
126
  MinLength extends SupportedLength,
90
- >(
91
- xs: Xs,
127
+ >(xs: Xs, minLength: MinLength): MinLengthArray<MinLength, Xs[number]> & Xs;
128
+
129
+ // Curried version
130
+ export function asMinLengthArray<MinLength extends SupportedLength>(
92
131
  minLength: MinLength,
93
- ): MinLengthArray<MinLength, Xs[number]> & Xs => {
94
- if (!isMinLengthArray(xs, minLength)) {
95
- throw new TypeError(
96
- `Expected an array of length >= ${minLength}, got an array of length ${xs.length}`,
97
- );
98
- }
132
+ ): <Xs extends readonly unknown[]>(
133
+ xs: Xs,
134
+ ) => MinLengthArray<MinLength, Xs[number]> & Xs;
99
135
 
100
- return xs;
101
- };
136
+ export function asMinLengthArray<E>(
137
+ ...args:
138
+ | readonly [xs: readonly E[], minLength: SupportedLength]
139
+ | readonly [minLength: SupportedLength]
140
+ ): readonly E[] | ((xs: readonly E[]) => readonly E[]) {
141
+ switch (args.length) {
142
+ case 2: {
143
+ const [xs, minLength] = args;
144
+
145
+ if (!isMinLengthArray(xs, minLength)) {
146
+ throw new TypeError(
147
+ `Expected an array of length >= ${minLength}, got an array of length ${xs.length}`,
148
+ );
149
+ }
150
+
151
+ return xs;
152
+ }
153
+
154
+ case 1: {
155
+ const [minLength] = args;
156
+
157
+ return (xs) => asMinLengthArray(xs, minLength);
158
+ }
159
+ }
160
+ }
102
161
 
103
162
  /**
104
163
  * Casts an array to `MaxLengthArray<MaxLength, E>` after checking that it has
@@ -108,6 +167,9 @@ export const asMinLengthArray = <
108
167
  * length is verified at runtime, and the original array type (e.g. tuple
109
168
  * types) is preserved via intersection.
110
169
  *
170
+ * Supports a curried overload: calling it with only `maxLength` returns a
171
+ * function that casts an array, which is convenient for `pipe`/`map`.
172
+ *
111
173
  * @example
112
174
  *
113
175
  * ```ts
@@ -115,6 +177,10 @@ export const asMinLengthArray = <
115
177
  *
116
178
  * const relaxed: MaxLengthArray<16, string> = tags; // OK (8 <= 16)
117
179
  *
180
+ * // curried version
181
+ * const asTags = Arr.asMaxLengthArray(8);
182
+ * const more = asTags(['d', 'e']);
183
+ *
118
184
  * // Arr.asMaxLengthArray(['a', 'b', 'c'], 2); // throws TypeError
119
185
  * ```
120
186
  *
@@ -125,21 +191,43 @@ export const asMinLengthArray = <
125
191
  * @returns `xs` typed as `MaxLengthArray<MaxLength, Xs[number]> & Xs`.
126
192
  * @throws {TypeError} If `xs.length > maxLength`.
127
193
  */
128
- export const asMaxLengthArray = <
194
+ export function asMaxLengthArray<
129
195
  Xs extends readonly unknown[],
130
196
  MaxLength extends SupportedLength,
131
- >(
132
- xs: Xs,
197
+ >(xs: Xs, maxLength: MaxLength): MaxLengthArray<MaxLength, Xs[number]> & Xs;
198
+
199
+ // Curried version
200
+ export function asMaxLengthArray<MaxLength extends SupportedLength>(
133
201
  maxLength: MaxLength,
134
- ): MaxLengthArray<MaxLength, Xs[number]> & Xs => {
135
- if (!isMaxLengthArray(xs, maxLength)) {
136
- throw new TypeError(
137
- `Expected an array of length <= ${maxLength}, got an array of length ${xs.length}`,
138
- );
139
- }
202
+ ): <Xs extends readonly unknown[]>(
203
+ xs: Xs,
204
+ ) => MaxLengthArray<MaxLength, Xs[number]> & Xs;
140
205
 
141
- return xs;
142
- };
206
+ export function asMaxLengthArray<E>(
207
+ ...args:
208
+ | readonly [xs: readonly E[], maxLength: SupportedLength]
209
+ | readonly [maxLength: SupportedLength]
210
+ ): readonly E[] | ((xs: readonly E[]) => readonly E[]) {
211
+ switch (args.length) {
212
+ case 2: {
213
+ const [xs, maxLength] = args;
214
+
215
+ if (!isMaxLengthArray(xs, maxLength)) {
216
+ throw new TypeError(
217
+ `Expected an array of length <= ${maxLength}, got an array of length ${xs.length}`,
218
+ );
219
+ }
220
+
221
+ return xs;
222
+ }
223
+
224
+ case 1: {
225
+ const [maxLength] = args;
226
+
227
+ return (xs) => asMaxLengthArray(xs, maxLength);
228
+ }
229
+ }
230
+ }
143
231
 
144
232
  /**
145
233
  * Casts an array to `BoundedLengthArray<MinLength, MaxLength, E>` after
@@ -151,6 +239,10 @@ export const asMaxLengthArray = <
151
239
  * at runtime, and the original array type (e.g. tuple types) is preserved via
152
240
  * intersection.
153
241
  *
242
+ * Supports a curried overload: calling it with only `minLength` and
243
+ * `maxLength` returns a function that casts an array, which is convenient for
244
+ * `pipe`/`map`.
245
+ *
154
246
  * @example
155
247
  *
156
248
  * ```ts
@@ -158,6 +250,10 @@ export const asMaxLengthArray = <
158
250
  *
159
251
  * const relaxed: BoundedLengthArray<0, 100, number> = selection; // OK
160
252
  *
253
+ * // curried version
254
+ * const asSelection = Arr.asBoundedLengthArray(1, 5);
255
+ * const next = asSelection([4, 5]);
256
+ *
161
257
  * // Arr.asBoundedLengthArray([], 1, 5); // throws TypeError
162
258
  * ```
163
259
  *
@@ -171,7 +267,7 @@ export const asMaxLengthArray = <
171
267
  * `BoundedLengthArray<MinLength, MaxLength, Xs[number]> & Xs`.
172
268
  * @throws {TypeError} If `xs.length < minLength || xs.length > maxLength`.
173
269
  */
174
- export const asBoundedLengthArray = <
270
+ export function asBoundedLengthArray<
175
271
  Xs extends readonly unknown[],
176
272
  MinLength extends SupportedLength,
177
273
  MaxLength extends SupportedLength,
@@ -179,11 +275,80 @@ export const asBoundedLengthArray = <
179
275
  xs: Xs,
180
276
  minLength: MinLength,
181
277
  maxLength: MaxLength,
182
- ): BoundedLengthArray<MinLength, MaxLength, Xs[number]> & Xs => {
183
- if (!isBoundedLengthArray(xs, minLength, maxLength)) {
184
- throw new TypeError(
185
- `Expected an array of length in [${minLength}, ${maxLength}], got an array of length ${xs.length}`,
186
- );
278
+ ): BoundedLengthArray<MinLength, MaxLength, Xs[number]> & Xs;
279
+
280
+ // Curried version
281
+ export function asBoundedLengthArray<
282
+ MinLength extends SupportedLength,
283
+ MaxLength extends SupportedLength,
284
+ >(
285
+ minLength: MinLength,
286
+ maxLength: MaxLength,
287
+ ): <Xs extends readonly unknown[]>(
288
+ xs: Xs,
289
+ ) => BoundedLengthArray<MinLength, MaxLength, Xs[number]> & Xs;
290
+
291
+ export function asBoundedLengthArray<E>(
292
+ ...args:
293
+ | readonly [
294
+ xs: readonly E[],
295
+ minLength: SupportedLength,
296
+ maxLength: SupportedLength,
297
+ ]
298
+ | readonly [minLength: SupportedLength, maxLength: SupportedLength]
299
+ ): readonly E[] | ((xs: readonly E[]) => readonly E[]) {
300
+ switch (args.length) {
301
+ case 3: {
302
+ const [xs, minLength, maxLength] = args;
303
+
304
+ if (!isBoundedLengthArray(xs, minLength, maxLength)) {
305
+ throw new TypeError(
306
+ `Expected an array of length in [${minLength}, ${maxLength}], got an array of length ${xs.length}`,
307
+ );
308
+ }
309
+
310
+ return xs;
311
+ }
312
+
313
+ case 2: {
314
+ const [minLength, maxLength] = args;
315
+
316
+ return (xs) => asBoundedLengthArray(xs, minLength, maxLength);
317
+ }
318
+ }
319
+ }
320
+
321
+ /**
322
+ * Casts an array to `NonEmptyArray<E>` after checking that it has at least one
323
+ * element.
324
+ *
325
+ * `NonEmptyArray<E>` is an alias of `MinLengthArray<1, E>`, so this is the
326
+ * length-1 specialization of {@link asMinLengthArray}. Use this instead of
327
+ * writing `xs as unknown as NonEmptyArray<E>`: the length is verified at
328
+ * runtime, and the original array type (e.g. tuple types) is preserved via
329
+ * intersection, so the result also benefits from the structural tuple prefix
330
+ * (`xs[0]` without `undefined` under `noUncheckedIndexedAccess`).
331
+ *
332
+ * @example
333
+ *
334
+ * ```ts
335
+ * const history = Arr.asNonEmptyArray([0, 1, 2, 3]);
336
+ *
337
+ * const first: number = history[0]; // OK — no `undefined`
338
+ *
339
+ * // Arr.asNonEmptyArray([]); // throws TypeError
340
+ * ```
341
+ *
342
+ * @template Xs - The input array type (tuple types are preserved).
343
+ * @param xs - The array to cast.
344
+ * @returns `xs` typed as `NonEmptyArray<Xs[number]> & Xs`.
345
+ * @throws {TypeError} If `xs` is empty.
346
+ */
347
+ export const asNonEmptyArray = <Xs extends readonly unknown[]>(
348
+ xs: Xs,
349
+ ): NonEmptyArray<Xs[number]> & Xs => {
350
+ if (!isMinLengthArray(xs, 1)) {
351
+ throw new TypeError('Expected a non-empty array, got an empty array');
187
352
  }
188
353
 
189
354
  return xs;
@@ -3,6 +3,7 @@ import {
3
3
  type FixedLengthArray,
4
4
  type MaxLengthArray,
5
5
  type MinLengthArray,
6
+ type NonEmptyArray,
6
7
  } from 'ts-type-forge';
7
8
  import { expectType } from '../../expect-type.mjs';
8
9
  import {
@@ -10,6 +11,7 @@ import {
10
11
  asFixedLengthArray,
11
12
  asMaxLengthArray,
12
13
  asMinLengthArray,
14
+ asNonEmptyArray,
13
15
  } from './array-utils-length-bounded-array-cast.mjs';
14
16
 
15
17
  describe(asFixedLengthArray, () => {
@@ -47,6 +49,29 @@ describe(asFixedLengthArray, () => {
47
49
 
48
50
  expectType<typeof casted, FixedLengthArray<3, 1 | 2 | 3>>('<=');
49
51
  });
52
+
53
+ test('curried version casts an array', () => {
54
+ const xs: readonly number[] = [0, 1, 2, 3] as const;
55
+
56
+ const casted = asFixedLengthArray(4)(xs);
57
+
58
+ expect(casted).toBe(xs);
59
+
60
+ expectType<typeof casted, FixedLengthArray<4, number>>('<=');
61
+
62
+ // in-range indexed access does not include `undefined`
63
+ const _first = casted[0];
64
+
65
+ expectType<typeof _first, number>('=');
66
+ });
67
+
68
+ test('curried version throws when the length does not match', () => {
69
+ expect(() => asFixedLengthArray(4)([0, 1, 2])).toThrow(TypeError);
70
+
71
+ expect(() => asFixedLengthArray(4)([0, 1, 2])).toThrow(
72
+ 'Expected an array of length 4, got an array of length 3',
73
+ );
74
+ });
50
75
  });
51
76
 
52
77
  describe(asMinLengthArray, () => {
@@ -69,6 +94,26 @@ describe(asMinLengthArray, () => {
69
94
  'Expected an array of length >= 3, got an array of length 1',
70
95
  );
71
96
  });
97
+
98
+ test('curried version casts an array', () => {
99
+ const xs: readonly number[] = [0, 1, 2, 3] as const;
100
+
101
+ const casted = asMinLengthArray(3)(xs);
102
+
103
+ expect(casted).toBe(xs);
104
+
105
+ expectType<typeof casted, MinLengthArray<3, number>>('<=');
106
+
107
+ expectType<typeof casted, MinLengthArray<1, number>>('<='); // 3 >= 1
108
+ });
109
+
110
+ test('curried version throws when the array is too short', () => {
111
+ expect(() => asMinLengthArray(3)([0])).toThrow(TypeError);
112
+
113
+ expect(() => asMinLengthArray(3)([0])).toThrow(
114
+ 'Expected an array of length >= 3, got an array of length 1',
115
+ );
116
+ });
72
117
  });
73
118
 
74
119
  describe(asMaxLengthArray, () => {
@@ -91,6 +136,26 @@ describe(asMaxLengthArray, () => {
91
136
  'Expected an array of length <= 2, got an array of length 3',
92
137
  );
93
138
  });
139
+
140
+ test('curried version casts an array', () => {
141
+ const xs: readonly number[] = [0, 1, 2] as const;
142
+
143
+ const casted = asMaxLengthArray(8)(xs);
144
+
145
+ expect(casted).toBe(xs);
146
+
147
+ expectType<typeof casted, MaxLengthArray<8, number>>('<=');
148
+
149
+ expectType<typeof casted, MaxLengthArray<16, number>>('<='); // 8 <= 16
150
+ });
151
+
152
+ test('curried version throws when the array is too long', () => {
153
+ expect(() => asMaxLengthArray(2)([0, 1, 2])).toThrow(TypeError);
154
+
155
+ expect(() => asMaxLengthArray(2)([0, 1, 2])).toThrow(
156
+ 'Expected an array of length <= 2, got an array of length 3',
157
+ );
158
+ });
94
159
  });
95
160
 
96
161
  describe(asBoundedLengthArray, () => {
@@ -115,4 +180,65 @@ describe(asBoundedLengthArray, () => {
115
180
  'Expected an array of length in [1, 5], got an array of length 6',
116
181
  );
117
182
  });
183
+
184
+ test('curried version casts an array', () => {
185
+ const xs: readonly number[] = [1, 2, 3] as const;
186
+
187
+ const casted = asBoundedLengthArray(1, 5)(xs);
188
+
189
+ expect(casted).toBe(xs);
190
+
191
+ expectType<typeof casted, BoundedLengthArray<1, 5, number>>('<=');
192
+
193
+ expectType<typeof casted, MinLengthArray<1, number>>('<=');
194
+
195
+ expectType<typeof casted, MaxLengthArray<5, number>>('<=');
196
+ });
197
+
198
+ test('curried version throws when the length is out of the range', () => {
199
+ expect(() => asBoundedLengthArray(1, 5)([])).toThrow(TypeError);
200
+
201
+ expect(() => asBoundedLengthArray(1, 5)([1, 2, 3, 4, 5, 6])).toThrow(
202
+ 'Expected an array of length in [1, 5], got an array of length 6',
203
+ );
204
+ });
205
+ });
206
+
207
+ describe(asNonEmptyArray, () => {
208
+ test('should return the input when the array is non-empty', () => {
209
+ const xs: readonly number[] = [0, 1, 2, 3] as const;
210
+
211
+ const casted = asNonEmptyArray(xs);
212
+
213
+ expect(casted).toBe(xs);
214
+
215
+ expectType<typeof casted, NonEmptyArray<number>>('<=');
216
+
217
+ expectType<typeof casted, MinLengthArray<1, number>>('<=');
218
+
219
+ // in-range indexed access does not include `undefined`
220
+ const _first = casted[0];
221
+
222
+ expectType<typeof _first, number>('=');
223
+ });
224
+
225
+ test('should throw when the array is empty', () => {
226
+ expect(() => asNonEmptyArray([])).toThrow(TypeError);
227
+
228
+ expect(() => asNonEmptyArray([])).toThrow(
229
+ 'Expected a non-empty array, got an empty array',
230
+ );
231
+ });
232
+
233
+ test('should preserve the original array type', () => {
234
+ const tuple = [1, 2, 3] as const;
235
+
236
+ const casted = asNonEmptyArray(tuple);
237
+
238
+ expect(casted).toBe(tuple);
239
+
240
+ expectType<typeof casted, readonly [1, 2, 3]>('<=');
241
+
242
+ expectType<typeof casted, NonEmptyArray<1 | 2 | 3>>('<=');
243
+ });
118
244
  });
@@ -266,19 +266,26 @@ export function toRemoved<E>(
266
266
  *
267
267
  * const appendedCurried = Arr.toPushed(4)(base);
268
268
  *
269
- * assert.deepStrictEqual(appended, [1, 2, 3]);
269
+ * // The result is guaranteed non-empty, so it is assignable to NonEmptyArray.
270
+ * const nonEmpty: NonEmptyArray<number> = appended;
270
271
  *
271
- * assert.deepStrictEqual(appendedCurried, [1, 2, 4]);
272
+ * assert.deepStrictEqual<readonly number[]>(appended, [1, 2, 3]);
273
+ *
274
+ * assert.deepStrictEqual<readonly number[]>(appendedCurried, [1, 2, 4]);
275
+ *
276
+ * assert.isTrue(nonEmpty.length >= 1);
272
277
  * ```
273
278
  */
274
279
  export function toPushed<const Ar extends readonly unknown[], const V>(
275
280
  array: Ar,
276
281
  newValue: V,
277
- ): readonly [...Ar, V];
282
+ ): readonly [...Ar, V] & NonEmptyArray<Ar[number] | V>;
278
283
 
279
284
  export function toPushed<const V>(
280
285
  newValue: V,
281
- ): <const Ar extends readonly unknown[]>(array: Ar) => readonly [...Ar, V];
286
+ ): <const Ar extends readonly unknown[]>(
287
+ array: Ar,
288
+ ) => readonly [...Ar, V] & NonEmptyArray<Ar[number] | V>;
282
289
 
283
290
  export function toPushed<const Ar extends readonly unknown[], const V>(
284
291
  ...args: readonly [array: Ar, newValue: V] | readonly [newValue: V]
@@ -314,19 +321,26 @@ export function toPushed<const Ar extends readonly unknown[], const V>(
314
321
  *
315
322
  * const prefixedCurried = Arr.toUnshifted(0)(base);
316
323
  *
317
- * assert.deepStrictEqual(prefixed, [1, 2, 3]);
324
+ * // The result is guaranteed non-empty, so it is assignable to NonEmptyArray.
325
+ * const nonEmpty: NonEmptyArray<number> = prefixed;
318
326
  *
319
- * assert.deepStrictEqual(prefixedCurried, [0, 2, 3]);
327
+ * assert.deepStrictEqual<readonly number[]>(prefixed, [1, 2, 3]);
328
+ *
329
+ * assert.deepStrictEqual<readonly number[]>(prefixedCurried, [0, 2, 3]);
330
+ *
331
+ * assert.isTrue(nonEmpty.length >= 1);
320
332
  * ```
321
333
  */
322
334
  export function toUnshifted<const Ar extends readonly unknown[], const V>(
323
335
  array: Ar,
324
336
  newValue: V,
325
- ): readonly [V, ...Ar];
337
+ ): readonly [V, ...Ar] & NonEmptyArray<V | Ar[number]>;
326
338
 
327
339
  export function toUnshifted<const V>(
328
340
  newValue: V,
329
- ): <const Ar extends readonly unknown[]>(array: Ar) => readonly [V, ...Ar];
341
+ ): <const Ar extends readonly unknown[]>(
342
+ array: Ar,
343
+ ) => readonly [V, ...Ar] & NonEmptyArray<V | Ar[number]>;
330
344
 
331
345
  export function toUnshifted<Ar extends readonly unknown[], const V>(
332
346
  ...args: readonly [array: Ar, newValue: V] | readonly [newValue: V]
@@ -134,9 +134,18 @@ describe('Arr modifications', () => {
134
134
 
135
135
  const result = toPushed(xs, 4 as const);
136
136
 
137
- expectType<typeof result, readonly [1, 2, 3, 4]>('=');
137
+ // The structural tuple is preserved, and the result is additionally
138
+ // branded as non-empty so it is assignable to `NonEmptyArray`.
139
+ expectType<
140
+ typeof result,
141
+ readonly [1, 2, 3, 4] & NonEmptyArray<1 | 2 | 3 | 4>
142
+ >('=');
138
143
 
139
- assert.deepStrictEqual(result, [1, 2, 3, 4]);
144
+ expectType<typeof result, readonly [1, 2, 3, 4]>('<=');
145
+
146
+ expectType<typeof result, NonEmptyArray<number>>('<=');
147
+
148
+ assert.deepStrictEqual<readonly number[]>(result, [1, 2, 3, 4]);
140
149
  });
141
150
 
142
151
  test('case 2', () => {
@@ -144,9 +153,14 @@ describe('Arr modifications', () => {
144
153
 
145
154
  const result = toPushed(xs, 4 as const);
146
155
 
147
- expectType<typeof result, readonly [...number[], 4]>('=');
156
+ expectType<
157
+ typeof result,
158
+ readonly [...number[], 4] & NonEmptyArray<number>
159
+ >('=');
148
160
 
149
- assert.deepStrictEqual(result, [1, 2, 3, 4]);
161
+ expectType<typeof result, NonEmptyArray<number>>('<=');
162
+
163
+ assert.deepStrictEqual<readonly number[]>(result, [1, 2, 3, 4]);
150
164
  });
151
165
 
152
166
  test('should work with curried version', () => {
@@ -154,7 +168,9 @@ describe('Arr modifications', () => {
154
168
 
155
169
  const result = pushFive([1, 2, 3]);
156
170
 
157
- assert.deepStrictEqual(result, [1, 2, 3, 5]);
171
+ expectType<typeof result, NonEmptyArray<number>>('<=');
172
+
173
+ assert.deepStrictEqual<readonly number[]>(result, [1, 2, 3, 5]);
158
174
  });
159
175
  });
160
176
 
@@ -164,9 +180,16 @@ describe('Arr modifications', () => {
164
180
 
165
181
  const result = toUnshifted(xs, 4 as const);
166
182
 
167
- expectType<typeof result, readonly [4, 1, 2, 3]>('=');
183
+ expectType<
184
+ typeof result,
185
+ readonly [4, 1, 2, 3] & NonEmptyArray<1 | 2 | 3 | 4>
186
+ >('=');
168
187
 
169
- assert.deepStrictEqual(result, [4, 1, 2, 3]);
188
+ expectType<typeof result, readonly [4, 1, 2, 3]>('<=');
189
+
190
+ expectType<typeof result, NonEmptyArray<number>>('<=');
191
+
192
+ assert.deepStrictEqual<readonly number[]>(result, [4, 1, 2, 3]);
170
193
  });
171
194
 
172
195
  test('case 2', () => {
@@ -174,9 +197,14 @@ describe('Arr modifications', () => {
174
197
 
175
198
  const result = toUnshifted(xs, 4 as const);
176
199
 
177
- expectType<typeof result, readonly [4, ...number[]]>('=');
200
+ expectType<
201
+ typeof result,
202
+ readonly [4, ...number[]] & NonEmptyArray<number>
203
+ >('=');
178
204
 
179
- assert.deepStrictEqual(result, [4, 1, 2, 3]);
205
+ expectType<typeof result, NonEmptyArray<number>>('<=');
206
+
207
+ assert.deepStrictEqual<readonly number[]>(result, [4, 1, 2, 3]);
180
208
  });
181
209
 
182
210
  test('should work with curried version', () => {
@@ -184,7 +212,9 @@ describe('Arr modifications', () => {
184
212
 
185
213
  const result = unshiftZero([1, 2, 3]);
186
214
 
187
- assert.deepStrictEqual(result, [0, 1, 2, 3]);
215
+ expectType<typeof result, NonEmptyArray<number>>('<=');
216
+
217
+ assert.deepStrictEqual<readonly number[]>(result, [0, 1, 2, 3]);
188
218
  });
189
219
  });
190
220