Debian Codegraph @

node/immutable/dist/immutable.d.ts

raw
1 /** @ignore we should disable this rules, but let's activate it to enable eslint first */
2 /**
3 * Immutable data encourages pure functions (data-in, data-out) and lends itself
4 * to much simpler application development and enabling techniques from
5 * functional programming such as lazy evaluation.
6 *
7 * While designed to bring these powerful functional concepts to JavaScript, it
8 * presents an Object-Oriented API familiar to Javascript engineers and closely
9 * mirroring that of Array, Map, and Set. It is easy and efficient to convert to
10 * and from plain Javascript types.
11 *
12 * ## How to read these docs
13 *
14 * In order to better explain what kinds of values the Immutable.js API expects
15 * and produces, this documentation is presented in a statically typed dialect of
16 * JavaScript (like [Flow][] or [TypeScript][]). You *don't need* to use these
17 * type checking tools in order to use Immutable.js, however becoming familiar
18 * with their syntax will help you get a deeper understanding of this API.
19 *
20 * **A few examples and how to read them.**
21 *
22 * All methods describe the kinds of data they accept and the kinds of data
23 * they return. For example a function which accepts two numbers and returns
24 * a number would look like this:
25 *
26 * ```js
27 * sum(first: number, second: number): number
28 * ```
29 *
30 * Sometimes, methods can accept different kinds of data or return different
31 * kinds of data, and this is described with a *type variable*, which is
32 * typically in all-caps. For example, a function which always returns the same
33 * kind of data it was provided would look like this:
34 *
35 * ```js
36 * identity<T>(value: T): T
37 * ```
38 *
39 * Type variables are defined with classes and referred to in methods. For
40 * example, a class that holds onto a value for you might look like this:
41 *
42 * ```js
43 * class Box<T> {
44 * constructor(value: T)
45 * getValue(): T
46 * }
47 * ```
48 *
49 * In order to manipulate Immutable data, methods that we're used to affecting
50 * a Collection instead return a new Collection of the same type. The type
51 * `this` refers to the same kind of class. For example, a List which returns
52 * new Lists when you `push` a value onto it might look like:
53 *
54 * ```js
55 * class List<T> {
56 * push(value: T): this
57 * }
58 * ```
59 *
60 * Many methods in Immutable.js accept values which implement the JavaScript
61 * [Iterable][] protocol, and might appear like `Iterable<string>` for something
62 * which represents sequence of strings. Typically in JavaScript we use plain
63 * Arrays (`[]`) when an Iterable is expected, but also all of the Immutable.js
64 * collections are iterable themselves!
65 *
66 * For example, to get a value deep within a structure of data, we might use
67 * `getIn` which expects an `Iterable` path:
68 *
69 * ```
70 * getIn(path: Iterable<string | number>): unknown
71 * ```
72 *
73 * To use this method, we could pass an array: `data.getIn([ "key", 2 ])`.
74 *
75 *
76 * Note: All examples are presented in the modern [ES2015][] version of
77 * JavaScript. Use tools like Babel to support older browsers.
78 *
79 * For example:
80 *
81 * ```js
82 * // ES2015
83 * const mappedFoo = foo.map(x => x * x);
84 * // ES5
85 * var mappedFoo = foo.map(function (x) { return x * x; });
86 * ```
87 *
88 * [ES2015]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript/ECMAScript_6_support_in_Mozilla
89 * [TypeScript]: https://www.typescriptlang.org/
90 * [Flow]: https://flowtype.org/
91 * [Iterable]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
92 */
93
94 declare namespace Immutable {
95 /** @ignore */
96 type OnlyObject<T> = Extract<T, object>;
97
98 /** @ignore */
99 type ContainObject<T> =
100 OnlyObject<T> extends object
101 ? OnlyObject<T> extends never
102 ? false
103 : true
104 : false;
105
106 /**
107 * @ignore
108 *
109 * Used to convert deeply all immutable types to a plain TS type.
110 * Using `unknown` on object instead of recursive call as we have a circular reference issue
111 */
112 export type DeepCopy<T> =
113 T extends Record<infer R>
114 ? // convert Record to DeepCopy plain JS object
115 {
116 [key in keyof R]: ContainObject<R[key]> extends true
117 ? unknown
118 : R[key];
119 }
120 : T extends MapOf<infer R>
121 ? // convert MapOf to DeepCopy plain JS object
122 {
123 [key in keyof R]: ContainObject<R[key]> extends true
124 ? unknown
125 : R[key];
126 }
127 : T extends Collection.Keyed<infer KeyedKey, infer V>
128 ? // convert KeyedCollection to DeepCopy plain JS object
129 {
130 [key in KeyedKey extends PropertyKey
131 ? KeyedKey
132 : string]: V extends object ? unknown : V;
133 }
134 : // convert IndexedCollection or Immutable.Set to DeepCopy plain JS array
135 // eslint-disable-next-line @typescript-eslint/no-unused-vars
136 T extends Collection<infer _, infer V>
137 ? Array<DeepCopy<V>>
138 : T extends string | number // Iterable scalar types : should be kept as is
139 ? T
140 : T extends Iterable<infer V> // Iterable are converted to plain JS array
141 ? Array<DeepCopy<V>>
142 : T extends object // plain JS object are converted deeply
143 ? {
144 [ObjectKey in keyof T]: ContainObject<
145 T[ObjectKey]
146 > extends true
147 ? unknown
148 : T[ObjectKey];
149 }
150 : // other case : should be kept as is
151 T;
152
153 /**
154 * Describes which item in a pair should be placed first when sorting
155 *
156 * @ignore
157 */
158 export enum PairSorting {
159 LeftThenRight = -1,
160 RightThenLeft = +1,
161 }
162
163 /**
164 * Function comparing two items of the same type. It can return:
165 *
166 * * a PairSorting value, to indicate whether the left-hand item or the right-hand item should be placed before the other
167 *
168 * * the traditional numeric return value - especially -1, 0, or 1
169 *
170 * @ignore
171 */
172 export type Comparator<T> = (left: T, right: T) => PairSorting | number;
173
174 /**
175 * @ignore
176 *
177 * KeyPath allowed for `xxxIn` methods
178 */
179 export type KeyPath<K> = OrderedCollection<K> | ArrayLike<K>;
180
181 /**
182 * Lists are ordered indexed dense collections, much like a JavaScript
183 * Array.
184 *
185 * Lists are immutable and fully persistent with O(log32 N) gets and sets,
186 * and O(1) push and pop.
187 *
188 * Lists implement Deque, with efficient addition and removal from both the
189 * end (`push`, `pop`) and beginning (`unshift`, `shift`).
190 *
191 * Unlike a JavaScript Array, there is no distinction between an
192 * "unset" index and an index set to `undefined`. `List#forEach` visits all
193 * indices from 0 to size, regardless of whether they were explicitly defined.
194 */
195 namespace List {
196 /**
197 * True if the provided value is a List
198 */
199 function isList(maybeList: unknown): maybeList is List<unknown>;
200
201 /**
202 * Creates a new List containing `values`.
203 *
204 * Note: Values are not altered or converted in any way.
205 */
206 function of<T>(...values: Array<T>): List<T>;
207 }
208
209 /**
210 * Create a new immutable List containing the values of the provided
211 * collection-like.
212 *
213 * Note: `List` is a factory function and not a class, and does not use the
214 * `new` keyword during construction.
215 */
216 function List<T>(collection?: Iterable<T> | ArrayLike<T>): List<T>;
217
218 interface List<T> extends Collection.Indexed<T> {
219 /**
220 * The number of items in this List.
221 */
222 readonly size: number;
223
224 // Persistent changes
225
226 /**
227 * Returns a new List which includes `value` at `index`. If `index` already
228 * exists in this List, it will be replaced.
229 *
230 * `index` may be a negative number, which indexes back from the end of the
231 * List. `v.set(-1, "value")` sets the last item in the List.
232 *
233 * If `index` larger than `size`, the returned List's `size` will be large
234 * enough to include the `index`.
235 *
236 * Note: `set` can be used in `withMutations`.
237 */
238 set(index: number, value: T): List<T>;
239
240 /**
241 * Returns a new List which excludes this `index` and with a size 1 less
242 * than this List. Values at indices above `index` are shifted down by 1 to
243 * fill the position.
244 *
245 * This is synonymous with `list.splice(index, 1)`.
246 *
247 * `index` may be a negative number, which indexes back from the end of the
248 * List. `v.delete(-1)` deletes the last item in the List.
249 *
250 * Note: `delete` cannot be safely used in IE8
251 *
252 * Since `delete()` re-indexes values, it produces a complete copy, which
253 * has `O(N)` complexity.
254 *
255 * Note: `delete` *cannot* be used in `withMutations`.
256 *
257 * @alias remove
258 */
259 delete(index: number): List<T>;
260 remove(index: number): List<T>;
261
262 /**
263 * Returns a new List with `value` at `index` with a size 1 more than this
264 * List. Values at indices above `index` are shifted over by 1.
265 *
266 * This is synonymous with `list.splice(index, 0, value)`.
267 *
268 * Since `insert()` re-indexes values, it produces a complete copy, which
269 * has `O(N)` complexity.
270 *
271 * Note: `insert` *cannot* be used in `withMutations`.
272 */
273 insert(index: number, value: T): List<T>;
274
275 /**
276 * Returns a new List with 0 size and no values in constant time.
277 *
278 * Note: `clear` can be used in `withMutations`.
279 */
280 clear(): List<T>;
281
282 /**
283 * Returns a new List with the provided `values` appended, starting at this
284 * List's `size`.
285 *
286 * Note: `push` can be used in `withMutations`.
287 */
288 push(...values: Array<T>): List<T>;
289
290 /**
291 * Returns a new List with a size ones less than this List, excluding
292 * the last index in this List.
293 *
294 * Note: this differs from `Array#pop` because it returns a new
295 * List rather than the removed value. Use `last()` to get the last value
296 * in this List.
297 *
298 * ```js
299 * List([ 1, 2, 3, 4 ]).pop()
300 * // List[ 1, 2, 3 ]
301 * ```
302 *
303 * Note: `pop` can be used in `withMutations`.
304 */
305 pop(): List<T>;
306
307 /**
308 * Returns a new List with the provided `values` prepended, shifting other
309 * values ahead to higher indices.
310 *
311 * Note: `unshift` can be used in `withMutations`.
312 */
313 unshift(...values: Array<T>): List<T>;
314
315 /**
316 * Returns a new List with a size ones less than this List, excluding
317 * the first index in this List, shifting all other values to a lower index.
318 *
319 * Note: this differs from `Array#shift` because it returns a new
320 * List rather than the removed value. Use `first()` to get the first
321 * value in this List.
322 *
323 * Note: `shift` can be used in `withMutations`.
324 */
325 shift(): List<T>;
326
327 /**
328 * Returns a new List with an updated value at `index` with the return
329 * value of calling `updater` with the existing value, or `notSetValue` if
330 * `index` was not set. If called with a single argument, `updater` is
331 * called with the List itself.
332 *
333 * `index` may be a negative number, which indexes back from the end of the
334 * List. `v.update(-1)` updates the last item in the List.
335 *
336 * This can be very useful as a way to "chain" a normal function into a
337 * sequence of methods. RxJS calls this "let" and lodash calls it "thru".
338 *
339 * For example, to sum a List after mapping and filtering:
340 *
341 * Note: `update(index)` can be used in `withMutations`.
342 *
343 * @see `Map#update`
344 */
345 update(index: number, notSetValue: T, updater: (value: T) => T): this;
346 update(
347 index: number,
348 updater: (value: T | undefined) => T | undefined
349 ): this;
350 update<R>(updater: (value: this) => R): R;
351
352 /**
353 * Returns a new List with size `size`. If `size` is less than this
354 * List's size, the new List will exclude values at the higher indices.
355 * If `size` is greater than this List's size, the new List will have
356 * undefined values for the newly available indices.
357 *
358 * When building a new List and the final size is known up front, `setSize`
359 * used in conjunction with `withMutations` may result in the more
360 * performant construction.
361 */
362 setSize(size: number): List<T>;
363
364 // Deep persistent changes
365
366 /**
367 * Returns a new List having set `value` at this `keyPath`. If any keys in
368 * `keyPath` do not exist, a new immutable Map will be created at that key.
369 *
370 * Index numbers are used as keys to determine the path to follow in
371 * the List.
372 *
373 * Plain JavaScript Object or Arrays may be nested within an Immutable.js
374 * Collection, and setIn() can update those values as well, treating them
375 * immutably by creating new copies of those values with the changes applied.
376 *
377 * Note: `setIn` can be used in `withMutations`.
378 */
379 setIn(keyPath: Iterable<unknown>, value: unknown): this;
380
381 /**
382 * Returns a new List having removed the value at this `keyPath`. If any
383 * keys in `keyPath` do not exist, no change will occur.
384 *
385 * Plain JavaScript Object or Arrays may be nested within an Immutable.js
386 * Collection, and removeIn() can update those values as well, treating them
387 * immutably by creating new copies of those values with the changes applied.
388 *
389 * Note: `deleteIn` *cannot* be safely used in `withMutations`.
390 *
391 * @alias removeIn
392 */
393 deleteIn(keyPath: Iterable<unknown>): this;
394 removeIn(keyPath: Iterable<unknown>): this;
395
396 /**
397 * Note: `updateIn` can be used in `withMutations`.
398 *
399 * @see `Map#updateIn`
400 */
401 updateIn(
402 keyPath: Iterable<unknown>,
403 notSetValue: unknown,
404 updater: (value: unknown) => unknown
405 ): this;
406 updateIn(
407 keyPath: Iterable<unknown>,
408 updater: (value: unknown) => unknown
409 ): this;
410
411 /**
412 * Note: `mergeIn` can be used in `withMutations`.
413 *
414 * @see `Map#mergeIn`
415 */
416 mergeIn(keyPath: Iterable<unknown>, ...collections: Array<unknown>): this;
417
418 /**
419 * Note: `mergeDeepIn` can be used in `withMutations`.
420 *
421 * @see `Map#mergeDeepIn`
422 */
423 mergeDeepIn(
424 keyPath: Iterable<unknown>,
425 ...collections: Array<unknown>
426 ): this;
427
428 // Transient changes
429
430 /**
431 * Note: Not all methods can be safely used on a mutable collection or within
432 * `withMutations`! Check the documentation for each method to see if it
433 * allows being used in `withMutations`.
434 *
435 * @see `Map#withMutations`
436 */
437 withMutations(mutator: (mutable: this) => unknown): this;
438
439 /**
440 * An alternative API for withMutations()
441 *
442 * Note: Not all methods can be safely used on a mutable collection or within
443 * `withMutations`! Check the documentation for each method to see if it
444 * allows being used in `withMutations`.
445 *
446 * @see `Map#asMutable`
447 */
448 asMutable(): this;
449
450 /**
451 * @see `Map#wasAltered`
452 */
453 wasAltered(): boolean;
454
455 /**
456 * @see `Map#asImmutable`
457 */
458 asImmutable(): this;
459
460 // Sequence algorithms
461
462 /**
463 * Returns a new List with other values or collections concatenated to this one.
464 *
465 * Note: `concat` can be used in `withMutations`.
466 *
467 * @alias merge
468 */
469 concat<C>(...valuesOrCollections: Array<Iterable<C> | C>): List<T | C>;
470 merge<C>(...collections: Array<Iterable<C>>): List<T | C>;
471
472 /**
473 * Returns a new List with values passed through a
474 * `mapper` function.
475 */
476 map<M>(
477 mapper: (value: T, key: number, iter: this) => M,
478 context?: unknown
479 ): List<M>;
480
481 /**
482 * Flat-maps the List, returning a new List.
483 *
484 * Similar to `list.map(...).flatten(true)`.
485 */
486 flatMap<M>(
487 mapper: (value: T, key: number, iter: this) => Iterable<M>,
488 context?: unknown
489 ): List<M>;
490
491 /**
492 * Returns a new List with only the values for which the `predicate`
493 * function returns true.
494 *
495 * Note: `filter()` always returns a new instance, even if it results in
496 * not filtering out any values.
497 */
498 filter<F extends T>(
499 predicate: (value: T, index: number, iter: this) => value is F,
500 context?: unknown
501 ): List<F>;
502 filter(
503 predicate: (value: T, index: number, iter: this) => unknown,
504 context?: unknown
505 ): this;
506
507 /**
508 * Returns a new List with the values for which the `predicate`
509 * function returns false and another for which is returns true.
510 */
511 partition<F extends T, C>(
512 predicate: (this: C, value: T, index: number, iter: this) => value is F,
513 context?: C
514 ): [List<T>, List<F>];
515 partition<C>(
516 predicate: (this: C, value: T, index: number, iter: this) => unknown,
517 context?: C
518 ): [this, this];
519
520 /**
521 * Returns a List "zipped" with the provided collection.
522 *
523 * Like `zipWith`, but using the default `zipper`: creating an `Array`.
524 */
525 zip<U>(other: Collection<unknown, U>): List<[T, U]>;
526 zip<U, V>(
527 other: Collection<unknown, U>,
528 other2: Collection<unknown, V>
529 ): List<[T, U, V]>;
530 zip(...collections: Array<Collection<unknown, unknown>>): List<unknown>;
531
532 /**
533 * Returns a List "zipped" with the provided collections.
534 *
535 * Unlike `zip`, `zipAll` continues zipping until the longest collection is
536 * exhausted. Missing values from shorter collections are filled with `undefined`.
537 *
538 * Note: Since zipAll will return a collection as large as the largest
539 * input, some results may contain undefined values. TypeScript cannot
540 * account for these without cases (as of v2.5).
541 */
542 zipAll<U>(other: Collection<unknown, U>): List<[T, U]>;
543 zipAll<U, V>(
544 other: Collection<unknown, U>,
545 other2: Collection<unknown, V>
546 ): List<[T, U, V]>;
547 zipAll(...collections: Array<Collection<unknown, unknown>>): List<unknown>;
548
549 /**
550 * Returns a List "zipped" with the provided collections by using a
551 * custom `zipper` function.
552 */
553 zipWith<U, Z>(
554 zipper: (value: T, otherValue: U) => Z,
555 otherCollection: Collection<unknown, U>
556 ): List<Z>;
557 zipWith<U, V, Z>(
558 zipper: (value: T, otherValue: U, thirdValue: V) => Z,
559 otherCollection: Collection<unknown, U>,
560 thirdCollection: Collection<unknown, V>
561 ): List<Z>;
562 zipWith<Z>(
563 zipper: (...values: Array<unknown>) => Z,
564 ...collections: Array<Collection<unknown, unknown>>
565 ): List<Z>;
566
567 /**
568 * Returns a new List with its values shuffled thanks to the
569 * [Fisher–Yates](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle)
570 * algorithm.
571 * It uses Math.random, but you can provide your own random number generator.
572 */
573 shuffle(random?: () => number): this;
574 }
575
576 /**
577 * Immutable Map is an unordered Collection.Keyed of (key, value) pairs with
578 * `O(log32 N)` gets and `O(log32 N)` persistent sets.
579 *
580 * Iteration order of a Map is undefined, however is stable. Multiple
581 * iterations of the same Map will iterate in the same order.
582 *
583 * Map's keys can be of any type, and use `Immutable.is` to determine key
584 * equality. This allows the use of any value (including NaN) as a key.
585 *
586 * Because `Immutable.is` returns equality based on value semantics, and
587 * Immutable collections are treated as values, any Immutable collection may
588 * be used as a key.
589 *
590 * Any JavaScript object may be used as a key, however strict identity is used
591 * to evaluate key equality. Two similar looking objects will represent two
592 * different keys.
593 *
594 * Implemented by a hash-array mapped trie.
595 */
596 namespace Map {
597 /**
598 * True if the provided value is a Map
599 */
600 function isMap(maybeMap: unknown): maybeMap is Map<unknown, unknown>;
601 }
602
603 /**
604 * Creates a new Immutable Map.
605 *
606 * Created with the same key value pairs as the provided Collection.Keyed or
607 * JavaScript Object or expects a Collection of [K, V] tuple entries.
608 *
609 * Note: `Map` is a factory function and not a class, and does not use the
610 * `new` keyword during construction.
611 *
612 * Keep in mind, when using JS objects to construct Immutable Maps, that
613 * JavaScript Object properties are always strings, even if written in a
614 * quote-less shorthand, while Immutable Maps accept keys of any type.
615 *
616 * Property access for JavaScript Objects first converts the key to a string,
617 * but since Immutable Map keys can be of any type the argument to `get()` is
618 * not altered.
619 */
620 function Map<K, V>(collection?: Iterable<readonly [K, V]>): Map<K, V>;
621 function Map<R extends { [key in PropertyKey]: unknown }>(obj: R): MapOf<R>;
622 function Map<V>(obj: { [key: string]: V }): Map<string, V>;
623 function Map<K extends string | symbol, V>(obj: { [P in K]?: V }): Map<K, V>;
624
625 /**
626 * Represent a Map constructed by an object
627 *
628 * @ignore
629 */
630 interface MapOf<R extends { [key in PropertyKey]: unknown }>
631 extends Map<keyof R, R[keyof R]> {
632 /**
633 * Returns the value associated with the provided key, or notSetValue if
634 * the Collection does not contain this key.
635 *
636 * Note: it is possible a key may be associated with an `undefined` value,
637 * so if `notSetValue` is not provided and this method returns `undefined`,
638 * that does not guarantee the key was not found.
639 */
640 get<K extends keyof R>(key: K, notSetValue?: unknown): R[K];
641 get<NSV>(key: unknown, notSetValue: NSV): NSV;
642
643 // TODO `<const P extends ...>` can be used after dropping support for TypeScript 4.x
644 // reference: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-0.html#const-type-parameters
645 // after this change, `as const` assertions can be remove from the type tests
646 getIn<P extends ReadonlyArray<PropertyKey>>(
647 searchKeyPath: [...P],
648 notSetValue?: unknown
649 ): RetrievePath<R, P>;
650
651 set<K extends keyof R>(key: K, value: R[K]): this;
652
653 update(updater: (value: this) => this): this;
654 update<K extends keyof R>(key: K, updater: (value: R[K]) => R[K]): this;
655 update<K extends keyof R, NSV extends R[K]>(
656 key: K,
657 notSetValue: NSV,
658 updater: (value: R[K]) => R[K]
659 ): this;
660
661 // Possible best type is MapOf<Omit<R, K>> but Omit seems to broke other function calls
662 // and generate recursion error with other methods (update, merge, etc.) until those functions are defined in MapOf
663 delete<K extends keyof R>(
664 key: K
665 ): Extract<R[K], undefined> extends never ? never : this;
666 remove<K extends keyof R>(
667 key: K
668 ): Extract<R[K], undefined> extends never ? never : this;
669
670 toJS(): { [K in keyof R]: DeepCopy<R[K]> };
671
672 toJSON(): { [K in keyof R]: R[K] };
673 }
674
675 // Loosely based off of this work.
676 // https://github.com/immutable-js/immutable-js/issues/1462#issuecomment-584123268
677
678 /**
679 * @ignore
680 * Convert an immutable type to the equivalent plain TS type
681 * - MapOf -> object
682 * - List -> Array
683 */
684 type GetNativeType<S> =
685 S extends MapOf<infer T> ? T : S extends List<infer I> ? Array<I> : S;
686
687 /** @ignore */
688 type Head<T extends ReadonlyArray<unknown>> = T extends [
689 infer H,
690 ...Array<unknown>,
691 ]
692 ? H
693 : never;
694 /** @ignore */
695 type Tail<T extends ReadonlyArray<unknown>> = T extends [unknown, ...infer I]
696 ? I
697 : Array<never>;
698 /** @ignore */
699 type RetrievePathReducer<
700 T,
701 C,
702 L extends ReadonlyArray<unknown>,
703 NT = GetNativeType<T>,
704 > =
705 // we can not retrieve a path from a primitive type
706 T extends string | number | boolean | null | undefined
707 ? never
708 : C extends keyof NT
709 ? L extends [] // L extends [] means we are at the end of the path, lets return the current type
710 ? NT[C]
711 : // we are not at the end of the path, lets continue with the next key
712 RetrievePathReducer<NT[C], Head<L>, Tail<L>>
713 : // C is not a "key" of NT, so the path is invalid
714 never;
715
716 /** @ignore */
717 type RetrievePath<R, P extends ReadonlyArray<unknown>> = P extends []
718 ? P
719 : RetrievePathReducer<R, Head<P>, Tail<P>>;
720
721 interface Map<K, V> extends Collection.Keyed<K, V> {
722 /**
723 * The number of entries in this Map.
724 */
725 readonly size: number;
726
727 // Persistent changes
728
729 /**
730 * Returns a new Map also containing the new key, value pair. If an equivalent
731 * key already exists in this Map, it will be replaced.
732 *
733 * Note: `set` can be used in `withMutations`.
734 */
735 set(key: K, value: V): this;
736
737 /**
738 * Returns a new Map which excludes this `key`.
739 *
740 * Note: `delete` cannot be safely used in IE8, but is provided to mirror
741 * the ES6 collection API.
742 *
743 * Note: `delete` can be used in `withMutations`.
744 *
745 * @alias remove
746 */
747 delete(key: K): this;
748 remove(key: K): this;
749
750 /**
751 * Returns a new Map which excludes the provided `keys`.
752 *
753 * Note: `deleteAll` can be used in `withMutations`.
754 *
755 * @alias removeAll
756 */
757 deleteAll(keys: Iterable<K>): this;
758 removeAll(keys: Iterable<K>): this;
759
760 /**
761 * Returns a new Map containing no keys or values.
762 *
763 * Note: `clear` can be used in `withMutations`.
764 */
765 clear(): this;
766
767 /**
768 * Returns a new Map having updated the value at this `key` with the return
769 * value of calling `updater` with the existing value.
770 *
771 * Similar to: `map.set(key, updater(map.get(key)))`.
772 *
773 * This is most commonly used to call methods on collections within a
774 * structure of data. For example, in order to `.push()` onto a nested `List`,
775 * `update` and `push` can be used together:
776 *
777 * When a `notSetValue` is provided, it is provided to the `updater`
778 * function when the value at the key does not exist in the Map.
779 *
780 * However, if the `updater` function returns the same value it was called
781 * with, then no change will occur. This is still true if `notSetValue`
782 * is provided.
783 *
784 * For code using ES2015 or later, using `notSetValue` is discourged in
785 * favor of function parameter default values. This helps to avoid any
786 * potential confusion with identify functions as described above.
787 *
788 * The previous example behaves differently when written with default values:
789 *
790 * If no key is provided, then the `updater` function return value is
791 * returned as well.
792 *
793 * This can be very useful as a way to "chain" a normal function into a
794 * sequence of methods. RxJS calls this "let" and lodash calls it "thru".
795 *
796 * For example, to sum the values in a Map
797 *
798 * Note: `update(key)` can be used in `withMutations`.
799 */
800 update(key: K, notSetValue: V, updater: (value: V) => V): this;
801 update(key: K, updater: (value: V | undefined) => V | undefined): this;
802 update<R>(updater: (value: this) => R): R;
803
804 /**
805 * Returns a new Map resulting from merging the provided Collections
806 * (or JS objects) into this Map. In other words, this takes each entry of
807 * each collection and sets it on this Map.
808 *
809 * Note: Values provided to `merge` are shallowly converted before being
810 * merged. No nested values are altered.
811 * ```
812 *
813 * Note: `merge` can be used in `withMutations`.
814 *
815 * @alias concat
816 */
817 merge<KC, VC>(
818 ...collections: Array<Iterable<[KC, VC]>>
819 ): Map<K | KC, Exclude<V, VC> | VC>;
820 merge<C>(
821 ...collections: Array<{ [key: string]: C }>
822 ): Map<K | string, Exclude<V, C> | C>;
823
824 concat<KC, VC>(
825 ...collections: Array<Iterable<[KC, VC]>>
826 ): Map<K | KC, Exclude<V, VC> | VC>;
827 concat<C>(
828 ...collections: Array<{ [key: string]: C }>
829 ): Map<K | string, Exclude<V, C> | C>;
830
831 /**
832 * Like `merge()`, `mergeWith()` returns a new Map resulting from merging
833 * the provided Collections (or JS objects) into this Map, but uses the
834 * `merger` function for dealing with conflicts.
835 *
836 * Note: `mergeWith` can be used in `withMutations`.
837 */
838 mergeWith<KC, VC, VCC>(
839 merger: (oldVal: V, newVal: VC, key: K) => VCC,
840 ...collections: Array<Iterable<[KC, VC]>>
841 ): Map<K | KC, V | VC | VCC>;
842 mergeWith<C, CC>(
843 merger: (oldVal: V, newVal: C, key: string) => CC,
844 ...collections: Array<{ [key: string]: C }>
845 ): Map<K | string, V | C | CC>;
846
847 /**
848 * Like `merge()`, but when two compatible collections are encountered with
849 * the same key, it merges them as well, recursing deeply through the nested
850 * data. Two collections are considered to be compatible (and thus will be
851 * merged together) if they both fall into one of three categories: keyed
852 * (e.g., `Map`s, `Record`s, and objects), indexed (e.g., `List`s and
853 * arrays), or set-like (e.g., `Set`s). If they fall into separate
854 * categories, `mergeDeep` will replace the existing collection with the
855 * collection being merged in. This behavior can be customized by using
856 * `mergeDeepWith()`.
857 *
858 * Note: Indexed and set-like collections are merged using
859 * `concat()`/`union()` and therefore do not recurse.
860 *
861 * Note: `mergeDeep` can be used in `withMutations`.
862 */
863 mergeDeep<KC, VC>(
864 ...collections: Array<Iterable<[KC, VC]>>
865 ): Map<K | KC, V | VC>;
866 mergeDeep<C>(
867 ...collections: Array<{ [key: string]: C }>
868 ): Map<K | string, V | C>;
869
870 /**
871 * Like `mergeDeep()`, but when two non-collections or incompatible
872 * collections are encountered at the same key, it uses the `merger`
873 * function to determine the resulting value. Collections are considered
874 * incompatible if they fall into separate categories between keyed,
875 * indexed, and set-like.
876 *
877 * Note: `mergeDeepWith` can be used in `withMutations`.
878 */
879 mergeDeepWith(
880 merger: (oldVal: unknown, newVal: unknown, key: unknown) => unknown,
881 ...collections: Array<Iterable<[K, V]> | { [key: string]: V }>
882 ): this;
883
884 // Deep persistent changes
885
886 /**
887 * Returns a new Map having set `value` at this `keyPath`. If any keys in
888 * `keyPath` do not exist, a new immutable Map will be created at that key.
889 *
890 * Plain JavaScript Object or Arrays may be nested within an Immutable.js
891 * Collection, and setIn() can update those values as well, treating them
892 * immutably by creating new copies of those values with the changes applied.
893 *
894 * If any key in the path exists but cannot be updated (such as a primitive
895 * like number or a custom Object like Date), an error will be thrown.
896 *
897 * Note: `setIn` can be used in `withMutations`.
898 */
899 setIn(keyPath: Iterable<unknown>, value: unknown): this;
900
901 /**
902 * Returns a new Map having removed the value at this `keyPath`. If any keys
903 * in `keyPath` do not exist, no change will occur.
904 *
905 * Note: `deleteIn` can be used in `withMutations`.
906 *
907 * @alias removeIn
908 */
909 deleteIn(keyPath: Iterable<unknown>): this;
910 removeIn(keyPath: Iterable<unknown>): this;
911
912 /**
913 * Returns a new Map having applied the `updater` to the entry found at the
914 * keyPath.
915 *
916 * This is most commonly used to call methods on collections nested within a
917 * structure of data. For example, in order to `.push()` onto a nested `List`,
918 * `updateIn` and `push` can be used together:
919
920 *
921 * If any keys in `keyPath` do not exist, new Immutable `Map`s will
922 * be created at those keys. If the `keyPath` does not already contain a
923 * value, the `updater` function will be called with `notSetValue`, if
924 * provided, otherwise `undefined`.
925 *
926 * If the `updater` function returns the same value it was called with, then
927 * no change will occur. This is still true if `notSetValue` is provided.
928 *
929 * For code using ES2015 or later, using `notSetValue` is discourged in
930 * favor of function parameter default values. This helps to avoid any
931 * potential confusion with identify functions as described above.
932 *
933 * The previous example behaves differently when written with default values:
934 *
935 * Plain JavaScript Object or Arrays may be nested within an Immutable.js
936 * Collection, and updateIn() can update those values as well, treating them
937 * immutably by creating new copies of those values with the changes applied.
938 *
939 * If any key in the path exists but cannot be updated (such as a primitive
940 * like number or a custom Object like Date), an error will be thrown.
941 *
942 * Note: `updateIn` can be used in `withMutations`.
943 */
944 updateIn(
945 keyPath: Iterable<unknown>,
946 notSetValue: unknown,
947 updater: (value: unknown) => unknown
948 ): this;
949 updateIn(
950 keyPath: Iterable<unknown>,
951 updater: (value: unknown) => unknown
952 ): this;
953
954 /**
955 * A combination of `updateIn` and `merge`, returning a new Map, but
956 * performing the merge at a point arrived at by following the keyPath.
957 * In other words, these two lines are equivalent:
958 *
959 * ```js
960 * map.updateIn(['a', 'b', 'c'], abc => abc.merge(y))
961 * map.mergeIn(['a', 'b', 'c'], y)
962 * ```
963 *
964 * Note: `mergeIn` can be used in `withMutations`.
965 */
966 mergeIn(keyPath: Iterable<unknown>, ...collections: Array<unknown>): this;
967
968 /**
969 * A combination of `updateIn` and `mergeDeep`, returning a new Map, but
970 * performing the deep merge at a point arrived at by following the keyPath.
971 * In other words, these two lines are equivalent:
972 *
973 * ```js
974 * map.updateIn(['a', 'b', 'c'], abc => abc.mergeDeep(y))
975 * map.mergeDeepIn(['a', 'b', 'c'], y)
976 * ```
977 *
978 * Note: `mergeDeepIn` can be used in `withMutations`.
979 */
980 mergeDeepIn(
981 keyPath: Iterable<unknown>,
982 ...collections: Array<unknown>
983 ): this;
984
985 // Transient changes
986
987 /**
988 * Every time you call one of the above functions, a new immutable Map is
989 * created. If a pure function calls a number of these to produce a final
990 * return value, then a penalty on performance and memory has been paid by
991 * creating all of the intermediate immutable Maps.
992 *
993 * If you need to apply a series of mutations to produce a new immutable
994 * Map, `withMutations()` creates a temporary mutable copy of the Map which
995 * can apply mutations in a highly performant manner. In fact, this is
996 * exactly how complex mutations like `merge` are done.
997 *
998 * As an example, this results in the creation of 2, not 4, new Maps:
999 *
1000 * Note: Not all methods can be used on a mutable collection or within
1001 * `withMutations`! Read the documentation for each method to see if it
1002 * is safe to use in `withMutations`.
1003 */
1004 withMutations(mutator: (mutable: this) => unknown): this;
1005
1006 /**
1007 * Another way to avoid creation of intermediate Immutable maps is to create
1008 * a mutable copy of this collection. Mutable copies *always* return `this`,
1009 * and thus shouldn't be used for equality. Your function should never return
1010 * a mutable copy of a collection, only use it internally to create a new
1011 * collection.
1012 *
1013 * If possible, use `withMutations` to work with temporary mutable copies as
1014 * it provides an easier to use API and considers many common optimizations.
1015 *
1016 * Note: if the collection is already mutable, `asMutable` returns itself.
1017 *
1018 * Note: Not all methods can be used on a mutable collection or within
1019 * `withMutations`! Read the documentation for each method to see if it
1020 * is safe to use in `withMutations`.
1021 *
1022 * @see `Map#asImmutable`
1023 */
1024 asMutable(): this;
1025
1026 /**
1027 * Returns true if this is a mutable copy (see `asMutable()`) and mutative
1028 * alterations have been applied.
1029 *
1030 * @see `Map#asMutable`
1031 */
1032 wasAltered(): boolean;
1033
1034 /**
1035 * The yin to `asMutable`'s yang. Because it applies to mutable collections,
1036 * this operation is *mutable* and may return itself (though may not
1037 * return itself, i.e. if the result is an empty collection). Once
1038 * performed, the original mutable copy must no longer be mutated since it
1039 * may be the immutable result.
1040 *
1041 * If possible, use `withMutations` to work with temporary mutable copies as
1042 * it provides an easier to use API and considers many common optimizations.
1043 *
1044 * @see `Map#asMutable`
1045 */
1046 asImmutable(): this;
1047
1048 // Sequence algorithms
1049
1050 /**
1051 * Returns a new Map with values passed through a
1052 * `mapper` function.
1053 *
1054 * Map({ a: 1, b: 2 }).map(x => 10 * x)
1055 * // Map { a: 10, b: 20 }
1056 */
1057 map<M>(
1058 mapper: (value: V, key: K, iter: this) => M,
1059 context?: unknown
1060 ): Map<K, M>;
1061
1062 /**
1063 * @see Collection.Keyed.mapKeys
1064 */
1065 mapKeys<M>(
1066 mapper: (key: K, value: V, iter: this) => M,
1067 context?: unknown
1068 ): Map<M, V>;
1069
1070 /**
1071 * @see Collection.Keyed.mapEntries
1072 */
1073 mapEntries<KM, VM>(
1074 mapper: (
1075 entry: [K, V],
1076 index: number,
1077 iter: this
1078 ) => [KM, VM] | undefined,
1079 context?: unknown
1080 ): Map<KM, VM>;
1081
1082 /**
1083 * Flat-maps the Map, returning a new Map.
1084 *
1085 * Similar to `data.map(...).flatten(true)`.
1086 */
1087 flatMap<KM, VM>(
1088 mapper: (value: V, key: K, iter: this) => Iterable<[KM, VM]>,
1089 context?: unknown
1090 ): Map<KM, VM>;
1091
1092 /**
1093 * Returns a new Map with only the entries for which the `predicate`
1094 * function returns true.
1095 *
1096 * Note: `filter()` always returns a new instance, even if it results in
1097 * not filtering out any values.
1098 */
1099 filter<F extends V>(
1100 predicate: (value: V, key: K, iter: this) => value is F,
1101 context?: unknown
1102 ): Map<K, F>;
1103 filter(
1104 predicate: (value: V, key: K, iter: this) => unknown,
1105 context?: unknown
1106 ): this;
1107
1108 /**
1109 * Returns a new Map with the values for which the `predicate`
1110 * function returns false and another for which is returns true.
1111 */
1112 partition<F extends V, C>(
1113 predicate: (this: C, value: V, key: K, iter: this) => value is F,
1114 context?: C
1115 ): [Map<K, V>, Map<K, F>];
1116 partition<C>(
1117 predicate: (this: C, value: V, key: K, iter: this) => unknown,
1118 context?: C
1119 ): [this, this];
1120
1121 /**
1122 * @see Collection.Keyed.flip
1123 */
1124 flip(): Map<V, K>;
1125
1126 /**
1127 * Returns an OrderedMap of the same type which includes the same entries,
1128 * stably sorted by using a `comparator`.
1129 *
1130 * If a `comparator` is not provided, a default comparator uses `<` and `>`.
1131 *
1132 * `comparator(valueA, valueB)`:
1133 *
1134 * * Returns `0` if the elements should not be swapped.
1135 * * Returns `-1` (or any negative number) if `valueA` comes before `valueB`
1136 * * Returns `1` (or any positive number) if `valueA` comes after `valueB`
1137 * * Alternatively, can return a value of the `PairSorting` enum type
1138 * * Is pure, i.e. it must always return the same value for the same pair
1139 * of values.
1140 *
1141 * Note: `sort()` Always returns a new instance, even if the original was
1142 * already sorted.
1143 *
1144 * Note: This is always an eager operation.
1145 */
1146 sort(comparator?: Comparator<V>): this & OrderedMap<K, V>;
1147
1148 /**
1149 * Like `sort`, but also accepts a `comparatorValueMapper` which allows for
1150 * sorting by more sophisticated means:
1151 *
1152 * Note: `sortBy()` Always returns a new instance, even if the original was
1153 * already sorted.
1154 *
1155 * Note: This is always an eager operation.
1156 */
1157 sortBy<C>(
1158 comparatorValueMapper: (value: V, key: K, iter: this) => C,
1159 comparator?: (valueA: C, valueB: C) => number
1160 ): this & OrderedMap<K, V>;
1161 }
1162
1163 /**
1164 * A type of Map that has the additional guarantee that the iteration order of
1165 * entries will be the order in which they were set().
1166 *
1167 * The iteration behavior of OrderedMap is the same as native ES6 Map and
1168 * JavaScript Object.
1169 *
1170 * Note that `OrderedMap` are more expensive than non-ordered `Map` and may
1171 * consume more memory. `OrderedMap#set` is amortized O(log32 N), but not
1172 * stable.
1173 */
1174 namespace OrderedMap {
1175 /**
1176 * True if the provided value is an OrderedMap.
1177 */
1178 function isOrderedMap(
1179 maybeOrderedMap: unknown
1180 ): maybeOrderedMap is OrderedMap<unknown, unknown>;
1181 }
1182
1183 /**
1184 * Creates a new Immutable OrderedMap.
1185 *
1186 * Created with the same key value pairs as the provided Collection.Keyed or
1187 * JavaScript Object or expects a Collection of [K, V] tuple entries.
1188 *
1189 * The iteration order of key-value pairs provided to this constructor will
1190 * be preserved in the OrderedMap.
1191 *
1192 * let newOrderedMap = OrderedMap({key: "value"})
1193 * let newOrderedMap = OrderedMap([["key", "value"]])
1194 *
1195 * Note: `OrderedMap` is a factory function and not a class, and does not use
1196 * the `new` keyword during construction.
1197 */
1198 function OrderedMap<K, V>(collection?: Iterable<[K, V]>): OrderedMap<K, V>;
1199 function OrderedMap<V>(obj: { [key: string]: V }): OrderedMap<string, V>;
1200
1201 interface OrderedMap<K, V> extends Map<K, V>, OrderedCollection<[K, V]> {
1202 /**
1203 * The number of entries in this OrderedMap.
1204 */
1205 readonly size: number;
1206
1207 /**
1208 * Returns a new OrderedMap also containing the new key, value pair. If an
1209 * equivalent key already exists in this OrderedMap, it will be replaced
1210 * while maintaining the existing order.
1211 *
1212 * Note: `set` can be used in `withMutations`.
1213 */
1214 set(key: K, value: V): this;
1215
1216 /**
1217 * Returns a new OrderedMap resulting from merging the provided Collections
1218 * (or JS objects) into this OrderedMap. In other words, this takes each
1219 * entry of each collection and sets it on this OrderedMap.
1220 *
1221 * Note: Values provided to `merge` are shallowly converted before being
1222 * merged. No nested values are altered.
1223 *
1224 * Note: `merge` can be used in `withMutations`.
1225 *
1226 * @alias concat
1227 */
1228 merge<KC, VC>(
1229 ...collections: Array<Iterable<[KC, VC]>>
1230 ): OrderedMap<K | KC, Exclude<V, VC> | VC>;
1231 merge<C>(
1232 ...collections: Array<{ [key: string]: C }>
1233 ): OrderedMap<K | string, Exclude<V, C> | C>;
1234
1235 concat<KC, VC>(
1236 ...collections: Array<Iterable<[KC, VC]>>
1237 ): OrderedMap<K | KC, Exclude<V, VC> | VC>;
1238 concat<C>(
1239 ...collections: Array<{ [key: string]: C }>
1240 ): OrderedMap<K | string, Exclude<V, C> | C>;
1241
1242 mergeWith<KC, VC, VCC>(
1243 merger: (oldVal: V, newVal: VC, key: K) => VCC,
1244 ...collections: Array<Iterable<[KC, VC]>>
1245 ): OrderedMap<K | KC, V | VC | VCC>;
1246 mergeWith<C, CC>(
1247 merger: (oldVal: V, newVal: C, key: string) => CC,
1248 ...collections: Array<{ [key: string]: C }>
1249 ): OrderedMap<K | string, V | C | CC>;
1250
1251 mergeDeep<KC, VC>(
1252 ...collections: Array<Iterable<[KC, VC]>>
1253 ): OrderedMap<K | KC, V | VC>;
1254 mergeDeep<C>(
1255 ...collections: Array<{ [key: string]: C }>
1256 ): OrderedMap<K | string, V | C>;
1257
1258 // Sequence algorithms
1259
1260 /**
1261 * Returns a new OrderedMap with values passed through a
1262 * `mapper` function.
1263 *
1264 * OrderedMap({ a: 1, b: 2 }).map(x => 10 * x)
1265 * // OrderedMap { "a": 10, "b": 20 }
1266 *
1267 * Note: `map()` always returns a new instance, even if it produced the same
1268 * value at every step.
1269 */
1270 map<M>(
1271 mapper: (value: V, key: K, iter: this) => M,
1272 context?: unknown
1273 ): OrderedMap<K, M>;
1274
1275 /**
1276 * @see Collection.Keyed.mapKeys
1277 */
1278 mapKeys<M>(
1279 mapper: (key: K, value: V, iter: this) => M,
1280 context?: unknown
1281 ): OrderedMap<M, V>;
1282
1283 /**
1284 * @see Collection.Keyed.mapEntries
1285 */
1286 mapEntries<KM, VM>(
1287 mapper: (
1288 entry: [K, V],
1289 index: number,
1290 iter: this
1291 ) => [KM, VM] | undefined,
1292 context?: unknown
1293 ): OrderedMap<KM, VM>;
1294
1295 /**
1296 * Flat-maps the OrderedMap, returning a new OrderedMap.
1297 *
1298 * Similar to `data.map(...).flatten(true)`.
1299 */
1300 flatMap<KM, VM>(
1301 mapper: (value: V, key: K, iter: this) => Iterable<[KM, VM]>,
1302 context?: unknown
1303 ): OrderedMap<KM, VM>;
1304
1305 /**
1306 * Returns a new OrderedMap with only the entries for which the `predicate`
1307 * function returns true.
1308 *
1309 * Note: `filter()` always returns a new instance, even if it results in
1310 * not filtering out any values.
1311 */
1312 filter<F extends V>(
1313 predicate: (value: V, key: K, iter: this) => value is F,
1314 context?: unknown
1315 ): OrderedMap<K, F>;
1316 filter(
1317 predicate: (value: V, key: K, iter: this) => unknown,
1318 context?: unknown
1319 ): this;
1320
1321 /**
1322 * Returns a new OrderedMap with the values for which the `predicate`
1323 * function returns false and another for which is returns true.
1324 */
1325 partition<F extends V, C>(
1326 predicate: (this: C, value: V, key: K, iter: this) => value is F,
1327 context?: C
1328 ): [OrderedMap<K, V>, OrderedMap<K, F>];
1329 partition<C>(
1330 predicate: (this: C, value: V, key: K, iter: this) => unknown,
1331 context?: C
1332 ): [this, this];
1333
1334 /**
1335 * @see Collection.Keyed.flip
1336 */
1337 flip(): OrderedMap<V, K>;
1338 }
1339
1340 /**
1341 * A Collection of unique values with `O(log32 N)` adds and has.
1342 *
1343 * When iterating a Set, the entries will be (value, value) pairs. Iteration
1344 * order of a Set is undefined, however is stable. Multiple iterations of the
1345 * same Set will iterate in the same order.
1346 *
1347 * Set values, like Map keys, may be of any type. Equality is determined using
1348 * `Immutable.is`, enabling Sets to uniquely include other Immutable
1349 * collections, custom value types, and NaN.
1350 */
1351 namespace Set {
1352 /**
1353 * True if the provided value is a Set
1354 */
1355 function isSet(maybeSet: unknown): maybeSet is Set<unknown>;
1356
1357 /**
1358 * Creates a new Set containing `values`.
1359 */
1360 function of<T>(...values: Array<T>): Set<T>;
1361
1362 /**
1363 * `Set.fromKeys()` creates a new immutable Set containing the keys from
1364 * this Collection or JavaScript Object.
1365 */
1366 function fromKeys<T>(iter: Collection.Keyed<T, unknown>): Set<T>;
1367 function fromKeys<T>(iter: Collection<T, unknown>): Set<T>;
1368 function fromKeys(obj: { [key: string]: unknown }): Set<string>;
1369
1370 /**
1371 * `Set.intersect()` creates a new immutable Set that is the intersection of
1372 * a collection of other sets.
1373 *
1374 * ```js
1375 * import { Set } from 'immutable'
1376 * const intersected = Set.intersect([
1377 * Set([ 'a', 'b', 'c' ])
1378 * Set([ 'c', 'a', 't' ])
1379 * ])
1380 * // Set [ "a", "c" ]
1381 * ```
1382 */
1383 function intersect<T>(sets: Iterable<Iterable<T>>): Set<T>;
1384
1385 /**
1386 * `Set.union()` creates a new immutable Set that is the union of a
1387 * collection of other sets.
1388 *
1389 * ```js
1390 * import { Set } from 'immutable'
1391 * const unioned = Set.union([
1392 * Set([ 'a', 'b', 'c' ])
1393 * Set([ 'c', 'a', 't' ])
1394 * ])
1395 * // Set [ "a", "b", "c", "t" ]
1396 * ```
1397 */
1398 function union<T>(sets: Iterable<Iterable<T>>): Set<T>;
1399 }
1400
1401 /**
1402 * Create a new immutable Set containing the values of the provided
1403 * collection-like.
1404 *
1405 * Note: `Set` is a factory function and not a class, and does not use the
1406 * `new` keyword during construction.
1407 */
1408 function Set<T>(collection?: Iterable<T> | ArrayLike<T>): Set<T>;
1409
1410 interface Set<T> extends Collection.Set<T> {
1411 /**
1412 * The number of items in this Set.
1413 */
1414 readonly size: number;
1415
1416 // Persistent changes
1417
1418 /**
1419 * Returns a new Set which also includes this value.
1420 *
1421 * Note: `add` can be used in `withMutations`.
1422 */
1423 add(value: T): this;
1424
1425 /**
1426 * Returns a new Set which excludes this value.
1427 *
1428 * Note: `delete` can be used in `withMutations`.
1429 *
1430 * Note: `delete` **cannot** be safely used in IE8, use `remove` if
1431 * supporting old browsers.
1432 *
1433 * @alias remove
1434 */
1435 delete(value: T): this;
1436 remove(value: T): this;
1437
1438 /**
1439 * Returns a new Set containing no values.
1440 *
1441 * Note: `clear` can be used in `withMutations`.
1442 */
1443 clear(): this;
1444
1445 /**
1446 * Returns a Set including any value from `collections` that does not already
1447 * exist in this Set.
1448 *
1449 * Note: `union` can be used in `withMutations`.
1450 * @alias merge
1451 * @alias concat
1452 */
1453 union<C>(...collections: Array<Iterable<C>>): Set<T | C>;
1454 merge<C>(...collections: Array<Iterable<C>>): Set<T | C>;
1455 concat<C>(...collections: Array<Iterable<C>>): Set<T | C>;
1456
1457 /**
1458 * Returns a Set which has removed any values not also contained
1459 * within `collections`.
1460 *
1461 * Note: `intersect` can be used in `withMutations`.
1462 */
1463 intersect(...collections: Array<Iterable<T>>): this;
1464
1465 /**
1466 * Returns a Set excluding any values contained within `collections`.
1467 *
1468 * Note: `subtract` can be used in `withMutations`.
1469 */
1470 subtract(...collections: Array<Iterable<T>>): this;
1471
1472 // Transient changes
1473
1474 /**
1475 * Note: Not all methods can be used on a mutable collection or within
1476 * `withMutations`! Check the documentation for each method to see if it
1477 * mentions being safe to use in `withMutations`.
1478 *
1479 * @see `Map#withMutations`
1480 */
1481 withMutations(mutator: (mutable: this) => unknown): this;
1482
1483 /**
1484 * Note: Not all methods can be used on a mutable collection or within
1485 * `withMutations`! Check the documentation for each method to see if it
1486 * mentions being safe to use in `withMutations`.
1487 *
1488 * @see `Map#asMutable`
1489 */
1490 asMutable(): this;
1491
1492 /**
1493 * @see `Map#wasAltered`
1494 */
1495 wasAltered(): boolean;
1496
1497 /**
1498 * @see `Map#asImmutable`
1499 */
1500 asImmutable(): this;
1501
1502 // Sequence algorithms
1503
1504 /**
1505 * Returns a new Set with values passed through a
1506 * `mapper` function.
1507 *
1508 * Set([1,2]).map(x => 10 * x)
1509 * // Set [10,20]
1510 */
1511 map<M>(
1512 mapper: (value: T, key: T, iter: this) => M,
1513 context?: unknown
1514 ): Set<M>;
1515
1516 /**
1517 * Flat-maps the Set, returning a new Set.
1518 *
1519 * Similar to `set.map(...).flatten(true)`.
1520 */
1521 flatMap<M>(
1522 mapper: (value: T, key: T, iter: this) => Iterable<M>,
1523 context?: unknown
1524 ): Set<M>;
1525
1526 /**
1527 * Returns a new Set with only the values for which the `predicate`
1528 * function returns true.
1529 *
1530 * Note: `filter()` always returns a new instance, even if it results in
1531 * not filtering out any values.
1532 */
1533 filter<F extends T>(
1534 predicate: (value: T, key: T, iter: this) => value is F,
1535 context?: unknown
1536 ): Set<F>;
1537 filter(
1538 predicate: (value: T, key: T, iter: this) => unknown,
1539 context?: unknown
1540 ): this;
1541
1542 /**
1543 * Returns a new Set with the values for which the `predicate` function
1544 * returns false and another for which is returns true.
1545 */
1546 partition<F extends T, C>(
1547 predicate: (this: C, value: T, key: T, iter: this) => value is F,
1548 context?: C
1549 ): [Set<T>, Set<F>];
1550 partition<C>(
1551 predicate: (this: C, value: T, key: T, iter: this) => unknown,
1552 context?: C
1553 ): [this, this];
1554
1555 /**
1556 * Returns an OrderedSet of the same type which includes the same entries,
1557 * stably sorted by using a `comparator`.
1558 *
1559 * If a `comparator` is not provided, a default comparator uses `<` and `>`.
1560 *
1561 * `comparator(valueA, valueB)`:
1562 *
1563 * * Returns `0` if the elements should not be swapped.
1564 * * Returns `-1` (or any negative number) if `valueA` comes before `valueB`
1565 * * Returns `1` (or any positive number) if `valueA` comes after `valueB`
1566 * * Alternatively, can return a value of the `PairSorting` enum type
1567 * * Is pure, i.e. it must always return the same value for the same pair
1568 * of values.
1569 *
1570 * Note: `sort()` Always returns a new instance, even if the original was
1571 * already sorted.
1572 *
1573 * Note: This is always an eager operation.
1574 */
1575 sort(comparator?: Comparator<T>): this & OrderedSet<T>;
1576
1577 /**
1578 * Like `sort`, but also accepts a `comparatorValueMapper` which allows for
1579 * sorting by more sophisticated means:
1580 *
1581 * Note: `sortBy()` Always returns a new instance, even if the original was
1582 * already sorted.
1583 *
1584 * Note: This is always an eager operation.
1585 */
1586 sortBy<C>(
1587 comparatorValueMapper: (value: T, key: T, iter: this) => C,
1588 comparator?: (valueA: C, valueB: C) => number
1589 ): this & OrderedSet<T>;
1590 }
1591
1592 /**
1593 * A type of Set that has the additional guarantee that the iteration order of
1594 * values will be the order in which they were `add`ed.
1595 *
1596 * The iteration behavior of OrderedSet is the same as native ES6 Set.
1597 *
1598 * Note that `OrderedSet` are more expensive than non-ordered `Set` and may
1599 * consume more memory. `OrderedSet#add` is amortized O(log32 N), but not
1600 * stable.
1601 */
1602 namespace OrderedSet {
1603 /**
1604 * True if the provided value is an OrderedSet.
1605 */
1606 function isOrderedSet(
1607 maybeOrderedSet: unknown
1608 ): maybeOrderedSet is OrderedSet<unknown>;
1609
1610 /**
1611 * Creates a new OrderedSet containing `values`.
1612 */
1613 function of<T>(...values: Array<T>): OrderedSet<T>;
1614
1615 /**
1616 * `OrderedSet.fromKeys()` creates a new immutable OrderedSet containing
1617 * the keys from this Collection or JavaScript Object.
1618 */
1619 function fromKeys<T>(iter: Collection.Keyed<T, unknown>): OrderedSet<T>;
1620 function fromKeys<T>(iter: Collection<T, unknown>): OrderedSet<T>;
1621 function fromKeys(obj: { [key: string]: unknown }): OrderedSet<string>;
1622 }
1623
1624 /**
1625 * Create a new immutable OrderedSet containing the values of the provided
1626 * collection-like.
1627 *
1628 * Note: `OrderedSet` is a factory function and not a class, and does not use
1629 * the `new` keyword during construction.
1630 */
1631 function OrderedSet<T>(
1632 collection?: Iterable<T> | ArrayLike<T>
1633 ): OrderedSet<T>;
1634
1635 interface OrderedSet<T> extends Set<T>, OrderedCollection<T> {
1636 /**
1637 * The number of items in this OrderedSet.
1638 */
1639 readonly size: number;
1640
1641 /**
1642 * Returns an OrderedSet including any value from `collections` that does
1643 * not already exist in this OrderedSet.
1644 *
1645 * Note: `union` can be used in `withMutations`.
1646 * @alias merge
1647 * @alias concat
1648 */
1649 union<C>(...collections: Array<Iterable<C>>): OrderedSet<T | C>;
1650 merge<C>(...collections: Array<Iterable<C>>): OrderedSet<T | C>;
1651 concat<C>(...collections: Array<Iterable<C>>): OrderedSet<T | C>;
1652
1653 // Sequence algorithms
1654
1655 /**
1656 * Returns a new Set with values passed through a
1657 * `mapper` function.
1658 *
1659 * OrderedSet([ 1, 2 ]).map(x => 10 * x)
1660 * // OrderedSet [10, 20]
1661 */
1662 map<M>(
1663 mapper: (value: T, key: T, iter: this) => M,
1664 context?: unknown
1665 ): OrderedSet<M>;
1666
1667 /**
1668 * Flat-maps the OrderedSet, returning a new OrderedSet.
1669 *
1670 * Similar to `set.map(...).flatten(true)`.
1671 */
1672 flatMap<M>(
1673 mapper: (value: T, key: T, iter: this) => Iterable<M>,
1674 context?: unknown
1675 ): OrderedSet<M>;
1676
1677 /**
1678 * Returns a new OrderedSet with only the values for which the `predicate`
1679 * function returns true.
1680 *
1681 * Note: `filter()` always returns a new instance, even if it results in
1682 * not filtering out any values.
1683 */
1684 filter<F extends T>(
1685 predicate: (value: T, key: T, iter: this) => value is F,
1686 context?: unknown
1687 ): OrderedSet<F>;
1688 filter(
1689 predicate: (value: T, key: T, iter: this) => unknown,
1690 context?: unknown
1691 ): this;
1692
1693 /**
1694 * Returns a new OrderedSet with the values for which the `predicate`
1695 * function returns false and another for which is returns true.
1696 */
1697 partition<F extends T, C>(
1698 predicate: (this: C, value: T, key: T, iter: this) => value is F,
1699 context?: C
1700 ): [OrderedSet<T>, OrderedSet<F>];
1701 partition<C>(
1702 predicate: (this: C, value: T, key: T, iter: this) => unknown,
1703 context?: C
1704 ): [this, this];
1705
1706 /**
1707 * Returns an OrderedSet of the same type "zipped" with the provided
1708 * collections.
1709 *
1710 * Like `zipWith`, but using the default `zipper`: creating an `Array`.
1711 *
1712 * ```js
1713 * const a = OrderedSet([ 1, 2, 3 ])
1714 * const b = OrderedSet([ 4, 5, 6 ])
1715 * const c = a.zip(b)
1716 * // OrderedSet [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
1717 * ```
1718 */
1719 zip<U>(other: Collection<unknown, U>): OrderedSet<[T, U]>;
1720 zip<U, V>(
1721 other1: Collection<unknown, U>,
1722 other2: Collection<unknown, V>
1723 ): OrderedSet<[T, U, V]>;
1724 zip(
1725 ...collections: Array<Collection<unknown, unknown>>
1726 ): OrderedSet<unknown>;
1727
1728 /**
1729 * Returns a OrderedSet of the same type "zipped" with the provided
1730 * collections.
1731 *
1732 * Unlike `zip`, `zipAll` continues zipping until the longest collection is
1733 * exhausted. Missing values from shorter collections are filled with `undefined`.
1734 *
1735 * ```js
1736 * const a = OrderedSet([ 1, 2 ]);
1737 * const b = OrderedSet([ 3, 4, 5 ]);
1738 * const c = a.zipAll(b); // OrderedSet [ [ 1, 3 ], [ 2, 4 ], [ undefined, 5 ] ]
1739 * ```
1740 *
1741 * Note: Since zipAll will return a collection as large as the largest
1742 * input, some results may contain undefined values. TypeScript cannot
1743 * account for these without cases (as of v2.5).
1744 */
1745 zipAll<U>(other: Collection<unknown, U>): OrderedSet<[T, U]>;
1746 zipAll<U, V>(
1747 other1: Collection<unknown, U>,
1748 other2: Collection<unknown, V>
1749 ): OrderedSet<[T, U, V]>;
1750 zipAll(
1751 ...collections: Array<Collection<unknown, unknown>>
1752 ): OrderedSet<unknown>;
1753
1754 /**
1755 * Returns an OrderedSet of the same type "zipped" with the provided
1756 * collections by using a custom `zipper` function.
1757 *
1758 * @see Seq.Indexed.zipWith
1759 */
1760 zipWith<U, Z>(
1761 zipper: (value: T, otherValue: U) => Z,
1762 otherCollection: Collection<unknown, U>
1763 ): OrderedSet<Z>;
1764 zipWith<U, V, Z>(
1765 zipper: (value: T, otherValue: U, thirdValue: V) => Z,
1766 otherCollection: Collection<unknown, U>,
1767 thirdCollection: Collection<unknown, V>
1768 ): OrderedSet<Z>;
1769 zipWith<Z>(
1770 zipper: (...values: Array<unknown>) => Z,
1771 ...collections: Array<Collection<unknown, unknown>>
1772 ): OrderedSet<Z>;
1773 }
1774
1775 /**
1776 * Stacks are indexed collections which support very efficient O(1) addition
1777 * and removal from the front using `unshift(v)` and `shift()`.
1778 *
1779 * For familiarity, Stack also provides `push(v)`, `pop()`, and `peek()`, but
1780 * be aware that they also operate on the front of the list, unlike List or
1781 * a JavaScript Array.
1782 *
1783 * Note: `reverse()` or any inherent reverse traversal (`reduceRight`,
1784 * `lastIndexOf`, etc.) is not efficient with a Stack.
1785 *
1786 * Stack is implemented with a Single-Linked List.
1787 */
1788 namespace Stack {
1789 /**
1790 * True if the provided value is a Stack
1791 */
1792 function isStack(maybeStack: unknown): maybeStack is Stack<unknown>;
1793
1794 /**
1795 * Creates a new Stack containing `values`.
1796 */
1797 function of<T>(...values: Array<T>): Stack<T>;
1798 }
1799
1800 /**
1801 * Create a new immutable Stack containing the values of the provided
1802 * collection-like.
1803 *
1804 * The iteration order of the provided collection is preserved in the
1805 * resulting `Stack`.
1806 *
1807 * Note: `Stack` is a factory function and not a class, and does not use the
1808 * `new` keyword during construction.
1809 */
1810 function Stack<T>(collection?: Iterable<T> | ArrayLike<T>): Stack<T>;
1811
1812 interface Stack<T> extends Collection.Indexed<T> {
1813 /**
1814 * The number of items in this Stack.
1815 */
1816 readonly size: number;
1817
1818 // Reading values
1819
1820 /**
1821 * Alias for `Stack.first()`.
1822 */
1823 peek(): T | undefined;
1824
1825 // Persistent changes
1826
1827 /**
1828 * Returns a new Stack with 0 size and no values.
1829 *
1830 * Note: `clear` can be used in `withMutations`.
1831 */
1832 clear(): Stack<T>;
1833
1834 /**
1835 * Returns a new Stack with the provided `values` prepended, shifting other
1836 * values ahead to higher indices.
1837 *
1838 * This is very efficient for Stack.
1839 *
1840 * Note: `unshift` can be used in `withMutations`.
1841 */
1842 unshift(...values: Array<T>): Stack<T>;
1843
1844 /**
1845 * Like `Stack#unshift`, but accepts a collection rather than varargs.
1846 *
1847 * Note: `unshiftAll` can be used in `withMutations`.
1848 */
1849 unshiftAll(iter: Iterable<T>): Stack<T>;
1850
1851 /**
1852 * Returns a new Stack with a size ones less than this Stack, excluding
1853 * the first item in this Stack, shifting all other values to a lower index.
1854 *
1855 * Note: this differs from `Array#shift` because it returns a new
1856 * Stack rather than the removed value. Use `first()` or `peek()` to get the
1857 * first value in this Stack.
1858 *
1859 * Note: `shift` can be used in `withMutations`.
1860 */
1861 shift(): Stack<T>;
1862
1863 /**
1864 * Alias for `Stack#unshift` and is not equivalent to `List#push`.
1865 */
1866 push(...values: Array<T>): Stack<T>;
1867
1868 /**
1869 * Alias for `Stack#unshiftAll`.
1870 */
1871 pushAll(iter: Iterable<T>): Stack<T>;
1872
1873 /**
1874 * Alias for `Stack#shift` and is not equivalent to `List#pop`.
1875 */
1876 pop(): Stack<T>;
1877
1878 // Transient changes
1879
1880 /**
1881 * Note: Not all methods can be used on a mutable collection or within
1882 * `withMutations`! Check the documentation for each method to see if it
1883 * mentions being safe to use in `withMutations`.
1884 *
1885 * @see `Map#withMutations`
1886 */
1887 withMutations(mutator: (mutable: this) => unknown): this;
1888
1889 /**
1890 * Note: Not all methods can be used on a mutable collection or within
1891 * `withMutations`! Check the documentation for each method to see if it
1892 * mentions being safe to use in `withMutations`.
1893 *
1894 * @see `Map#asMutable`
1895 */
1896 asMutable(): this;
1897
1898 /**
1899 * @see `Map#wasAltered`
1900 */
1901 wasAltered(): boolean;
1902
1903 /**
1904 * @see `Map#asImmutable`
1905 */
1906 asImmutable(): this;
1907
1908 // Sequence algorithms
1909
1910 /**
1911 * Returns a new Stack with other collections concatenated to this one.
1912 */
1913 concat<C>(...valuesOrCollections: Array<Iterable<C> | C>): Stack<T | C>;
1914
1915 /**
1916 * Returns a new Stack with values passed through a
1917 * `mapper` function.
1918 *
1919 * Stack([ 1, 2 ]).map(x => 10 * x)
1920 * // Stack [ 10, 20 ]
1921 *
1922 * Note: `map()` always returns a new instance, even if it produced the same
1923 * value at every step.
1924 */
1925 map<M>(
1926 mapper: (value: T, key: number, iter: this) => M,
1927 context?: unknown
1928 ): Stack<M>;
1929
1930 /**
1931 * Flat-maps the Stack, returning a new Stack.
1932 *
1933 * Similar to `stack.map(...).flatten(true)`.
1934 */
1935 flatMap<M>(
1936 mapper: (value: T, key: number, iter: this) => Iterable<M>,
1937 context?: unknown
1938 ): Stack<M>;
1939
1940 /**
1941 * Returns a new Set with only the values for which the `predicate`
1942 * function returns true.
1943 *
1944 * Note: `filter()` always returns a new instance, even if it results in
1945 * not filtering out any values.
1946 */
1947 filter<F extends T>(
1948 predicate: (value: T, index: number, iter: this) => value is F,
1949 context?: unknown
1950 ): Set<F>;
1951 filter(
1952 predicate: (value: T, index: number, iter: this) => unknown,
1953 context?: unknown
1954 ): this;
1955
1956 /**
1957 * Returns a Stack "zipped" with the provided collections.
1958 *
1959 * Like `zipWith`, but using the default `zipper`: creating an `Array`.
1960 *
1961 * ```js
1962 * const a = Stack([ 1, 2, 3 ]);
1963 * const b = Stack([ 4, 5, 6 ]);
1964 * const c = a.zip(b); // Stack [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
1965 * ```
1966 */
1967 zip<U>(other: Collection<unknown, U>): Stack<[T, U]>;
1968 zip<U, V>(
1969 other: Collection<unknown, U>,
1970 other2: Collection<unknown, V>
1971 ): Stack<[T, U, V]>;
1972 zip(...collections: Array<Collection<unknown, unknown>>): Stack<unknown>;
1973
1974 /**
1975 * Returns a Stack "zipped" with the provided collections.
1976 *
1977 * Unlike `zip`, `zipAll` continues zipping until the longest collection is
1978 * exhausted. Missing values from shorter collections are filled with `undefined`.
1979 *
1980 * ```js
1981 * const a = Stack([ 1, 2 ]);
1982 * const b = Stack([ 3, 4, 5 ]);
1983 * const c = a.zipAll(b); // Stack [ [ 1, 3 ], [ 2, 4 ], [ undefined, 5 ] ]
1984 * ```
1985 *
1986 * Note: Since zipAll will return a collection as large as the largest
1987 * input, some results may contain undefined values. TypeScript cannot
1988 * account for these without cases (as of v2.5).
1989 */
1990 zipAll<U>(other: Collection<unknown, U>): Stack<[T, U]>;
1991 zipAll<U, V>(
1992 other: Collection<unknown, U>,
1993 other2: Collection<unknown, V>
1994 ): Stack<[T, U, V]>;
1995 zipAll(...collections: Array<Collection<unknown, unknown>>): Stack<unknown>;
1996
1997 /**
1998 * Returns a Stack "zipped" with the provided collections by using a
1999 * custom `zipper` function.
2000 *
2001 * ```js
2002 * const a = Stack([ 1, 2, 3 ]);
2003 * const b = Stack([ 4, 5, 6 ]);
2004 * const c = a.zipWith((a, b) => a + b, b);
2005 * // Stack [ 5, 7, 9 ]
2006 * ```
2007 */
2008 zipWith<U, Z>(
2009 zipper: (value: T, otherValue: U) => Z,
2010 otherCollection: Collection<unknown, U>
2011 ): Stack<Z>;
2012 zipWith<U, V, Z>(
2013 zipper: (value: T, otherValue: U, thirdValue: V) => Z,
2014 otherCollection: Collection<unknown, U>,
2015 thirdCollection: Collection<unknown, V>
2016 ): Stack<Z>;
2017 zipWith<Z>(
2018 zipper: (...values: Array<unknown>) => Z,
2019 ...collections: Array<Collection<unknown, unknown>>
2020 ): Stack<Z>;
2021 }
2022
2023 /**
2024 * Returns a Seq.Indexed of numbers from `start` (inclusive) to `end`
2025 * (exclusive), by `step`, where `start` defaults to 0, `step` to 1, and `end` to
2026 * infinity. When `start` is equal to `end`, returns empty range.
2027 *
2028 * Note: `Range` is a factory function and not a class, and does not use the
2029 * `new` keyword during construction.
2030 *
2031 * ```js
2032 * import { Range } from 'immutable'
2033 * Range(10, 15) // [ 10, 11, 12, 13, 14 ]
2034 * Range(10, 30, 5) // [ 10, 15, 20, 25 ]
2035 * Range(30, 10, 5) // [ 30, 25, 20, 15 ]
2036 * Range(30, 30, 5) // []
2037 * ```
2038 */
2039 function Range(
2040 start: number,
2041 end: number,
2042 step?: number
2043 ): Seq.Indexed<number>;
2044
2045 /**
2046 * Returns a Seq.Indexed of `value` repeated `times` times. When `times` is
2047 * not defined, returns an infinite `Seq` of `value`.
2048 *
2049 * Note: `Repeat` is a factory function and not a class, and does not use the
2050 * `new` keyword during construction.
2051 *
2052 * ```js
2053 * import { Repeat } from 'immutable'
2054 * Repeat('foo') // [ 'foo', 'foo', 'foo', ... ]
2055 * Repeat('bar', 4) // [ 'bar', 'bar', 'bar', 'bar' ]
2056 * ```
2057 */
2058 function Repeat<T>(value: T, times?: number): Seq.Indexed<T>;
2059
2060 /**
2061 * A record is similar to a JS object, but enforces a specific set of allowed
2062 * string keys, and has default values.
2063 *
2064 * The `Record()` function produces new Record Factories, which when called
2065 * create Record instances.
2066 *
2067 * ```js
2068 * import { Record } from 'immutable'
2069 * const ABRecord = Record({ a: 1, b: 2 })
2070 * const myRecord = ABRecord({ b: 3 })
2071 * ```
2072 *
2073 * Records always have a value for the keys they define. `remove`ing a key
2074 * from a record simply resets it to the default value for that key.
2075 *
2076 * ```js
2077 * myRecord.get('a') // 1
2078 * myRecord.get('b') // 3
2079 * const myRecordWithoutB = myRecord.remove('b')
2080 * myRecordWithoutB.get('b') // 2
2081 * ```
2082 *
2083 * Values provided to the constructor not found in the Record type will
2084 * be ignored. For example, in this case, ABRecord is provided a key "x" even
2085 * though only "a" and "b" have been defined. The value for "x" will be
2086 * ignored for this record.
2087 *
2088 * ```js
2089 * const myRecord = ABRecord({ b: 3, x: 10 })
2090 * myRecord.get('x') // undefined
2091 * ```
2092 *
2093 * Because Records have a known set of string keys, property get access works
2094 * as expected, however property sets will throw an Error.
2095 *
2096 * Note: IE8 does not support property access. Only use `get()` when
2097 * supporting IE8.
2098 *
2099 * ```js
2100 * myRecord.b // 3
2101 * myRecord.b = 5 // throws Error
2102 * ```
2103 *
2104 * Record Types can be extended as well, allowing for custom methods on your
2105 * Record. This is not a common pattern in functional environments, but is in
2106 * many JS programs.
2107 *
2108 * However Record Types are more restricted than typical JavaScript classes.
2109 * They do not use a class constructor, which also means they cannot use
2110 * class properties (since those are technically part of a constructor).
2111 *
2112 * While Record Types can be syntactically created with the JavaScript `class`
2113 * form, the resulting Record function is actually a factory function, not a
2114 * class constructor. Even though Record Types are not classes, JavaScript
2115 * currently requires the use of `new` when creating new Record instances if
2116 * they are defined as a `class`.
2117 *
2118 * ```
2119 * class ABRecord extends Record({ a: 1, b: 2 }) {
2120 * getAB() {
2121 * return this.a + this.b;
2122 * }
2123 * }
2124 *
2125 * var myRecord = new ABRecord({b: 3})
2126 * myRecord.getAB() // 4
2127 * ```
2128 *
2129 *
2130 * **Flow Typing Records:**
2131 *
2132 * Immutable.js exports two Flow types designed to make it easier to use
2133 * Records with flow typed code, `RecordOf<TProps>` and `RecordFactory<TProps>`.
2134 *
2135 * When defining a new kind of Record factory function, use a flow type that
2136 * describes the values the record contains along with `RecordFactory<TProps>`.
2137 * To type instances of the Record (which the factory function returns),
2138 * use `RecordOf<TProps>`.
2139 *
2140 * Typically, new Record definitions will export both the Record factory
2141 * function as well as the Record instance type for use in other code.
2142 *
2143 * ```js
2144 * import type { RecordFactory, RecordOf } from 'immutable';
2145 *
2146 * // Use RecordFactory<TProps> for defining new Record factory functions.
2147 * type Point3DProps = { x: number, y: number, z: number };
2148 * const defaultValues: Point3DProps = { x: 0, y: 0, z: 0 };
2149 * const makePoint3D: RecordFactory<Point3DProps> = Record(defaultValues);
2150 * export makePoint3D;
2151 *
2152 * // Use RecordOf<T> for defining new instances of that Record.
2153 * export type Point3D = RecordOf<Point3DProps>;
2154 * const some3DPoint: Point3D = makePoint3D({ x: 10, y: 20, z: 30 });
2155 * ```
2156 *
2157 * **Flow Typing Record Subclasses:**
2158 *
2159 * Records can be subclassed as a means to add additional methods to Record
2160 * instances. This is generally discouraged in favor of a more functional API,
2161 * since Subclasses have some minor overhead. However the ability to create
2162 * a rich API on Record types can be quite valuable.
2163 *
2164 * When using Flow to type Subclasses, do not use `RecordFactory<TProps>`,
2165 * instead apply the props type when subclassing:
2166 *
2167 * ```js
2168 * type PersonProps = {name: string, age: number};
2169 * const defaultValues: PersonProps = {name: 'Aristotle', age: 2400};
2170 * const PersonRecord = Record(defaultValues);
2171 * class Person extends PersonRecord<PersonProps> {
2172 * getName(): string {
2173 * return this.get('name')
2174 * }
2175 *
2176 * setName(name: string): this {
2177 * return this.set('name', name);
2178 * }
2179 * }
2180 * ```
2181 *
2182 * **Choosing Records vs plain JavaScript objects**
2183 *
2184 * Records offer a persistently immutable alternative to plain JavaScript
2185 * objects, however they're not required to be used within Immutable.js
2186 * collections. In fact, the deep-access and deep-updating functions
2187 * like `getIn()` and `setIn()` work with plain JavaScript Objects as well.
2188 *
2189 * Deciding to use Records or Objects in your application should be informed
2190 * by the tradeoffs and relative benefits of each:
2191 *
2192 * - *Runtime immutability*: plain JS objects may be carefully treated as
2193 * immutable, however Record instances will *throw* if attempted to be
2194 * mutated directly. Records provide this additional guarantee, however at
2195 * some marginal runtime cost. While JS objects are mutable by nature, the
2196 * use of type-checking tools like [Flow](https://medium.com/@gcanti/immutability-with-flow-faa050a1aef4)
2197 * can help gain confidence in code written to favor immutability.
2198 *
2199 * - *Value equality*: Records use value equality when compared with `is()`
2200 * or `record.equals()`. That is, two Records with the same keys and values
2201 * are equal. Plain objects use *reference equality*. Two objects with the
2202 * same keys and values are not equal since they are different objects.
2203 * This is important to consider when using objects as keys in a `Map` or
2204 * values in a `Set`, which use equality when retrieving values.
2205 *
2206 * - *API methods*: Records have a full featured API, with methods like
2207 * `.getIn()`, and `.equals()`. These can make working with these values
2208 * easier, but comes at the cost of not allowing keys with those names.
2209 *
2210 * - *Default values*: Records provide default values for every key, which
2211 * can be useful when constructing Records with often unchanging values.
2212 * However default values can make using Flow and TypeScript more laborious.
2213 *
2214 * - *Serialization*: Records use a custom internal representation to
2215 * efficiently store and update their values. Converting to and from this
2216 * form isn't free. If converting Records to plain objects is common,
2217 * consider sticking with plain objects to begin with.
2218 */
2219 namespace Record {
2220 /**
2221 * True if `maybeRecord` is an instance of a Record.
2222 */
2223 function isRecord(maybeRecord: unknown): maybeRecord is Record<object>;
2224
2225 /**
2226 * Records allow passing a second parameter to supply a descriptive name
2227 * that appears when converting a Record to a string or in any error
2228 * messages. A descriptive name for any record can be accessed by using this
2229 * method. If one was not provided, the string "Record" is returned.
2230 *
2231 * ```js
2232 * import { Record } from 'immutable'
2233 * const Person = Record({
2234 * name: null
2235 * }, 'Person')
2236 *
2237 * var me = Person({ name: 'My Name' })
2238 * me.toString() // "Person { "name": "My Name" }"
2239 * Record.getDescriptiveName(me) // "Person"
2240 * ```
2241 */
2242 function getDescriptiveName<TProps extends object>(
2243 record: RecordOf<TProps>
2244 ): string;
2245
2246 /**
2247 * A Record.Factory is created by the `Record()` function. Record instances
2248 * are created by passing it some of the accepted values for that Record
2249 * type:
2250 *
2251 * Note that Record Factories return `Record<TProps> & Readonly<TProps>`,
2252 * this allows use of both the Record instance API, and direct property
2253 * access on the resulting instances:
2254 */
2255 namespace Factory {}
2256
2257 interface Factory<TProps extends object> {
2258 (
2259 values?: Partial<TProps> | Iterable<[string, unknown]>
2260 ): RecordOf<TProps>;
2261 new (
2262 values?: Partial<TProps> | Iterable<[string, unknown]>
2263 ): RecordOf<TProps>;
2264
2265 /**
2266 * The name provided to `Record(values, name)` can be accessed with
2267 * `displayName`.
2268 */
2269 displayName: string;
2270 }
2271
2272 function Factory<TProps extends object>(
2273 values?: Partial<TProps> | Iterable<[string, unknown]>
2274 ): RecordOf<TProps>;
2275 }
2276
2277 /**
2278 * Unlike other types in Immutable.js, the `Record()` function creates a new
2279 * Record Factory, which is a function that creates Record instances.
2280 *
2281 * See above for examples of using `Record()`.
2282 *
2283 * Note: `Record` is a factory function and not a class, and does not use the
2284 * `new` keyword during construction.
2285 */
2286 function Record<TProps extends object>(
2287 defaultValues: TProps,
2288 name?: string
2289 ): Record.Factory<TProps>;
2290
2291 interface Record<TProps extends object> {
2292 // Reading values
2293
2294 has(key: string): key is keyof TProps & string;
2295
2296 /**
2297 * Returns the value associated with the provided key, which may be the
2298 * default value defined when creating the Record factory function.
2299 *
2300 * If the requested key is not defined by this Record type, then
2301 * notSetValue will be returned if provided. Note that this scenario would
2302 * produce an error when using Flow or TypeScript.
2303 */
2304 get<K extends keyof TProps>(key: K, notSetValue?: unknown): TProps[K];
2305 get<T>(key: string, notSetValue: T): T;
2306
2307 // Reading deep values
2308
2309 hasIn(keyPath: Iterable<unknown>): boolean;
2310 getIn(keyPath: Iterable<unknown>): unknown;
2311
2312 // Value equality
2313
2314 equals(other: unknown): boolean;
2315 hashCode(): number;
2316
2317 // Persistent changes
2318
2319 set<K extends keyof TProps>(key: K, value: TProps[K]): this;
2320 update<K extends keyof TProps>(
2321 key: K,
2322 updater: (value: TProps[K]) => TProps[K]
2323 ): this;
2324 merge(
2325 ...collections: Array<Partial<TProps> | Iterable<[string, unknown]>>
2326 ): this;
2327 mergeDeep(
2328 ...collections: Array<Partial<TProps> | Iterable<[string, unknown]>>
2329 ): this;
2330
2331 mergeWith(
2332 merger: (oldVal: unknown, newVal: unknown, key: keyof TProps) => unknown,
2333 ...collections: Array<Partial<TProps> | Iterable<[string, unknown]>>
2334 ): this;
2335 mergeDeepWith(
2336 merger: (oldVal: unknown, newVal: unknown, key: unknown) => unknown,
2337 ...collections: Array<Partial<TProps> | Iterable<[string, unknown]>>
2338 ): this;
2339
2340 /**
2341 * Returns a new instance of this Record type with the value for the
2342 * specific key set to its default value.
2343 *
2344 * @alias remove
2345 */
2346 delete<K extends keyof TProps>(key: K): this;
2347 remove<K extends keyof TProps>(key: K): this;
2348
2349 /**
2350 * Returns a new instance of this Record type with all values set
2351 * to their default values.
2352 */
2353 clear(): this;
2354
2355 // Deep persistent changes
2356
2357 setIn(keyPath: Iterable<unknown>, value: unknown): this;
2358 updateIn(
2359 keyPath: Iterable<unknown>,
2360 updater: (value: unknown) => unknown
2361 ): this;
2362 mergeIn(keyPath: Iterable<unknown>, ...collections: Array<unknown>): this;
2363 mergeDeepIn(
2364 keyPath: Iterable<unknown>,
2365 ...collections: Array<unknown>
2366 ): this;
2367
2368 /**
2369 * @alias removeIn
2370 */
2371 deleteIn(keyPath: Iterable<unknown>): this;
2372 removeIn(keyPath: Iterable<unknown>): this;
2373
2374 // Conversion to JavaScript types
2375
2376 /**
2377 * Deeply converts this Record to equivalent native JavaScript Object.
2378 *
2379 * Note: This method may not be overridden. Objects with custom
2380 * serialization to plain JS may override toJSON() instead.
2381 */
2382 toJS(): DeepCopy<TProps>;
2383
2384 /**
2385 * Shallowly converts this Record to equivalent native JavaScript Object.
2386 */
2387 toJSON(): TProps;
2388
2389 /**
2390 * Shallowly converts this Record to equivalent JavaScript Object.
2391 */
2392 toObject(): TProps;
2393
2394 // Transient changes
2395
2396 /**
2397 * Note: Not all methods can be used on a mutable collection or within
2398 * `withMutations`! Only `set` may be used mutatively.
2399 *
2400 * @see `Map#withMutations`
2401 */
2402 withMutations(mutator: (mutable: this) => unknown): this;
2403
2404 /**
2405 * @see `Map#asMutable`
2406 */
2407 asMutable(): this;
2408
2409 /**
2410 * @see `Map#wasAltered`
2411 */
2412 wasAltered(): boolean;
2413
2414 /**
2415 * @see `Map#asImmutable`
2416 */
2417 asImmutable(): this;
2418
2419 // Sequence algorithms
2420
2421 toSeq(): Seq.Keyed<keyof TProps, TProps[keyof TProps]>;
2422
2423 [Symbol.iterator](): IterableIterator<[keyof TProps, TProps[keyof TProps]]>;
2424 }
2425
2426 /**
2427 * RecordOf<T> is used in TypeScript to define interfaces expecting an
2428 * instance of record with type T.
2429 *
2430 * This is equivalent to an instance of a record created by a Record Factory.
2431 */
2432 type RecordOf<TProps extends object> = Record<TProps> & Readonly<TProps>;
2433
2434 /**
2435 * `Seq` describes a lazy operation, allowing them to efficiently chain
2436 * use of all the higher-order collection methods (such as `map` and `filter`)
2437 * by not creating intermediate collections.
2438 *
2439 * **Seq is immutable** — Once a Seq is created, it cannot be
2440 * changed, appended to, rearranged or otherwise modified. Instead, any
2441 * mutative method called on a `Seq` will return a new `Seq`.
2442 *
2443 * **Seq is lazy** — `Seq` does as little work as necessary to respond to any
2444 * method call. Values are often created during iteration, including implicit
2445 * iteration when reducing or converting to a concrete data structure such as
2446 * a `List` or JavaScript `Array`.
2447 *
2448 * For example, the following performs no work, because the resulting
2449 * `Seq`'s values are never iterated:
2450 *
2451 * ```js
2452 * import { Seq } from 'immutable'
2453 * const oddSquares = Seq([ 1, 2, 3, 4, 5, 6, 7, 8 ])
2454 * .filter(x => x % 2 !== 0)
2455 * .map(x => x * x)
2456 * ```
2457 *
2458 * Once the `Seq` is used, it performs only the work necessary. In this
2459 * example, no intermediate arrays are ever created, filter is called three
2460 * times, and map is only called once:
2461 *
2462 * ```js
2463 * oddSquares.get(1); // 9
2464 * ```
2465 *
2466 * Any collection can be converted to a lazy Seq with `Seq()`.
2467 *
2468 * `Seq` allows for the efficient chaining of operations, allowing for the
2469 * expression of logic that can otherwise be very tedious:
2470 *
2471 * ```js
2472 * lazySeq
2473 * .flip()
2474 * .map(key => key.toUpperCase())
2475 * .flip()
2476 * // Seq { A: 1, B: 1, C: 1 }
2477 * ```
2478 *
2479 * As well as expressing logic that would otherwise seem memory or time
2480 * limited, for example `Range` is a special kind of Lazy sequence.
2481 *
2482 * Seq is often used to provide a rich collection API to JavaScript Object.
2483 *
2484 * ```js
2485 * Seq({ x: 0, y: 1, z: 2 }).map(v => v * 2).toObject();
2486 * // { x: 0, y: 2, z: 4 }
2487 * ```
2488 */
2489
2490 namespace Seq {
2491 /**
2492 * True if `maybeSeq` is a Seq, it is not backed by a concrete
2493 * structure such as Map, List, or Set.
2494 */
2495 function isSeq(
2496 maybeSeq: unknown
2497 ): maybeSeq is
2498 | Seq.Indexed<unknown>
2499 | Seq.Keyed<unknown, unknown>
2500 | Seq.Set<unknown>;
2501
2502 /**
2503 * `Seq` which represents key-value pairs.
2504 */
2505 namespace Keyed {}
2506
2507 /**
2508 * Always returns a Seq.Keyed, if input is not keyed, expects an
2509 * collection of [K, V] tuples.
2510 *
2511 * Note: `Seq.Keyed` is a conversion function and not a class, and does not
2512 * use the `new` keyword during construction.
2513 */
2514 function Keyed<K, V>(collection?: Iterable<[K, V]>): Seq.Keyed<K, V>;
2515 function Keyed<V>(obj: { [key: string]: V }): Seq.Keyed<string, V>;
2516
2517 interface Keyed<K, V> extends Seq<K, V>, Collection.Keyed<K, V> {
2518 /**
2519 * Deeply converts this Keyed Seq to equivalent native JavaScript Object.
2520 *
2521 * Converts keys to Strings.
2522 */
2523 toJS(): { [key in PropertyKey]: DeepCopy<V> };
2524
2525 /**
2526 * Shallowly converts this Keyed Seq to equivalent native JavaScript Object.
2527 *
2528 * Converts keys to Strings.
2529 */
2530 toJSON(): { [key in PropertyKey]: V };
2531
2532 /**
2533 * Shallowly converts this collection to an Array.
2534 */
2535 toArray(): Array<[K, V]>;
2536
2537 /**
2538 * Returns itself
2539 */
2540 toSeq(): this;
2541
2542 /**
2543 * Returns a new Seq with other collections concatenated to this one.
2544 *
2545 * All entries will be present in the resulting Seq, even if they
2546 * have the same key.
2547 */
2548 concat<KC, VC>(
2549 ...collections: Array<Iterable<[KC, VC]>>
2550 ): Seq.Keyed<K | KC, V | VC>;
2551 concat<C>(
2552 ...collections: Array<{ [key: string]: C }>
2553 ): Seq.Keyed<K | string, V | C>;
2554
2555 /**
2556 * Returns a new Seq.Keyed with values passed through a
2557 * `mapper` function.
2558 *
2559 * ```js
2560 * import { Seq } from 'immutable'
2561 * Seq.Keyed({ a: 1, b: 2 }).map(x => 10 * x)
2562 * // Seq { "a": 10, "b": 20 }
2563 * ```
2564 *
2565 * Note: `map()` always returns a new instance, even if it produced the
2566 * same value at every step.
2567 */
2568 map<M>(
2569 mapper: (value: V, key: K, iter: this) => M,
2570 context?: unknown
2571 ): Seq.Keyed<K, M>;
2572
2573 /**
2574 * @see Collection.Keyed.mapKeys
2575 */
2576 mapKeys<M>(
2577 mapper: (key: K, value: V, iter: this) => M,
2578 context?: unknown
2579 ): Seq.Keyed<M, V>;
2580
2581 /**
2582 * @see Collection.Keyed.mapEntries
2583 */
2584 mapEntries<KM, VM>(
2585 mapper: (
2586 entry: [K, V],
2587 index: number,
2588 iter: this
2589 ) => [KM, VM] | undefined,
2590 context?: unknown
2591 ): Seq.Keyed<KM, VM>;
2592
2593 /**
2594 * Flat-maps the Seq, returning a Seq of the same type.
2595 *
2596 * Similar to `seq.map(...).flatten(true)`.
2597 */
2598 flatMap<KM, VM>(
2599 mapper: (value: V, key: K, iter: this) => Iterable<[KM, VM]>,
2600 context?: unknown
2601 ): Seq.Keyed<KM, VM>;
2602
2603 /**
2604 * Returns a new Seq with only the entries for which the `predicate`
2605 * function returns true.
2606 *
2607 * Note: `filter()` always returns a new instance, even if it results in
2608 * not filtering out any values.
2609 */
2610 filter<F extends V>(
2611 predicate: (value: V, key: K, iter: this) => value is F,
2612 context?: unknown
2613 ): Seq.Keyed<K, F>;
2614 filter(
2615 predicate: (value: V, key: K, iter: this) => unknown,
2616 context?: unknown
2617 ): this;
2618
2619 /**
2620 * Returns a new keyed Seq with the values for which the `predicate`
2621 * function returns false and another for which is returns true.
2622 */
2623 partition<F extends V, C>(
2624 predicate: (this: C, value: V, key: K, iter: this) => value is F,
2625 context?: C
2626 ): [Seq.Keyed<K, V>, Seq.Keyed<K, F>];
2627 partition<C>(
2628 predicate: (this: C, value: V, key: K, iter: this) => unknown,
2629 context?: C
2630 ): [this, this];
2631
2632 /**
2633 * @see Collection.Keyed.flip
2634 */
2635 flip(): Seq.Keyed<V, K>;
2636
2637 [Symbol.iterator](): IterableIterator<[K, V]>;
2638 }
2639
2640 /**
2641 * `Seq` which represents an ordered indexed list of values.
2642 */
2643 namespace Indexed {
2644 /**
2645 * Provides an Seq.Indexed of the values provided.
2646 */
2647 function of<T>(...values: Array<T>): Seq.Indexed<T>;
2648 }
2649
2650 /**
2651 * Always returns Seq.Indexed, discarding associated keys and
2652 * supplying incrementing indices.
2653 *
2654 * Note: `Seq.Indexed` is a conversion function and not a class, and does
2655 * not use the `new` keyword during construction.
2656 */
2657 function Indexed<T>(
2658 collection?: Iterable<T> | ArrayLike<T>
2659 ): Seq.Indexed<T>;
2660
2661 interface Indexed<T> extends Seq<number, T>, Collection.Indexed<T> {
2662 /**
2663 * Deeply converts this Indexed Seq to equivalent native JavaScript Array.
2664 */
2665 toJS(): Array<DeepCopy<T>>;
2666
2667 /**
2668 * Shallowly converts this Indexed Seq to equivalent native JavaScript Array.
2669 */
2670 toJSON(): Array<T>;
2671
2672 /**
2673 * Shallowly converts this collection to an Array.
2674 */
2675 toArray(): Array<T>;
2676
2677 /**
2678 * Returns itself
2679 */
2680 toSeq(): this;
2681
2682 /**
2683 * Returns a new Seq with other collections concatenated to this one.
2684 */
2685 concat<C>(
2686 ...valuesOrCollections: Array<Iterable<C> | C>
2687 ): Seq.Indexed<T | C>;
2688
2689 /**
2690 * Returns a new Seq.Indexed with values passed through a
2691 * `mapper` function.
2692 *
2693 * ```js
2694 * import { Seq } from 'immutable'
2695 * Seq.Indexed([ 1, 2 ]).map(x => 10 * x)
2696 * // Seq [ 10, 20 ]
2697 * ```
2698 *
2699 * Note: `map()` always returns a new instance, even if it produced the
2700 * same value at every step.
2701 */
2702 map<M>(
2703 mapper: (value: T, key: number, iter: this) => M,
2704 context?: unknown
2705 ): Seq.Indexed<M>;
2706
2707 /**
2708 * Flat-maps the Seq, returning a a Seq of the same type.
2709 *
2710 * Similar to `seq.map(...).flatten(true)`.
2711 */
2712 flatMap<M>(
2713 mapper: (value: T, key: number, iter: this) => Iterable<M>,
2714 context?: unknown
2715 ): Seq.Indexed<M>;
2716
2717 /**
2718 * Returns a new Seq with only the values for which the `predicate`
2719 * function returns true.
2720 *
2721 * Note: `filter()` always returns a new instance, even if it results in
2722 * not filtering out any values.
2723 */
2724 filter<F extends T>(
2725 predicate: (value: T, index: number, iter: this) => value is F,
2726 context?: unknown
2727 ): Seq.Indexed<F>;
2728 filter(
2729 predicate: (value: T, index: number, iter: this) => unknown,
2730 context?: unknown
2731 ): this;
2732
2733 /**
2734 * Returns a new indexed Seq with the values for which the `predicate`
2735 * function returns false and another for which is returns true.
2736 */
2737 partition<F extends T, C>(
2738 predicate: (this: C, value: T, index: number, iter: this) => value is F,
2739 context?: C
2740 ): [Seq.Indexed<T>, Seq.Indexed<F>];
2741 partition<C>(
2742 predicate: (this: C, value: T, index: number, iter: this) => unknown,
2743 context?: C
2744 ): [this, this];
2745
2746 /**
2747 * Returns a Seq "zipped" with the provided collections.
2748 *
2749 * Like `zipWith`, but using the default `zipper`: creating an `Array`.
2750 *
2751 * ```js
2752 * const a = Seq([ 1, 2, 3 ]);
2753 * const b = Seq([ 4, 5, 6 ]);
2754 * const c = a.zip(b); // Seq [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
2755 * ```
2756 */
2757 zip<U>(other: Collection<unknown, U>): Seq.Indexed<[T, U]>;
2758 zip<U, V>(
2759 other: Collection<unknown, U>,
2760 other2: Collection<unknown, V>
2761 ): Seq.Indexed<[T, U, V]>;
2762 zip(
2763 ...collections: Array<Collection<unknown, unknown>>
2764 ): Seq.Indexed<unknown>;
2765
2766 /**
2767 * Returns a Seq "zipped" with the provided collections.
2768 *
2769 * Unlike `zip`, `zipAll` continues zipping until the longest collection is
2770 * exhausted. Missing values from shorter collections are filled with `undefined`.
2771 *
2772 * ```js
2773 * const a = Seq([ 1, 2 ]);
2774 * const b = Seq([ 3, 4, 5 ]);
2775 * const c = a.zipAll(b); // Seq [ [ 1, 3 ], [ 2, 4 ], [ undefined, 5 ] ]
2776 * ```
2777 */
2778 zipAll<U>(other: Collection<unknown, U>): Seq.Indexed<[T, U]>;
2779 zipAll<U, V>(
2780 other: Collection<unknown, U>,
2781 other2: Collection<unknown, V>
2782 ): Seq.Indexed<[T, U, V]>;
2783 zipAll(
2784 ...collections: Array<Collection<unknown, unknown>>
2785 ): Seq.Indexed<unknown>;
2786
2787 /**
2788 * Returns a Seq "zipped" with the provided collections by using a
2789 * custom `zipper` function.
2790 *
2791 * ```js
2792 * const a = Seq([ 1, 2, 3 ]);
2793 * const b = Seq([ 4, 5, 6 ]);
2794 * const c = a.zipWith((a, b) => a + b, b);
2795 * // Seq [ 5, 7, 9 ]
2796 * ```
2797 */
2798 zipWith<U, Z>(
2799 zipper: (value: T, otherValue: U) => Z,
2800 otherCollection: Collection<unknown, U>
2801 ): Seq.Indexed<Z>;
2802 zipWith<U, V, Z>(
2803 zipper: (value: T, otherValue: U, thirdValue: V) => Z,
2804 otherCollection: Collection<unknown, U>,
2805 thirdCollection: Collection<unknown, V>
2806 ): Seq.Indexed<Z>;
2807 zipWith<Z>(
2808 zipper: (...values: Array<unknown>) => Z,
2809 ...collections: Array<Collection<unknown, unknown>>
2810 ): Seq.Indexed<Z>;
2811
2812 [Symbol.iterator](): IterableIterator<T>;
2813 }
2814
2815 /**
2816 * `Seq` which represents a set of values.
2817 *
2818 * Because `Seq` are often lazy, `Seq.Set` does not provide the same guarantee
2819 * of value uniqueness as the concrete `Set`.
2820 */
2821 namespace Set {
2822 /**
2823 * Returns a Seq.Set of the provided values
2824 */
2825 function of<T>(...values: Array<T>): Seq.Set<T>;
2826 }
2827
2828 /**
2829 * Always returns a Seq.Set, discarding associated indices or keys.
2830 *
2831 * Note: `Seq.Set` is a conversion function and not a class, and does not
2832 * use the `new` keyword during construction.
2833 */
2834 function Set<T>(collection?: Iterable<T> | ArrayLike<T>): Seq.Set<T>;
2835
2836 interface Set<T> extends Seq<T, T>, Collection.Set<T> {
2837 /**
2838 * Deeply converts this Set Seq to equivalent native JavaScript Array.
2839 */
2840 toJS(): Array<DeepCopy<T>>;
2841
2842 /**
2843 * Shallowly converts this Set Seq to equivalent native JavaScript Array.
2844 */
2845 toJSON(): Array<T>;
2846
2847 /**
2848 * Shallowly converts this collection to an Array.
2849 */
2850 toArray(): Array<T>;
2851
2852 /**
2853 * Returns itself
2854 */
2855 toSeq(): this;
2856
2857 /**
2858 * Returns a new Seq with other collections concatenated to this one.
2859 *
2860 * All entries will be present in the resulting Seq, even if they
2861 * are duplicates.
2862 */
2863 concat<U>(...collections: Array<Iterable<U>>): Seq.Set<T | U>;
2864
2865 /**
2866 * Returns a new Seq.Set with values passed through a
2867 * `mapper` function.
2868 *
2869 * ```js
2870 * Seq.Set([ 1, 2 ]).map(x => 10 * x)
2871 * // Seq { 10, 20 }
2872 * ```
2873 *
2874 * Note: `map()` always returns a new instance, even if it produced the
2875 * same value at every step.
2876 */
2877 map<M>(
2878 mapper: (value: T, key: T, iter: this) => M,
2879 context?: unknown
2880 ): Seq.Set<M>;
2881
2882 /**
2883 * Flat-maps the Seq, returning a Seq of the same type.
2884 *
2885 * Similar to `seq.map(...).flatten(true)`.
2886 */
2887 flatMap<M>(
2888 mapper: (value: T, key: T, iter: this) => Iterable<M>,
2889 context?: unknown
2890 ): Seq.Set<M>;
2891
2892 /**
2893 * Returns a new Seq with only the values for which the `predicate`
2894 * function returns true.
2895 *
2896 * Note: `filter()` always returns a new instance, even if it results in
2897 * not filtering out any values.
2898 */
2899 filter<F extends T>(
2900 predicate: (value: T, key: T, iter: this) => value is F,
2901 context?: unknown
2902 ): Seq.Set<F>;
2903 filter(
2904 predicate: (value: T, key: T, iter: this) => unknown,
2905 context?: unknown
2906 ): this;
2907
2908 /**
2909 * Returns a new set Seq with the values for which the `predicate`
2910 * function returns false and another for which is returns true.
2911 */
2912 partition<F extends T, C>(
2913 predicate: (this: C, value: T, key: T, iter: this) => value is F,
2914 context?: C
2915 ): [Seq.Set<T>, Seq.Set<F>];
2916 partition<C>(
2917 predicate: (this: C, value: T, key: T, iter: this) => unknown,
2918 context?: C
2919 ): [this, this];
2920
2921 [Symbol.iterator](): IterableIterator<T>;
2922 }
2923 }
2924
2925 /**
2926 * Creates a Seq.
2927 *
2928 * Returns a particular kind of `Seq` based on the input.
2929 *
2930 * * If a `Seq`, that same `Seq`.
2931 * * If an `Collection`, a `Seq` of the same kind (Keyed, Indexed, or Set).
2932 * * If an Array-like, an `Seq.Indexed`.
2933 * * If an Iterable Object, an `Seq.Indexed`.
2934 * * If an Object, a `Seq.Keyed`.
2935 *
2936 * Note: An Iterator itself will be treated as an object, becoming a `Seq.Keyed`,
2937 * which is usually not what you want. You should turn your Iterator Object into
2938 * an iterable object by defining a Symbol.iterator (or @@iterator) method which
2939 * returns `this`.
2940 *
2941 * Note: `Seq` is a conversion function and not a class, and does not use the
2942 * `new` keyword during construction.
2943 */
2944 function Seq<S extends Seq<unknown, unknown>>(seq: S): S;
2945 function Seq<K, V>(collection: Collection.Keyed<K, V>): Seq.Keyed<K, V>;
2946 function Seq<T>(collection: Collection.Set<T>): Seq.Set<T>;
2947 function Seq<T>(
2948 collection: Collection.Indexed<T> | Iterable<T> | ArrayLike<T>
2949 ): Seq.Indexed<T>;
2950 function Seq<V>(obj: { [key: string]: V }): Seq.Keyed<string, V>;
2951 function Seq<K = unknown, V = unknown>(): Seq<K, V>;
2952
2953 interface Seq<K, V> extends Collection<K, V> {
2954 /**
2955 * Some Seqs can describe their size lazily. When this is the case,
2956 * size will be an integer. Otherwise it will be undefined.
2957 *
2958 * For example, Seqs returned from `map()` or `reverse()`
2959 * preserve the size of the original `Seq` while `filter()` does not.
2960 *
2961 * Note: `Range`, `Repeat` and `Seq`s made from `Array`s and `Object`s will
2962 * always have a size.
2963 */
2964 readonly size: number | undefined;
2965
2966 // Force evaluation
2967
2968 /**
2969 * Because Sequences are lazy and designed to be chained together, they do
2970 * not cache their results. For example, this map function is called a total
2971 * of 6 times, as each `join` iterates the Seq of three values.
2972 *
2973 * var squares = Seq([ 1, 2, 3 ]).map(x => x * x)
2974 * squares.join() + squares.join()
2975 *
2976 * If you know a `Seq` will be used multiple times, it may be more
2977 * efficient to first cache it in memory. Here, the map function is called
2978 * only 3 times.
2979 *
2980 * var squares = Seq([ 1, 2, 3 ]).map(x => x * x).cacheResult()
2981 * squares.join() + squares.join()
2982 *
2983 * Use this method judiciously, as it must fully evaluate a Seq which can be
2984 * a burden on memory and possibly performance.
2985 *
2986 * Note: after calling `cacheResult`, a Seq will always have a `size`.
2987 */
2988 cacheResult(): this;
2989
2990 // Sequence algorithms
2991
2992 /**
2993 * Returns a new Seq with values passed through a
2994 * `mapper` function.
2995 *
2996 * ```js
2997 * import { Seq } from 'immutable'
2998 * Seq([ 1, 2 ]).map(x => 10 * x)
2999 * // Seq [ 10, 20 ]
3000 * ```
3001 *
3002 * Note: `map()` always returns a new instance, even if it produced the same
3003 * value at every step.
3004 */
3005 map<M>(
3006 mapper: (value: V, key: K, iter: this) => M,
3007 context?: unknown
3008 ): Seq<K, M>;
3009
3010 /**
3011 * Returns a new Seq with values passed through a
3012 * `mapper` function.
3013 *
3014 * ```js
3015 * import { Seq } from 'immutable'
3016 * Seq([ 1, 2 ]).map(x => 10 * x)
3017 * // Seq [ 10, 20 ]
3018 * ```
3019 *
3020 * Note: `map()` always returns a new instance, even if it produced the same
3021 * value at every step.
3022 * Note: used only for sets.
3023 */
3024 map<M>(
3025 mapper: (value: V, key: K, iter: this) => M,
3026 context?: unknown
3027 ): Seq<M, M>;
3028
3029 /**
3030 * Flat-maps the Seq, returning a Seq of the same type.
3031 *
3032 * Similar to `seq.map(...).flatten(true)`.
3033 */
3034 flatMap<M>(
3035 mapper: (value: V, key: K, iter: this) => Iterable<M>,
3036 context?: unknown
3037 ): Seq<K, M>;
3038
3039 /**
3040 * Flat-maps the Seq, returning a Seq of the same type.
3041 *
3042 * Similar to `seq.map(...).flatten(true)`.
3043 * Note: Used only for sets.
3044 */
3045 flatMap<M>(
3046 mapper: (value: V, key: K, iter: this) => Iterable<M>,
3047 context?: unknown
3048 ): Seq<M, M>;
3049
3050 /**
3051 * Returns a new Seq with only the values for which the `predicate`
3052 * function returns true.
3053 *
3054 * Note: `filter()` always returns a new instance, even if it results in
3055 * not filtering out any values.
3056 */
3057 filter<F extends V>(
3058 predicate: (value: V, key: K, iter: this) => value is F,
3059 context?: unknown
3060 ): Seq<K, F>;
3061 filter(
3062 predicate: (value: V, key: K, iter: this) => unknown,
3063 context?: unknown
3064 ): this;
3065
3066 /**
3067 * Returns a new Seq with the values for which the `predicate` function
3068 * returns false and another for which is returns true.
3069 */
3070 partition<F extends V, C>(
3071 predicate: (this: C, value: V, key: K, iter: this) => value is F,
3072 context?: C
3073 ): [Seq<K, V>, Seq<K, F>];
3074 partition<C>(
3075 predicate: (this: C, value: V, key: K, iter: this) => unknown,
3076 context?: C
3077 ): [this, this];
3078
3079 /**
3080 * Returns a new Sequence of the same type with other values and
3081 * collection-like concatenated to this one.
3082 *
3083 * All entries will be present in the resulting Seq, even if they
3084 * have the same key.
3085 */
3086 concat(...valuesOrCollections: Array<unknown>): Seq<unknown, unknown>;
3087 }
3088
3089 /**
3090 * The `Collection` is a set of (key, value) entries which can be iterated, and
3091 * is the base class for all collections in `immutable`, allowing them to
3092 * make use of all the Collection methods (such as `map` and `filter`).
3093 *
3094 * Note: A collection is always iterated in the same order, however that order
3095 * may not always be well defined, as is the case for the `Map` and `Set`.
3096 *
3097 * Collection is the abstract base class for concrete data structures. It
3098 * cannot be constructed directly.
3099 *
3100 * Implementations should extend one of the subclasses, `Collection.Keyed`,
3101 * `Collection.Indexed`, or `Collection.Set`.
3102 */
3103 namespace Collection {
3104 /**
3105 * Keyed Collections have discrete keys tied to each value.
3106 *
3107 * When iterating `Collection.Keyed`, each iteration will yield a `[K, V]`
3108 * tuple, in other words, `Collection#entries` is the default iterator for
3109 * Keyed Collections.
3110 */
3111 namespace Keyed {}
3112
3113 /**
3114 * Creates a Collection.Keyed
3115 *
3116 * Similar to `Collection()`, however it expects collection-likes of [K, V]
3117 * tuples if not constructed from a Collection.Keyed or JS Object.
3118 *
3119 * Note: `Collection.Keyed` is a conversion function and not a class, and
3120 * does not use the `new` keyword during construction.
3121 */
3122 function Keyed<K, V>(collection?: Iterable<[K, V]>): Collection.Keyed<K, V>;
3123 function Keyed<V>(obj: { [key: string]: V }): Collection.Keyed<string, V>;
3124
3125 interface Keyed<K, V> extends Collection<K, V> {
3126 /**
3127 * Deeply converts this Keyed collection to equivalent native JavaScript Object.
3128 *
3129 * Converts keys to Strings.
3130 */
3131 toJS(): { [key in PropertyKey]: DeepCopy<V> };
3132
3133 /**
3134 * Shallowly converts this Keyed collection to equivalent native JavaScript Object.
3135 *
3136 * Converts keys to Strings.
3137 */
3138 toJSON(): { [key in PropertyKey]: V };
3139
3140 /**
3141 * Shallowly converts this collection to an Array.
3142 */
3143 toArray(): Array<[K, V]>;
3144
3145 /**
3146 * Returns Seq.Keyed.
3147 * @override
3148 */
3149 toSeq(): Seq.Keyed<K, V>;
3150
3151 // Sequence functions
3152
3153 /**
3154 * Returns a new Collection.Keyed of the same type where the keys and values
3155 * have been flipped.
3156 */
3157 flip(): Collection.Keyed<V, K>;
3158
3159 /**
3160 * Returns a new Collection with other collections concatenated to this one.
3161 */
3162 concat<KC, VC>(
3163 ...collections: Array<Iterable<[KC, VC]>>
3164 ): Collection.Keyed<K | KC, V | VC>;
3165 concat<C>(
3166 ...collections: Array<{ [key: string]: C }>
3167 ): Collection.Keyed<K | string, V | C>;
3168
3169 /**
3170 * Returns a new Collection.Keyed with values passed through a
3171 * `mapper` function.
3172 *
3173 * ```js
3174 * import { Collection } from 'immutable'
3175 * Collection.Keyed({ a: 1, b: 2 }).map(x => 10 * x)
3176 * // Seq { "a": 10, "b": 20 }
3177 * ```
3178 *
3179 * Note: `map()` always returns a new instance, even if it produced the
3180 * same value at every step.
3181 */
3182 map<M>(
3183 mapper: (value: V, key: K, iter: this) => M,
3184 context?: unknown
3185 ): Collection.Keyed<K, M>;
3186
3187 /**
3188 * Returns a new Collection.Keyed of the same type with keys passed through
3189 * a `mapper` function.
3190 *
3191 * Note: `mapKeys()` always returns a new instance, even if it produced
3192 * the same key at every step.
3193 */
3194 mapKeys<M>(
3195 mapper: (key: K, value: V, iter: this) => M,
3196 context?: unknown
3197 ): Collection.Keyed<M, V>;
3198
3199 /**
3200 * Returns a new Collection.Keyed of the same type with entries
3201 * ([key, value] tuples) passed through a `mapper` function.
3202 *
3203 * Note: `mapEntries()` always returns a new instance, even if it produced
3204 * the same entry at every step.
3205 *
3206 * If the mapper function returns `undefined`, then the entry will be filtered
3207 */
3208 mapEntries<KM, VM>(
3209 mapper: (
3210 entry: [K, V],
3211 index: number,
3212 iter: this
3213 ) => [KM, VM] | undefined,
3214 context?: unknown
3215 ): Collection.Keyed<KM, VM>;
3216
3217 /**
3218 * Flat-maps the Collection, returning a Collection of the same type.
3219 *
3220 * Similar to `collection.map(...).flatten(true)`.
3221 */
3222 flatMap<KM, VM>(
3223 mapper: (value: V, key: K, iter: this) => Iterable<[KM, VM]>,
3224 context?: unknown
3225 ): Collection.Keyed<KM, VM>;
3226
3227 /**
3228 * Returns a new Collection with only the values for which the `predicate`
3229 * function returns true.
3230 *
3231 * Note: `filter()` always returns a new instance, even if it results in
3232 * not filtering out any values.
3233 */
3234 filter<F extends V>(
3235 predicate: (value: V, key: K, iter: this) => value is F,
3236 context?: unknown
3237 ): Collection.Keyed<K, F>;
3238 filter(
3239 predicate: (value: V, key: K, iter: this) => unknown,
3240 context?: unknown
3241 ): this;
3242
3243 /**
3244 * Returns a new keyed Collection with the values for which the
3245 * `predicate` function returns false and another for which is returns
3246 * true.
3247 */
3248 partition<F extends V, C>(
3249 predicate: (this: C, value: V, key: K, iter: this) => value is F,
3250 context?: C
3251 ): [Collection.Keyed<K, V>, Collection.Keyed<K, F>];
3252 partition<C>(
3253 predicate: (this: C, value: V, key: K, iter: this) => unknown,
3254 context?: C
3255 ): [this, this];
3256
3257 [Symbol.iterator](): IterableIterator<[K, V]>;
3258 }
3259
3260 /**
3261 * Indexed Collections have incrementing numeric keys. They exhibit
3262 * slightly different behavior than `Collection.Keyed` for some methods in order
3263 * to better mirror the behavior of JavaScript's `Array`, and add methods
3264 * which do not make sense on non-indexed Collections such as `indexOf`.
3265 *
3266 * Unlike JavaScript arrays, `Collection.Indexed`s are always dense. "Unset"
3267 * indices and `undefined` indices are indistinguishable, and all indices from
3268 * 0 to `size` are visited when iterated.
3269 *
3270 * All Collection.Indexed methods return re-indexed Collections. In other words,
3271 * indices always start at 0 and increment until size. If you wish to
3272 * preserve indices, using them as keys, convert to a Collection.Keyed by
3273 * calling `toKeyedSeq`.
3274 */
3275 namespace Indexed {}
3276
3277 /**
3278 * Creates a new Collection.Indexed.
3279 *
3280 * Note: `Collection.Indexed` is a conversion function and not a class, and
3281 * does not use the `new` keyword during construction.
3282 */
3283 function Indexed<T>(
3284 collection?: Iterable<T> | ArrayLike<T>
3285 ): Collection.Indexed<T>;
3286
3287 interface Indexed<T> extends Collection<number, T>, OrderedCollection<T> {
3288 /**
3289 * Deeply converts this Indexed collection to equivalent native JavaScript Array.
3290 */
3291 toJS(): Array<DeepCopy<T>>;
3292
3293 /**
3294 * Shallowly converts this Indexed collection to equivalent native JavaScript Array.
3295 */
3296 toJSON(): Array<T>;
3297
3298 /**
3299 * Shallowly converts this collection to an Array.
3300 */
3301 toArray(): Array<T>;
3302
3303 // Reading values
3304
3305 /**
3306 * Returns the value associated with the provided index, or notSetValue if
3307 * the index is beyond the bounds of the Collection.
3308 *
3309 * `index` may be a negative number, which indexes back from the end of the
3310 * Collection. `s.get(-1)` gets the last item in the Collection.
3311 */
3312 get<NSV>(index: number, notSetValue: NSV): T | NSV;
3313 get(index: number): T | undefined;
3314
3315 // Conversion to Seq
3316
3317 /**
3318 * Returns Seq.Indexed.
3319 * @override
3320 */
3321 toSeq(): Seq.Indexed<T>;
3322
3323 /**
3324 * If this is a collection of [key, value] entry tuples, it will return a
3325 * Seq.Keyed of those entries.
3326 */
3327 fromEntrySeq(): Seq.Keyed<unknown, unknown>;
3328
3329 // Combination
3330
3331 /**
3332 * Returns a Collection of the same type with `separator` between each item
3333 * in this Collection.
3334 */
3335 interpose(separator: T): this;
3336
3337 /**
3338 * Returns a Collection of the same type with the provided `collections`
3339 * interleaved into this collection.
3340 *
3341 * The resulting Collection includes the first item from each, then the
3342 * second from each, etc.
3343 *
3344 * The shortest Collection stops interleave.
3345 *
3346 * Since `interleave()` re-indexes values, it produces a complete copy,
3347 * which has `O(N)` complexity.
3348 *
3349 * Note: `interleave` *cannot* be used in `withMutations`.
3350 */
3351 interleave(...collections: Array<Collection<unknown, T>>): this;
3352
3353 /**
3354 * Splice returns a new indexed Collection by replacing a region of this
3355 * Collection with new values. If values are not provided, it only skips the
3356 * region to be removed.
3357 *
3358 * `index` may be a negative number, which indexes back from the end of the
3359 * Collection. `s.splice(-2)` splices after the second to last item.
3360 *
3361 * Since `splice()` re-indexes values, it produces a complete copy, which
3362 * has `O(N)` complexity.
3363 *
3364 * Note: `splice` *cannot* be used in `withMutations`.
3365 */
3366 splice(index: number, removeNum: number, ...values: Array<T>): this;
3367
3368 /**
3369 * Returns a Collection of the same type "zipped" with the provided
3370 * collections.
3371 *
3372 * Like `zipWith`, but using the default `zipper`: creating an `Array`.
3373 */
3374 zip<U>(other: Collection<unknown, U>): Collection.Indexed<[T, U]>;
3375 zip<U, V>(
3376 other: Collection<unknown, U>,
3377 other2: Collection<unknown, V>
3378 ): Collection.Indexed<[T, U, V]>;
3379 zip(
3380 ...collections: Array<Collection<unknown, unknown>>
3381 ): Collection.Indexed<unknown>;
3382
3383 /**
3384 * Returns a Collection "zipped" with the provided collections.
3385 *
3386 * Unlike `zip`, `zipAll` continues zipping until the longest collection is
3387 * exhausted. Missing values from shorter collections are filled with `undefined`.
3388 *
3389 * ```js
3390 * const a = List([ 1, 2 ]);
3391 * const b = List([ 3, 4, 5 ]);
3392 * const c = a.zipAll(b); // List [ [ 1, 3 ], [ 2, 4 ], [ undefined, 5 ] ]
3393 * ```
3394 */
3395 zipAll<U>(other: Collection<unknown, U>): Collection.Indexed<[T, U]>;
3396 zipAll<U, V>(
3397 other: Collection<unknown, U>,
3398 other2: Collection<unknown, V>
3399 ): Collection.Indexed<[T, U, V]>;
3400 zipAll(
3401 ...collections: Array<Collection<unknown, unknown>>
3402 ): Collection.Indexed<unknown>;
3403
3404 /**
3405 * Returns a Collection of the same type "zipped" with the provided
3406 * collections by using a custom `zipper` function.
3407 */
3408 zipWith<U, Z>(
3409 zipper: (value: T, otherValue: U) => Z,
3410 otherCollection: Collection<unknown, U>
3411 ): Collection.Indexed<Z>;
3412 zipWith<U, V, Z>(
3413 zipper: (value: T, otherValue: U, thirdValue: V) => Z,
3414 otherCollection: Collection<unknown, U>,
3415 thirdCollection: Collection<unknown, V>
3416 ): Collection.Indexed<Z>;
3417 zipWith<Z>(
3418 zipper: (...values: Array<unknown>) => Z,
3419 ...collections: Array<Collection<unknown, unknown>>
3420 ): Collection.Indexed<Z>;
3421
3422 // Search for value
3423
3424 /**
3425 * Returns the first index at which a given value can be found in the
3426 * Collection, or -1 if it is not present.
3427 */
3428 indexOf(searchValue: T): number;
3429
3430 /**
3431 * Returns the last index at which a given value can be found in the
3432 * Collection, or -1 if it is not present.
3433 */
3434 lastIndexOf(searchValue: T): number;
3435
3436 /**
3437 * Returns the first index in the Collection where a value satisfies the
3438 * provided predicate function. Otherwise -1 is returned.
3439 */
3440 findIndex(
3441 predicate: (value: T, index: number, iter: this) => boolean,
3442 context?: unknown
3443 ): number;
3444
3445 /**
3446 * Returns the last index in the Collection where a value satisfies the
3447 * provided predicate function. Otherwise -1 is returned.
3448 */
3449 findLastIndex(
3450 predicate: (value: T, index: number, iter: this) => boolean,
3451 context?: unknown
3452 ): number;
3453
3454 // Sequence algorithms
3455
3456 /**
3457 * Returns a new Collection with other collections concatenated to this one.
3458 */
3459 concat<C>(
3460 ...valuesOrCollections: Array<Iterable<C> | C>
3461 ): Collection.Indexed<T | C>;
3462
3463 /**
3464 * Returns a new Collection.Indexed with values passed through a
3465 * `mapper` function.
3466 *
3467 * ```js
3468 * import { Collection } from 'immutable'
3469 * Collection.Indexed([1,2]).map(x => 10 * x)
3470 * // Seq [ 1, 2 ]
3471 * ```
3472 *
3473 * Note: `map()` always returns a new instance, even if it produced the
3474 * same value at every step.
3475 */
3476 map<M>(
3477 mapper: (value: T, key: number, iter: this) => M,
3478 context?: unknown
3479 ): Collection.Indexed<M>;
3480
3481 /**
3482 * Flat-maps the Collection, returning a Collection of the same type.
3483 *
3484 * Similar to `collection.map(...).flatten(true)`.
3485 */
3486 flatMap<M>(
3487 mapper: (value: T, key: number, iter: this) => Iterable<M>,
3488 context?: unknown
3489 ): Collection.Indexed<M>;
3490
3491 /**
3492 * Returns a new Collection with only the values for which the `predicate`
3493 * function returns true.
3494 *
3495 * Note: `filter()` always returns a new instance, even if it results in
3496 * not filtering out any values.
3497 */
3498 filter<F extends T>(
3499 predicate: (value: T, index: number, iter: this) => value is F,
3500 context?: unknown
3501 ): Collection.Indexed<F>;
3502 filter(
3503 predicate: (value: T, index: number, iter: this) => unknown,
3504 context?: unknown
3505 ): this;
3506
3507 /**
3508 * Returns a new indexed Collection with the values for which the
3509 * `predicate` function returns false and another for which is returns
3510 * true.
3511 */
3512 partition<F extends T, C>(
3513 predicate: (this: C, value: T, index: number, iter: this) => value is F,
3514 context?: C
3515 ): [Collection.Indexed<T>, Collection.Indexed<F>];
3516 partition<C>(
3517 predicate: (this: C, value: T, index: number, iter: this) => unknown,
3518 context?: C
3519 ): [this, this];
3520
3521 [Symbol.iterator](): IterableIterator<T>;
3522 }
3523
3524 /**
3525 * Set Collections only represent values. They have no associated keys or
3526 * indices. Duplicate values are possible in the lazy `Seq.Set`s, however
3527 * the concrete `Set` Collection does not allow duplicate values.
3528 *
3529 * Collection methods on Collection.Set such as `map` and `forEach` will provide
3530 * the value as both the first and second arguments to the provided function.
3531 *
3532 * ```js
3533 * import { Collection } from 'immutable'
3534 * const seq = Collection.Set([ 'A', 'B', 'C' ])
3535 * // Seq { "A", "B", "C" }
3536 * seq.forEach((v, k) =>
3537 * assert.equal(v, k)
3538 * )
3539 * ```
3540 */
3541 namespace Set {}
3542
3543 /**
3544 * Similar to `Collection()`, but always returns a Collection.Set.
3545 *
3546 * Note: `Collection.Set` is a factory function and not a class, and does
3547 * not use the `new` keyword during construction.
3548 */
3549 function Set<T>(collection?: Iterable<T> | ArrayLike<T>): Collection.Set<T>;
3550
3551 interface Set<T> extends Collection<T, T> {
3552 /**
3553 * Deeply converts this Set collection to equivalent native JavaScript Array.
3554 */
3555 toJS(): Array<DeepCopy<T>>;
3556
3557 /**
3558 * Shallowly converts this Set collection to equivalent native JavaScript Array.
3559 */
3560 toJSON(): Array<T>;
3561
3562 /**
3563 * Shallowly converts this collection to an Array.
3564 */
3565 toArray(): Array<T>;
3566
3567 /**
3568 * Returns Seq.Set.
3569 * @override
3570 */
3571 toSeq(): Seq.Set<T>;
3572
3573 // Sequence algorithms
3574
3575 /**
3576 * Returns a new Collection with other collections concatenated to this one.
3577 */
3578 concat<U>(...collections: Array<Iterable<U>>): Collection.Set<T | U>;
3579
3580 /**
3581 * Returns a new Collection.Set with values passed through a
3582 * `mapper` function.
3583 *
3584 * ```
3585 * Collection.Set([ 1, 2 ]).map(x => 10 * x)
3586 * // Seq { 1, 2 }
3587 * ```
3588 *
3589 * Note: `map()` always returns a new instance, even if it produced the
3590 * same value at every step.
3591 */
3592 map<M>(
3593 mapper: (value: T, key: T, iter: this) => M,
3594 context?: unknown
3595 ): Collection.Set<M>;
3596
3597 /**
3598 * Flat-maps the Collection, returning a Collection of the same type.
3599 *
3600 * Similar to `collection.map(...).flatten(true)`.
3601 */
3602 flatMap<M>(
3603 mapper: (value: T, key: T, iter: this) => Iterable<M>,
3604 context?: unknown
3605 ): Collection.Set<M>;
3606
3607 /**
3608 * Returns a new Collection with only the values for which the `predicate`
3609 * function returns true.
3610 *
3611 * Note: `filter()` always returns a new instance, even if it results in
3612 * not filtering out any values.
3613 */
3614 filter<F extends T>(
3615 predicate: (value: T, key: T, iter: this) => value is F,
3616 context?: unknown
3617 ): Collection.Set<F>;
3618 filter(
3619 predicate: (value: T, key: T, iter: this) => unknown,
3620 context?: unknown
3621 ): this;
3622
3623 /**
3624 * Returns a new set Collection with the values for which the
3625 * `predicate` function returns false and another for which is returns
3626 * true.
3627 */
3628 partition<F extends T, C>(
3629 predicate: (this: C, value: T, key: T, iter: this) => value is F,
3630 context?: C
3631 ): [Collection.Set<T>, Collection.Set<F>];
3632 partition<C>(
3633 predicate: (this: C, value: T, key: T, iter: this) => unknown,
3634 context?: C
3635 ): [this, this];
3636
3637 [Symbol.iterator](): IterableIterator<T>;
3638 }
3639 }
3640
3641 /**
3642 * Creates a Collection.
3643 *
3644 * The type of Collection created is based on the input.
3645 *
3646 * * If an `Collection`, that same `Collection`.
3647 * * If an Array-like, an `Collection.Indexed`.
3648 * * If an Object with an Iterator defined, an `Collection.Indexed`.
3649 * * If an Object, an `Collection.Keyed`.
3650 *
3651 * This methods forces the conversion of Objects and Strings to Collections.
3652 * If you want to ensure that a Collection of one item is returned, use
3653 * `Seq.of`.
3654 *
3655 * Note: An Iterator itself will be treated as an object, becoming a `Seq.Keyed`,
3656 * which is usually not what you want. You should turn your Iterator Object into
3657 * an iterable object by defining a Symbol.iterator (or @@iterator) method which
3658 * returns `this`.
3659 *
3660 * Note: `Collection` is a conversion function and not a class, and does not
3661 * use the `new` keyword during construction.
3662 */
3663 function Collection<I extends Collection<unknown, unknown>>(collection: I): I;
3664 function Collection<T>(
3665 collection: Iterable<T> | ArrayLike<T>
3666 ): Collection.Indexed<T>;
3667 function Collection<V>(obj: {
3668 [key: string]: V;
3669 }): Collection.Keyed<string, V>;
3670 function Collection<K = unknown, V = unknown>(): Collection<K, V>;
3671
3672 interface Collection<K, V> extends ValueObject {
3673 // Value equality
3674
3675 /**
3676 * True if this and the other Collection have value equality, as defined
3677 * by `Immutable.is()`.
3678 *
3679 * Note: This is equivalent to `Immutable.is(this, other)`, but provided to
3680 * allow for chained expressions.
3681 */
3682 equals(other: unknown): boolean;
3683
3684 /**
3685 * Computes and returns the hashed identity for this Collection.
3686 *
3687 * The `hashCode` of a Collection is used to determine potential equality,
3688 * and is used when adding this to a `Set` or as a key in a `Map`, enabling
3689 * lookup via a different instance.
3690 *
3691 * If two values have the same `hashCode`, they are [not guaranteed
3692 * to be equal][Hash Collision]. If two values have different `hashCode`s,
3693 * they must not be equal.
3694 *
3695 * [Hash Collision]: https://en.wikipedia.org/wiki/Collision_(computer_science)
3696 */
3697 hashCode(): number;
3698
3699 // Reading values
3700
3701 /**
3702 * Returns the value associated with the provided key, or notSetValue if
3703 * the Collection does not contain this key.
3704 *
3705 * Note: it is possible a key may be associated with an `undefined` value,
3706 * so if `notSetValue` is not provided and this method returns `undefined`,
3707 * that does not guarantee the key was not found.
3708 */
3709 get<NSV>(key: K, notSetValue: NSV): V | NSV;
3710 get(key: K): V | undefined;
3711
3712 /**
3713 * True if a key exists within this `Collection`, using `Immutable.is`
3714 * to determine equality
3715 */
3716 has(key: K): boolean;
3717
3718 /**
3719 * True if a value exists within this `Collection`, using `Immutable.is`
3720 * to determine equality
3721 * @alias contains
3722 */
3723 includes(value: V): boolean;
3724 contains(value: V): boolean;
3725
3726 /**
3727 * In case the `Collection` is not empty returns the first element of the
3728 * `Collection`.
3729 * In case the `Collection` is empty returns the optional default
3730 * value if provided, if no default value is provided returns undefined.
3731 */
3732 first<NSV>(notSetValue: NSV): V | NSV;
3733 first(): V | undefined;
3734
3735 /**
3736 * In case the `Collection` is not empty returns the last element of the
3737 * `Collection`.
3738 * In case the `Collection` is empty returns the optional default
3739 * value if provided, if no default value is provided returns undefined.
3740 */
3741 last<NSV>(notSetValue: NSV): V | NSV;
3742 last(): V | undefined;
3743
3744 // Reading deep values
3745
3746 /**
3747 * Returns the value found by following a path of keys or indices through
3748 * nested Collections.
3749 *
3750 * Plain JavaScript Object or Arrays may be nested within an Immutable.js
3751 * Collection, and getIn() can access those values as well:
3752 */
3753 getIn(searchKeyPath: Iterable<unknown>, notSetValue?: unknown): unknown;
3754
3755 /**
3756 * True if the result of following a path of keys or indices through nested
3757 * Collections results in a set value.
3758 */
3759 hasIn(searchKeyPath: Iterable<unknown>): boolean;
3760
3761 // Persistent changes
3762
3763 /**
3764 * This can be very useful as a way to "chain" a normal function into a
3765 * sequence of methods. RxJS calls this "let" and lodash calls it "thru".
3766 *
3767 * For example, to sum a Seq after mapping and filtering:
3768 */
3769 update<R>(updater: (value: this) => R): R;
3770
3771 // Conversion to JavaScript types
3772
3773 /**
3774 * Deeply converts this Collection to equivalent native JavaScript Array or Object.
3775 *
3776 * `Collection.Indexed`, and `Collection.Set` become `Array`, while
3777 * `Collection.Keyed` become `Object`, converting keys to Strings.
3778 */
3779 toJS(): Array<DeepCopy<V>> | { [key in PropertyKey]: DeepCopy<V> };
3780
3781 /**
3782 * Shallowly converts this Collection to equivalent native JavaScript Array or Object.
3783 *
3784 * `Collection.Indexed`, and `Collection.Set` become `Array`, while
3785 * `Collection.Keyed` become `Object`, converting keys to Strings.
3786 */
3787 toJSON(): Array<V> | { [key in PropertyKey]: V };
3788
3789 /**
3790 * Shallowly converts this collection to an Array.
3791 *
3792 * `Collection.Indexed`, and `Collection.Set` produce an Array of values.
3793 * `Collection.Keyed` produce an Array of [key, value] tuples.
3794 */
3795 toArray(): Array<V> | Array<[K, V]>;
3796
3797 /**
3798 * Shallowly converts this Collection to an Object.
3799 *
3800 * Converts keys to Strings.
3801 */
3802 toObject(): { [key: string]: V };
3803
3804 // Conversion to Collections
3805
3806 /**
3807 * Converts this Collection to a Map, Throws if keys are not hashable.
3808 *
3809 * Note: This is equivalent to `Map(this.toKeyedSeq())`, but provided
3810 * for convenience and to allow for chained expressions.
3811 */
3812 toMap(): Map<K, V>;
3813
3814 /**
3815 * Converts this Collection to a Map, maintaining the order of iteration.
3816 *
3817 * Note: This is equivalent to `OrderedMap(this.toKeyedSeq())`, but
3818 * provided for convenience and to allow for chained expressions.
3819 */
3820 toOrderedMap(): OrderedMap<K, V>;
3821
3822 /**
3823 * Converts this Collection to a Set, discarding keys. Throws if values
3824 * are not hashable.
3825 *
3826 * Note: This is equivalent to `Set(this)`, but provided to allow for
3827 * chained expressions.
3828 */
3829 toSet(): Set<V>;
3830
3831 /**
3832 * Converts this Collection to a Set, maintaining the order of iteration and
3833 * discarding keys.
3834 *
3835 * Note: This is equivalent to `OrderedSet(this.valueSeq())`, but provided
3836 * for convenience and to allow for chained expressions.
3837 */
3838 toOrderedSet(): OrderedSet<V>;
3839
3840 /**
3841 * Converts this Collection to a List, discarding keys.
3842 *
3843 * This is similar to `List(collection)`, but provided to allow for chained
3844 * expressions. However, when called on `Map` or other keyed collections,
3845 * `collection.toList()` discards the keys and creates a list of only the
3846 * values, whereas `List(collection)` creates a list of entry tuples.
3847 */
3848 toList(): List<V>;
3849
3850 /**
3851 * Converts this Collection to a Stack, discarding keys. Throws if values
3852 * are not hashable.
3853 *
3854 * Note: This is equivalent to `Stack(this)`, but provided to allow for
3855 * chained expressions.
3856 */
3857 toStack(): Stack<V>;
3858
3859 // Conversion to Seq
3860
3861 /**
3862 * Converts this Collection to a Seq of the same kind (indexed,
3863 * keyed, or set).
3864 */
3865 toSeq(): Seq<K, V>;
3866
3867 /**
3868 * Returns a Seq.Keyed from this Collection where indices are treated as keys.
3869 *
3870 * This is useful if you want to operate on an
3871 * Collection.Indexed and preserve the [index, value] pairs.
3872 *
3873 * The returned Seq will have identical iteration order as
3874 * this Collection.
3875 */
3876 toKeyedSeq(): Seq.Keyed<K, V>;
3877
3878 /**
3879 * Returns an Seq.Indexed of the values of this Collection, discarding keys.
3880 */
3881 toIndexedSeq(): Seq.Indexed<V>;
3882
3883 /**
3884 * Returns a Seq.Set of the values of this Collection, discarding keys.
3885 */
3886 toSetSeq(): Seq.Set<V>;
3887
3888 // Iterators
3889
3890 /**
3891 * An iterator of this `Collection`'s keys.
3892 *
3893 * Note: this will return an ES6 iterator which does not support
3894 * Immutable.js sequence algorithms. Use `keySeq` instead, if this is
3895 * what you want.
3896 */
3897 keys(): IterableIterator<K>;
3898
3899 /**
3900 * An iterator of this `Collection`'s values.
3901 *
3902 * Note: this will return an ES6 iterator which does not support
3903 * Immutable.js sequence algorithms. Use `valueSeq` instead, if this is
3904 * what you want.
3905 */
3906 values(): IterableIterator<V>;
3907
3908 /**
3909 * An iterator of this `Collection`'s entries as `[ key, value ]` tuples.
3910 *
3911 * Note: this will return an ES6 iterator which does not support
3912 * Immutable.js sequence algorithms. Use `entrySeq` instead, if this is
3913 * what you want.
3914 */
3915 entries(): IterableIterator<[K, V]>;
3916
3917 [Symbol.iterator](): IterableIterator<unknown>;
3918
3919 // Collections (Seq)
3920
3921 /**
3922 * Returns a new Seq.Indexed of the keys of this Collection,
3923 * discarding values.
3924 */
3925 keySeq(): Seq.Indexed<K>;
3926
3927 /**
3928 * Returns an Seq.Indexed of the values of this Collection, discarding keys.
3929 */
3930 valueSeq(): Seq.Indexed<V>;
3931
3932 /**
3933 * Returns a new Seq.Indexed of [key, value] tuples.
3934 */
3935 entrySeq(): Seq.Indexed<[K, V]>;
3936
3937 // Sequence algorithms
3938
3939 /**
3940 * Returns a new Collection of the same type with values passed through a
3941 * `mapper` function.
3942 *
3943 * Note: `map()` always returns a new instance, even if it produced the same
3944 * value at every step.
3945 */
3946 map<M>(
3947 mapper: (value: V, key: K, iter: this) => M,
3948 context?: unknown
3949 ): Collection<K, M>;
3950
3951 /**
3952 * Note: used only for sets, which return Collection<M, M> but are otherwise
3953 * identical to normal `map()`.
3954 *
3955 * @ignore
3956 */
3957 map(...args: Array<never>): unknown;
3958
3959 /**
3960 * Returns a new Collection of the same type with only the entries for which
3961 * the `predicate` function returns true.
3962 *
3963 * Note: `filter()` always returns a new instance, even if it results in
3964 * not filtering out any values.
3965 */
3966 filter<F extends V>(
3967 predicate: (value: V, key: K, iter: this) => value is F,
3968 context?: unknown
3969 ): Collection<K, F>;
3970 filter(
3971 predicate: (value: V, key: K, iter: this) => unknown,
3972 context?: unknown
3973 ): this;
3974
3975 /**
3976 * Returns a new Collection of the same type with only the entries for which
3977 * the `predicate` function returns false.
3978 *
3979 * Note: `filterNot()` always returns a new instance, even if it results in
3980 * not filtering out any values.
3981 */
3982 filterNot(
3983 predicate: (value: V, key: K, iter: this) => boolean,
3984 context?: unknown
3985 ): this;
3986
3987 /**
3988 * Returns a new Collection with the values for which the `predicate`
3989 * function returns false and another for which is returns true.
3990 */
3991 partition<F extends V, C>(
3992 predicate: (this: C, value: V, key: K, iter: this) => value is F,
3993 context?: C
3994 ): [Collection<K, V>, Collection<K, F>];
3995 partition<C>(
3996 predicate: (this: C, value: V, key: K, iter: this) => unknown,
3997 context?: C
3998 ): [this, this];
3999
4000 /**
4001 * Returns a new Collection of the same type in reverse order.
4002 */
4003 reverse(): this;
4004
4005 /**
4006 * Returns a new Collection of the same type which includes the same entries,
4007 * stably sorted by using a `comparator`.
4008 *
4009 * If a `comparator` is not provided, a default comparator uses `<` and `>`.
4010 *
4011 * `comparator(valueA, valueB)`:
4012 *
4013 * * Returns `0` if the elements should not be swapped.
4014 * * Returns `-1` (or any negative number) if `valueA` comes before `valueB`
4015 * * Returns `1` (or any positive number) if `valueA` comes after `valueB`
4016 * * Alternatively, can return a value of the `PairSorting` enum type
4017 * * Is pure, i.e. it must always return the same value for the same pair
4018 * of values.
4019 *
4020 * When sorting collections which have no defined order, their ordered
4021 * equivalents will be returned. e.g. `map.sort()` returns OrderedMap.
4022 *
4023 * Note: `sort()` Always returns a new instance, even if the original was
4024 * already sorted.
4025 *
4026 * Note: This is always an eager operation.
4027 */
4028 sort(comparator?: Comparator<V>): this;
4029
4030 /**
4031 * Like `sort`, but also accepts a `comparatorValueMapper` which allows for
4032 * sorting by more sophisticated means:
4033 *
4034 * Note: `sortBy()` Always returns a new instance, even if the original was
4035 * already sorted.
4036 *
4037 * Note: This is always an eager operation.
4038 */
4039 sortBy<C>(
4040 comparatorValueMapper: (value: V, key: K, iter: this) => C,
4041 comparator?: Comparator<C>
4042 ): this;
4043
4044 /**
4045 * Returns a `Map` of `Collection`, grouped by the return
4046 * value of the `grouper` function.
4047 *
4048 * Note: This is always an eager operation.
4049 */
4050 groupBy<G>(
4051 grouper: (value: V, key: K, iter: this) => G,
4052 context?: unknown
4053 ): Map<G, this>;
4054
4055 // Side effects
4056
4057 /**
4058 * The `sideEffect` is executed for every entry in the Collection.
4059 *
4060 * Unlike `Array#forEach`, if any call of `sideEffect` returns
4061 * `false`, the iteration will stop. Returns the number of entries iterated
4062 * (including the last iteration which returned false).
4063 */
4064 forEach(
4065 sideEffect: (value: V, key: K, iter: this) => unknown,
4066 context?: unknown
4067 ): number;
4068
4069 // Creating subsets
4070
4071 /**
4072 * Returns a new Collection of the same type representing a portion of this
4073 * Collection from start up to but not including end.
4074 *
4075 * If begin is negative, it is offset from the end of the Collection. e.g.
4076 * `slice(-2)` returns a Collection of the last two entries. If it is not
4077 * provided the new Collection will begin at the beginning of this Collection.
4078 *
4079 * If end is negative, it is offset from the end of the Collection. e.g.
4080 * `slice(0, -1)` returns a Collection of everything but the last entry. If
4081 * it is not provided, the new Collection will continue through the end of
4082 * this Collection.
4083 *
4084 * If the requested slice is equivalent to the current Collection, then it
4085 * will return itself.
4086 */
4087 slice(begin?: number, end?: number): this;
4088
4089 /**
4090 * Returns a new Collection of the same type containing all entries except
4091 * the first.
4092 */
4093 rest(): this;
4094
4095 /**
4096 * Returns a new Collection of the same type containing all entries except
4097 * the last.
4098 */
4099 butLast(): this;
4100
4101 /**
4102 * Returns a new Collection of the same type which excludes the first `amount`
4103 * entries from this Collection.
4104 */
4105 skip(amount: number): this;
4106
4107 /**
4108 * Returns a new Collection of the same type which excludes the last `amount`
4109 * entries from this Collection.
4110 */
4111 skipLast(amount: number): this;
4112
4113 /**
4114 * Returns a new Collection of the same type which includes entries starting
4115 * from when `predicate` first returns false.
4116 */
4117 skipWhile(
4118 predicate: (value: V, key: K, iter: this) => boolean,
4119 context?: unknown
4120 ): this;
4121
4122 /**
4123 * Returns a new Collection of the same type which includes entries starting
4124 * from when `predicate` first returns true.
4125 */
4126 skipUntil(
4127 predicate: (value: V, key: K, iter: this) => boolean,
4128 context?: unknown
4129 ): this;
4130
4131 /**
4132 * Returns a new Collection of the same type which includes the first `amount`
4133 * entries from this Collection.
4134 */
4135 take(amount: number): this;
4136
4137 /**
4138 * Returns a new Collection of the same type which includes the last `amount`
4139 * entries from this Collection.
4140 */
4141 takeLast(amount: number): this;
4142
4143 /**
4144 * Returns a new Collection of the same type which includes entries from this
4145 * Collection as long as the `predicate` returns true.
4146 */
4147 takeWhile(
4148 predicate: (value: V, key: K, iter: this) => boolean,
4149 context?: unknown
4150 ): this;
4151
4152 /**
4153 * Returns a new Collection of the same type which includes entries from this
4154 * Collection as long as the `predicate` returns false.
4155 */
4156 takeUntil(
4157 predicate: (value: V, key: K, iter: this) => boolean,
4158 context?: unknown
4159 ): this;
4160
4161 // Combination
4162
4163 /**
4164 * Returns a new Collection of the same type with other values and
4165 * collection-like concatenated to this one.
4166 *
4167 * For Seqs, all entries will be present in the resulting Seq, even if they
4168 * have the same key.
4169 */
4170 concat(
4171 ...valuesOrCollections: Array<unknown>
4172 ): Collection<unknown, unknown>;
4173
4174 /**
4175 * Flattens nested Collections.
4176 *
4177 * Will deeply flatten the Collection by default, returning a Collection of the
4178 * same type, but a `depth` can be provided in the form of a number or
4179 * boolean (where true means to shallowly flatten one level). A depth of 0
4180 * (or shallow: false) will deeply flatten.
4181 *
4182 * Flattens only others Collection, not Arrays or Objects.
4183 *
4184 * Note: `flatten(true)` operates on Collection<unknown, Collection<K, V>> and
4185 * returns Collection<K, V>
4186 */
4187 flatten(depth?: number): Collection<unknown, unknown>;
4188 flatten(shallow?: boolean): Collection<unknown, unknown>;
4189
4190 /**
4191 * Flat-maps the Collection, returning a Collection of the same type.
4192 *
4193 * Similar to `collection.map(...).flatten(true)`.
4194 */
4195 flatMap<M>(
4196 mapper: (value: V, key: K, iter: this) => Iterable<M>,
4197 context?: unknown
4198 ): Collection<K, M>;
4199
4200 /**
4201 * Flat-maps the Collection, returning a Collection of the same type.
4202 *
4203 * Similar to `collection.map(...).flatten(true)`.
4204 * Used for Dictionaries only.
4205 */
4206 flatMap<KM, VM>(
4207 mapper: (value: V, key: K, iter: this) => Iterable<[KM, VM]>,
4208 context?: unknown
4209 ): Collection<KM, VM>;
4210
4211 // Reducing a value
4212
4213 /**
4214 * Reduces the Collection to a value by calling the `reducer` for every entry
4215 * in the Collection and passing along the reduced value.
4216 *
4217 * If `initialReduction` is not provided, the first item in the
4218 * Collection will be used.
4219 *
4220 * @see `Array#reduce`.
4221 */
4222 reduce(reducer: (reduction: V, value: V, key: K, iter: this) => V): V;
4223 reduce<R>(
4224 reducer: (reduction: R, value: V, key: K, iter: this) => R,
4225 initialReduction: R,
4226 context?: unknown
4227 ): R;
4228 reduce<R>(
4229 reducer: (reduction: V | R, value: V, key: K, iter: this) => R
4230 ): R;
4231
4232 /**
4233 * Reduces the Collection in reverse (from the right side).
4234 *
4235 * Note: Similar to this.reverse().reduce(), and provided for parity
4236 * with `Array#reduceRight`.
4237 */
4238 reduceRight(reducer: (reduction: V, value: V, key: K, iter: this) => V): V;
4239 reduceRight<R>(
4240 reducer: (reduction: R, value: V, key: K, iter: this) => R,
4241 initialReduction: R,
4242 context?: unknown
4243 ): R;
4244 reduceRight<R>(
4245 reducer: (reduction: V | R, value: V, key: K, iter: this) => R
4246 ): R;
4247
4248 /**
4249 * True if `predicate` returns true for all entries in the Collection.
4250 */
4251 every(
4252 predicate: (value: V, key: K, iter: this) => boolean,
4253 context?: unknown
4254 ): boolean;
4255
4256 /**
4257 * True if `predicate` returns true for any entry in the Collection.
4258 */
4259 some(
4260 predicate: (value: V, key: K, iter: this) => boolean,
4261 context?: unknown
4262 ): boolean;
4263
4264 /**
4265 * Joins values together as a string, inserting a separator between each.
4266 * The default separator is `","`.
4267 */
4268 join(separator?: string): string;
4269
4270 /**
4271 * Returns true if this Collection includes no values.
4272 *
4273 * For some lazy `Seq`, `isEmpty` might need to iterate to determine
4274 * emptiness. At most one iteration will occur.
4275 */
4276 isEmpty(): boolean;
4277
4278 /**
4279 * Returns the size of this Collection.
4280 *
4281 * Regardless of if this Collection can describe its size lazily (some Seqs
4282 * cannot), this method will always return the correct size. E.g. it
4283 * evaluates a lazy `Seq` if necessary.
4284 *
4285 * If `predicate` is provided, then this returns the count of entries in the
4286 * Collection for which the `predicate` returns true.
4287 */
4288 count(): number;
4289 count(
4290 predicate: (value: V, key: K, iter: this) => boolean,
4291 context?: unknown
4292 ): number;
4293
4294 /**
4295 * Returns a `Seq.Keyed` of counts, grouped by the return value of
4296 * the `grouper` function.
4297 *
4298 * Note: This is not a lazy operation.
4299 */
4300 countBy<G>(
4301 grouper: (value: V, key: K, iter: this) => G,
4302 context?: unknown
4303 ): Map<G, number>;
4304
4305 // Search for value
4306
4307 /**
4308 * Returns the first value for which the `predicate` returns true.
4309 */
4310 find(
4311 predicate: (value: V, key: K, iter: this) => boolean,
4312 context?: unknown,
4313 notSetValue?: V
4314 ): V | undefined;
4315
4316 /**
4317 * Returns the last value for which the `predicate` returns true.
4318 *
4319 * Note: `predicate` will be called for each entry in reverse.
4320 */
4321 findLast(
4322 predicate: (value: V, key: K, iter: this) => boolean,
4323 context?: unknown,
4324 notSetValue?: V
4325 ): V | undefined;
4326
4327 /**
4328 * Returns the first [key, value] entry for which the `predicate` returns true.
4329 */
4330 findEntry(
4331 predicate: (value: V, key: K, iter: this) => boolean,
4332 context?: unknown,
4333 notSetValue?: V
4334 ): [K, V] | undefined;
4335
4336 /**
4337 * Returns the last [key, value] entry for which the `predicate`
4338 * returns true.
4339 *
4340 * Note: `predicate` will be called for each entry in reverse.
4341 */
4342 findLastEntry(
4343 predicate: (value: V, key: K, iter: this) => boolean,
4344 context?: unknown,
4345 notSetValue?: V
4346 ): [K, V] | undefined;
4347
4348 /**
4349 * Returns the key for which the `predicate` returns true.
4350 */
4351 findKey(
4352 predicate: (value: V, key: K, iter: this) => boolean,
4353 context?: unknown
4354 ): K | undefined;
4355
4356 /**
4357 * Returns the last key for which the `predicate` returns true.
4358 *
4359 * Note: `predicate` will be called for each entry in reverse.
4360 */
4361 findLastKey(
4362 predicate: (value: V, key: K, iter: this) => boolean,
4363 context?: unknown
4364 ): K | undefined;
4365
4366 /**
4367 * Returns the key associated with the search value, or undefined.
4368 */
4369 keyOf(searchValue: V): K | undefined;
4370
4371 /**
4372 * Returns the last key associated with the search value, or undefined.
4373 */
4374 lastKeyOf(searchValue: V): K | undefined;
4375
4376 /**
4377 * Returns the maximum value in this collection. If any values are
4378 * comparatively equivalent, the first one found will be returned.
4379 *
4380 * The `comparator` is used in the same way as `Collection#sort`. If it is not
4381 * provided, the default comparator is `>`.
4382 *
4383 * When two values are considered equivalent, the first encountered will be
4384 * returned. Otherwise, `max` will operate independent of the order of input
4385 * as long as the comparator is commutative. The default comparator `>` is
4386 * commutative *only* when types do not differ.
4387 *
4388 * If `comparator` returns 0 and either value is NaN, undefined, or null,
4389 * that value will be returned.
4390 */
4391 max(comparator?: Comparator<V>): V | undefined;
4392
4393 /**
4394 * Like `max`, but also accepts a `comparatorValueMapper` which allows for
4395 * comparing by more sophisticated means:
4396 */
4397 maxBy<C>(
4398 comparatorValueMapper: (value: V, key: K, iter: this) => C,
4399 comparator?: Comparator<C>
4400 ): V | undefined;
4401
4402 /**
4403 * Returns the minimum value in this collection. If any values are
4404 * comparatively equivalent, the first one found will be returned.
4405 *
4406 * The `comparator` is used in the same way as `Collection#sort`. If it is not
4407 * provided, the default comparator is `<`.
4408 *
4409 * When two values are considered equivalent, the first encountered will be
4410 * returned. Otherwise, `min` will operate independent of the order of input
4411 * as long as the comparator is commutative. The default comparator `<` is
4412 * commutative *only* when types do not differ.
4413 *
4414 * If `comparator` returns 0 and either value is NaN, undefined, or null,
4415 * that value will be returned.
4416 */
4417 min(comparator?: Comparator<V>): V | undefined;
4418
4419 /**
4420 * Like `min`, but also accepts a `comparatorValueMapper` which allows for
4421 * comparing by more sophisticated means:
4422 */
4423 minBy<C>(
4424 comparatorValueMapper: (value: V, key: K, iter: this) => C,
4425 comparator?: Comparator<C>
4426 ): V | undefined;
4427
4428 // Comparison
4429
4430 /**
4431 * True if `iter` includes every value in this Collection.
4432 */
4433 isSubset(iter: Iterable<V>): boolean;
4434
4435 /**
4436 * True if this Collection includes every value in `iter`.
4437 */
4438 isSuperset(iter: Iterable<V>): boolean;
4439 }
4440
4441 /**
4442 * The interface to fulfill to qualify as a Value Object.
4443 */
4444 interface ValueObject {
4445 /**
4446 * True if this and the other Collection have value equality, as defined
4447 * by `Immutable.is()`.
4448 *
4449 * Note: This is equivalent to `Immutable.is(this, other)`, but provided to
4450 * allow for chained expressions.
4451 */
4452 equals(other: unknown): boolean;
4453
4454 /**
4455 * Computes and returns the hashed identity for this Collection.
4456 *
4457 * The `hashCode` of a Collection is used to determine potential equality,
4458 * and is used when adding this to a `Set` or as a key in a `Map`, enabling
4459 * lookup via a different instance.
4460 *
4461 * Note: hashCode() MUST return a Uint32 number. The easiest way to
4462 * guarantee this is to return `myHash | 0` from a custom implementation.
4463 *
4464 * If two values have the same `hashCode`, they are [not guaranteed
4465 * to be equal][Hash Collision]. If two values have different `hashCode`s,
4466 * they must not be equal.
4467 *
4468 * Note: `hashCode()` is not guaranteed to always be called before
4469 * `equals()`. Most but not all Immutable.js collections use hash codes to
4470 * organize their internal data structures, while all Immutable.js
4471 * collections use equality during lookups.
4472 *
4473 * [Hash Collision]: https://en.wikipedia.org/wiki/Collision_(computer_science)
4474 */
4475 hashCode(): number;
4476 }
4477
4478 /**
4479 * Interface representing all oredered collections.
4480 * This includes `List`, `Stack`, `Map`, `OrderedMap`, `Set`, and `OrderedSet`.
4481 * return of `isOrdered()` return true in that case.
4482 */
4483 interface OrderedCollection<T> {
4484 /**
4485 * Shallowly converts this collection to an Array.
4486 */
4487 toArray(): Array<T>;
4488
4489 [Symbol.iterator](): IterableIterator<T>;
4490 }
4491
4492 /**
4493 * Deeply converts plain JS objects and arrays to Immutable Maps and Lists.
4494 *
4495 * `fromJS` will convert Arrays and [array-like objects][2] to a List, and
4496 * plain objects (without a custom prototype) to a Map. [Iterable objects][3]
4497 * may be converted to List, Map, or Set.
4498 *
4499 * If a `reviver` is optionally provided, it will be called with every
4500 * collection as a Seq (beginning with the most nested collections
4501 * and proceeding to the top-level collection itself), along with the key
4502 * referring to each collection and the parent JS object provided as `this`.
4503 * For the top level, object, the key will be `""`. This `reviver` is expected
4504 * to return a new Immutable Collection, allowing for custom conversions from
4505 * deep JS objects. Finally, a `path` is provided which is the sequence of
4506 * keys to this value from the starting value.
4507 *
4508 * `reviver` acts similarly to the [same parameter in `JSON.parse`][1].
4509 *
4510 * If `reviver` is not provided, the default behavior will convert Objects
4511 * into Maps and Arrays into Lists like so:
4512 *
4513 * Accordingly, this example converts native JS data to OrderedMap and List:
4514 *
4515 * Keep in mind, when using JS objects to construct Immutable Maps, that
4516 * JavaScript Object properties are always strings, even if written in a
4517 * quote-less shorthand, while Immutable Maps accept keys of any type.
4518 *
4519 * Property access for JavaScript Objects first converts the key to a string,
4520 * but since Immutable Map keys can be of any type the argument to `get()` is
4521 * not altered.
4522 *
4523 * [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Example.3A_Using_the_reviver_parameter
4524 * "Using the reviver parameter"
4525 * [2]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Indexed_collections#working_with_array-like_objects
4526 * "Working with array-like objects"
4527 * [3]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol
4528 * "The iterable protocol"
4529 */
4530 function fromJS<JSValue>(
4531 jsValue: JSValue,
4532 reviver?: undefined
4533 ): FromJS<JSValue>;
4534 function fromJS(
4535 jsValue: unknown,
4536 reviver?: (
4537 key: string | number,
4538 sequence: Collection.Keyed<string, unknown> | Collection.Indexed<unknown>,
4539 path?: Array<string | number>
4540 ) => unknown
4541 ): Collection<unknown, unknown>;
4542
4543 type FromJS<JSValue> = JSValue extends FromJSNoTransform
4544 ? JSValue
4545 : JSValue extends Array<unknown>
4546 ? FromJSArray<JSValue>
4547 : JSValue extends object
4548 ? FromJSObject<JSValue>
4549 : unknown;
4550
4551 type FromJSNoTransform =
4552 | Collection<unknown, unknown>
4553 | number
4554 | string
4555 | null
4556 | undefined;
4557
4558 type FromJSArray<JSValue> =
4559 JSValue extends Array<infer T> ? List<FromJS<T>> : never;
4560
4561 type FromJSObject<JSValue> = JSValue extends object
4562 ? Map<keyof JSValue, FromJS<JSValue[keyof JSValue]>>
4563 : never;
4564
4565 /**
4566 * Value equality check with semantics similar to `Object.is`, but treats
4567 * Immutable `Collection`s as values, equal if the second `Collection` includes
4568 * equivalent values.
4569 *
4570 * It's used throughout Immutable when checking for equality, including `Map`
4571 * key equality and `Set` membership.
4572 *
4573 * `is()` compares primitive types like strings and numbers, Immutable.js
4574 * collections like `Map` and `List`, but also any custom object which
4575 * implements `ValueObject` by providing `equals()` and `hashCode()` methods.
4576 *
4577 * Note: Unlike `Object.is`, `Immutable.is` assumes `0` and `-0` are the same
4578 * value, matching the behavior of ES6 Map key equality.
4579 */
4580 function is(first: unknown, second: unknown): boolean;
4581
4582 /**
4583 * The `hash()` function is an important part of how Immutable determines if
4584 * two values are equivalent and is used to determine how to store those
4585 * values. Provided with any value, `hash()` will return a 31-bit integer.
4586 *
4587 * When designing Objects which may be equal, it's important that when a
4588 * `.equals()` method returns true, that both values `.hashCode()` method
4589 * return the same value. `hash()` may be used to produce those values.
4590 *
4591 * For non-Immutable Objects that do not provide a `.hashCode()` functions
4592 * (including plain Objects, plain Arrays, Date objects, etc), a unique hash
4593 * value will be created for each *instance*. That is, the create hash
4594 * represents referential equality, and not value equality for Objects. This
4595 * ensures that if that Object is mutated over time that its hash code will
4596 * remain consistent, allowing Objects to be used as keys and values in
4597 * Immutable.js collections.
4598 *
4599 * Note that `hash()` attempts to balance between speed and avoiding
4600 * collisions, however it makes no attempt to produce secure hashes.
4601 *
4602 * *New in Version 4.0*
4603 */
4604 function hash(value: unknown): number;
4605
4606 /**
4607 * True if `maybeImmutable` is an Immutable Collection or Record.
4608 *
4609 * Note: Still returns true even if the collections is within a `withMutations()`.
4610 */
4611 function isImmutable(
4612 maybeImmutable: unknown
4613 ): maybeImmutable is Collection<unknown, unknown>;
4614
4615 /**
4616 * True if `maybeCollection` is a Collection, or any of its subclasses.
4617 */
4618 function isCollection(
4619 maybeCollection: unknown
4620 ): maybeCollection is Collection<unknown, unknown>;
4621
4622 /**
4623 * True if `maybeKeyed` is a Collection.Keyed, or any of its subclasses.
4624 */
4625 function isKeyed(
4626 maybeKeyed: unknown
4627 ): maybeKeyed is Collection.Keyed<unknown, unknown>;
4628
4629 /**
4630 * True if `maybeIndexed` is a Collection.Indexed, or any of its subclasses.
4631 */
4632 function isIndexed(
4633 maybeIndexed: unknown
4634 ): maybeIndexed is Collection.Indexed<unknown>;
4635
4636 /**
4637 * True if `maybeAssociative` is either a Keyed or Indexed Collection.
4638 */
4639 function isAssociative(
4640 maybeAssociative: unknown
4641 ): maybeAssociative is
4642 | Collection.Keyed<unknown, unknown>
4643 | Collection.Indexed<unknown>;
4644
4645 /**
4646 * True if `maybeOrdered` is a Collection where iteration order is well
4647 * defined. True for Collection.Indexed as well as OrderedMap and OrderedSet.
4648 */
4649 function isOrdered<T>(
4650 maybeOrdered: Iterable<T>
4651 ): maybeOrdered is OrderedCollection<T>;
4652 function isOrdered(
4653 maybeOrdered: unknown
4654 ): maybeOrdered is OrderedCollection<unknown>;
4655
4656 /**
4657 * True if `maybeValue` is a JavaScript Object which has *both* `equals()`
4658 * and `hashCode()` methods.
4659 *
4660 * Any two instances of *value objects* can be compared for value equality with
4661 * `Immutable.is()` and can be used as keys in a `Map` or members in a `Set`.
4662 */
4663 function isValueObject(maybeValue: unknown): maybeValue is ValueObject;
4664
4665 /**
4666 * True if `maybeSeq` is a Seq.
4667 */
4668 function isSeq(
4669 maybeSeq: unknown
4670 ): maybeSeq is
4671 | Seq.Indexed<unknown>
4672 | Seq.Keyed<unknown, unknown>
4673 | Seq.Set<unknown>;
4674
4675 /**
4676 * True if `maybeList` is a List.
4677 */
4678 function isList(maybeList: unknown): maybeList is List<unknown>;
4679
4680 /**
4681 * True if `maybeMap` is a Map.
4682 *
4683 * Also true for OrderedMaps.
4684 */
4685 function isMap(maybeMap: unknown): maybeMap is Map<unknown, unknown>;
4686
4687 /**
4688 * True if `maybeOrderedMap` is an OrderedMap.
4689 */
4690 function isOrderedMap(
4691 maybeOrderedMap: unknown
4692 ): maybeOrderedMap is OrderedMap<unknown, unknown>;
4693
4694 /**
4695 * True if `maybeStack` is a Stack.
4696 */
4697 function isStack(maybeStack: unknown): maybeStack is Stack<unknown>;
4698
4699 /**
4700 * True if `maybeSet` is a Set.
4701 *
4702 * Also true for OrderedSets.
4703 */
4704 function isSet(maybeSet: unknown): maybeSet is Set<unknown>;
4705
4706 /**
4707 * True if `maybeOrderedSet` is an OrderedSet.
4708 */
4709 function isOrderedSet(
4710 maybeOrderedSet: unknown
4711 ): maybeOrderedSet is OrderedSet<unknown>;
4712
4713 /**
4714 * True if `maybeRecord` is a Record.
4715 */
4716 function isRecord(maybeRecord: unknown): maybeRecord is Record<object>;
4717
4718 /**
4719 * Returns the value within the provided collection associated with the
4720 * provided key, or notSetValue if the key is not defined in the collection.
4721 *
4722 * A functional alternative to `collection.get(key)` which will also work on
4723 * plain Objects and Arrays as an alternative for `collection[key]`.
4724 */
4725 function get<K, V>(collection: Collection<K, V>, key: K): V | undefined;
4726 function get<K, V, NSV>(
4727 collection: Collection<K, V>,
4728 key: K,
4729 notSetValue: NSV
4730 ): V | NSV;
4731 function get<TProps extends object, K extends keyof TProps>(
4732 record: Record<TProps>,
4733 key: K,
4734 notSetValue: unknown
4735 ): TProps[K];
4736 function get<V>(collection: Array<V>, key: number): V | undefined;
4737 function get<V, NSV>(
4738 collection: Array<V>,
4739 key: number,
4740 notSetValue: NSV
4741 ): V | NSV;
4742 function get<C extends object, K extends keyof C>(
4743 object: C,
4744 key: K,
4745 notSetValue: unknown
4746 ): C[K];
4747 function get<V>(
4748 collection: { [key: PropertyKey]: V },
4749 key: string
4750 ): V | undefined;
4751 function get<V, NSV>(
4752 collection: { [key: PropertyKey]: V },
4753 key: string,
4754 notSetValue: NSV
4755 ): V | NSV;
4756
4757 /**
4758 * Returns true if the key is defined in the provided collection.
4759 *
4760 * A functional alternative to `collection.has(key)` which will also work with
4761 * plain Objects and Arrays as an alternative for
4762 * `collection.hasOwnProperty(key)`.
4763 */
4764 function has(collection: object, key: unknown): boolean;
4765
4766 /**
4767 * Returns a copy of the collection with the value at key removed.
4768 *
4769 * A functional alternative to `collection.remove(key)` which will also work
4770 * with plain Objects and Arrays as an alternative for
4771 * `delete collectionCopy[key]`.
4772 */
4773 function remove<K, C extends Collection<K, unknown>>(
4774 collection: C,
4775 key: K
4776 ): C;
4777 function remove<
4778 TProps extends object,
4779 C extends Record<TProps>,
4780 K extends keyof TProps,
4781 >(collection: C, key: K): C;
4782 function remove<C extends Array<unknown>>(collection: C, key: number): C;
4783 function remove<C, K extends keyof C>(collection: C, key: K): C;
4784 function remove<C extends { [key: string]: unknown }, K extends keyof C>(
4785 collection: C,
4786 key: K
4787 ): C;
4788
4789 /**
4790 * Returns a copy of the collection with the value at key set to the provided
4791 * value.
4792 *
4793 * A functional alternative to `collection.set(key, value)` which will also
4794 * work with plain Objects and Arrays as an alternative for
4795 * `collectionCopy[key] = value`.
4796 */
4797 function set<K, V, C extends Collection<K, V>>(
4798 collection: C,
4799 key: K,
4800 value: V
4801 ): C;
4802 function set<
4803 TProps extends object,
4804 C extends Record<TProps>,
4805 K extends keyof TProps,
4806 >(record: C, key: K, value: TProps[K]): C;
4807 function set<V, C extends Array<V>>(collection: C, key: number, value: V): C;
4808 function set<C, K extends keyof C>(object: C, key: K, value: C[K]): C;
4809 function set<V, C extends { [key: string]: V }>(
4810 collection: C,
4811 key: string,
4812 value: V
4813 ): C;
4814
4815 /**
4816 * Returns a copy of the collection with the value at key set to the result of
4817 * providing the existing value to the updating function.
4818 *
4819 * A functional alternative to `collection.update(key, fn)` which will also
4820 * work with plain Objects and Arrays as an alternative for
4821 * `collectionCopy[key] = fn(collection[key])`.
4822 */
4823 function update<K, V, C extends Collection<K, V>>(
4824 collection: C,
4825 key: K,
4826 updater: (value: V | undefined) => V | undefined
4827 ): C;
4828 function update<K, V, C extends Collection<K, V>, NSV>(
4829 collection: C,
4830 key: K,
4831 notSetValue: NSV,
4832 updater: (value: V | NSV) => V
4833 ): C;
4834 function update<
4835 TProps extends object,
4836 C extends Record<TProps>,
4837 K extends keyof TProps,
4838 >(record: C, key: K, updater: (value: TProps[K]) => TProps[K]): C;
4839 function update<
4840 TProps extends object,
4841 C extends Record<TProps>,
4842 K extends keyof TProps,
4843 NSV,
4844 >(
4845 record: C,
4846 key: K,
4847 notSetValue: NSV,
4848 updater: (value: TProps[K] | NSV) => TProps[K]
4849 ): C;
4850 function update<V>(
4851 collection: Array<V>,
4852 key: number,
4853 updater: (value: V | undefined) => V | undefined
4854 ): Array<V>;
4855 function update<V, NSV>(
4856 collection: Array<V>,
4857 key: number,
4858 notSetValue: NSV,
4859 updater: (value: V | NSV) => V
4860 ): Array<V>;
4861 function update<C, K extends keyof C>(
4862 object: C,
4863 key: K,
4864 updater: (value: C[K]) => C[K]
4865 ): C;
4866 function update<C, K extends keyof C, NSV>(
4867 object: C,
4868 key: K,
4869 notSetValue: NSV,
4870 updater: (value: C[K] | NSV) => C[K]
4871 ): C;
4872 function update<V, C extends { [key: string]: V }, K extends keyof C>(
4873 collection: C,
4874 key: K,
4875 updater: (value: V) => V
4876 ): { [key: string]: V };
4877 function update<V, C extends { [key: string]: V }, K extends keyof C, NSV>(
4878 collection: C,
4879 key: K,
4880 notSetValue: NSV,
4881 updater: (value: V | NSV) => V
4882 ): { [key: string]: V };
4883
4884 // TODO `<const P extends ...>` can be used after dropping support for TypeScript 4.x
4885 // reference: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-0.html#const-type-parameters
4886 // after this change, `as const` assertions can be remove from the type tests
4887 /**
4888 * Returns the value at the provided key path starting at the provided
4889 * collection, or notSetValue if the key path is not defined.
4890 *
4891 * A functional alternative to `collection.getIn(keypath)` which will also
4892 * work with plain Objects and Arrays.
4893 */
4894 function getIn<C, P extends ReadonlyArray<PropertyKey>>(
4895 object: C,
4896 keyPath: [...P]
4897 ): RetrievePath<C, P>;
4898 function getIn<C, P extends KeyPath<unknown>>(object: C, keyPath: P): unknown;
4899 function getIn<C, P extends ReadonlyArray<PropertyKey>, NSV>(
4900 collection: C,
4901 keyPath: [...P],
4902 notSetValue: NSV
4903 ): RetrievePath<C, P> extends never ? NSV : RetrievePath<C, P>;
4904 function getIn<C, P extends KeyPath<unknown>, NSV>(
4905 object: C,
4906 keyPath: P,
4907 notSetValue: NSV
4908 ): unknown;
4909
4910 /**
4911 * Returns true if the key path is defined in the provided collection.
4912 *
4913 * A functional alternative to `collection.hasIn(keypath)` which will also
4914 * work with plain Objects and Arrays.
4915 */
4916 function hasIn(
4917 collection: string | boolean | number,
4918 keyPath: KeyPath<unknown>
4919 ): never;
4920 function hasIn<K>(collection: unknown, keyPath: KeyPath<K>): boolean;
4921
4922 /**
4923 * Returns a copy of the collection with the value at the key path removed.
4924 *
4925 * A functional alternative to `collection.removeIn(keypath)` which will also
4926 * work with plain Objects and Arrays.
4927 */
4928 function removeIn<C>(collection: C, keyPath: Iterable<unknown>): C;
4929
4930 /**
4931 * Returns a copy of the collection with the value at the key path set to the
4932 * provided value.
4933 *
4934 * A functional alternative to `collection.setIn(keypath)` which will also
4935 * work with plain Objects and Arrays.
4936 */
4937 function setIn<C>(
4938 collection: C,
4939 keyPath: Iterable<unknown>,
4940 value: unknown
4941 ): C;
4942
4943 /**
4944 * Returns a copy of the collection with the value at key path set to the
4945 * result of providing the existing value to the updating function.
4946 *
4947 * A functional alternative to `collection.updateIn(keypath)` which will also
4948 * work with plain Objects and Arrays.
4949 */
4950 function updateIn<K extends PropertyKey, V, C extends Collection<K, V>>(
4951 collection: C,
4952 keyPath: KeyPath<K>,
4953 updater: (
4954 value: RetrievePath<C, Array<K>> | undefined
4955 ) => unknown | undefined
4956 ): C;
4957 function updateIn<K extends PropertyKey, V, C extends Collection<K, V>, NSV>(
4958 collection: C,
4959 keyPath: KeyPath<K>,
4960 notSetValue: NSV,
4961 updater: (value: RetrievePath<C, Array<K>> | NSV) => unknown
4962 ): C;
4963 function updateIn<
4964 TProps extends object,
4965 C extends Record<TProps>,
4966 K extends keyof TProps,
4967 >(
4968 record: C,
4969 keyPath: KeyPath<K>,
4970 updater: (value: RetrievePath<C, Array<K>>) => unknown
4971 ): C;
4972 function updateIn<
4973 TProps extends object,
4974 C extends Record<TProps>,
4975 K extends keyof TProps,
4976 NSV,
4977 >(
4978 record: C,
4979 keyPath: KeyPath<K>,
4980 notSetValue: NSV,
4981 updater: (value: RetrievePath<C, Array<K>> | NSV) => unknown
4982 ): C;
4983 function updateIn<K extends PropertyKey, V, C extends Array<V>>(
4984 collection: Array<V>,
4985 keyPath: KeyPath<string | number>,
4986 updater: (
4987 value: RetrievePath<C, Array<K>> | undefined
4988 ) => unknown | undefined
4989 ): Array<V>;
4990 function updateIn<K extends PropertyKey, V, C extends Array<V>, NSV>(
4991 collection: Array<V>,
4992 keyPath: KeyPath<K>,
4993 notSetValue: NSV,
4994 updater: (value: RetrievePath<C, Array<K>> | NSV) => unknown
4995 ): Array<V>;
4996 function updateIn<K extends PropertyKey, C>(
4997 object: C,
4998 keyPath: KeyPath<K>,
4999 updater: (value: RetrievePath<C, Array<K>>) => unknown
5000 ): C;
5001 function updateIn<K extends PropertyKey, C, NSV>(
5002 object: C,
5003 keyPath: KeyPath<K>,
5004 notSetValue: NSV,
5005 updater: (value: RetrievePath<C, Array<K>> | NSV) => unknown
5006 ): C;
5007 function updateIn<
5008 K extends PropertyKey,
5009 V,
5010 C extends { [key: PropertyKey]: V },
5011 >(
5012 collection: C,
5013 keyPath: KeyPath<K>,
5014 updater: (value: RetrievePath<C, Array<K>>) => unknown
5015 ): { [key: PropertyKey]: V };
5016 function updateIn<
5017 K extends PropertyKey,
5018 V,
5019 C extends { [key: PropertyKey]: V },
5020 NSV,
5021 >(
5022 collection: C,
5023 keyPath: KeyPath<K>,
5024 notSetValue: NSV,
5025 updater: (value: RetrievePath<C, Array<K>> | NSV) => unknown
5026 ): { [key: PropertyKey]: V };
5027
5028 /**
5029 * Returns a copy of the collection with the remaining collections merged in.
5030 *
5031 * A functional alternative to `collection.merge()` which will also work with
5032 * plain Objects and Arrays.
5033 */
5034 function merge<C>(
5035 collection: C,
5036 ...collections: Array<
5037 | Iterable<unknown>
5038 | Iterable<[unknown, unknown]>
5039 | { [key: string]: unknown }
5040 >
5041 ): C;
5042
5043 /**
5044 * Returns a copy of the collection with the remaining collections merged in,
5045 * calling the `merger` function whenever an existing value is encountered.
5046 *
5047 * A functional alternative to `collection.mergeWith()` which will also work
5048 * with plain Objects and Arrays.
5049 */
5050 function mergeWith<C>(
5051 merger: (oldVal: unknown, newVal: unknown, key: unknown) => unknown,
5052 collection: C,
5053 ...collections: Array<
5054 | Iterable<unknown>
5055 | Iterable<[unknown, unknown]>
5056 | { [key: string]: unknown }
5057 >
5058 ): C;
5059
5060 /**
5061 * Like `merge()`, but when two compatible collections are encountered with
5062 * the same key, it merges them as well, recursing deeply through the nested
5063 * data. Two collections are considered to be compatible (and thus will be
5064 * merged together) if they both fall into one of three categories: keyed
5065 * (e.g., `Map`s, `Record`s, and objects), indexed (e.g., `List`s and
5066 * arrays), or set-like (e.g., `Set`s). If they fall into separate
5067 * categories, `mergeDeep` will replace the existing collection with the
5068 * collection being merged in. This behavior can be customized by using
5069 * `mergeDeepWith()`.
5070 *
5071 * Note: Indexed and set-like collections are merged using
5072 * `concat()`/`union()` and therefore do not recurse.
5073 *
5074 * A functional alternative to `collection.mergeDeep()` which will also work
5075 * with plain Objects and Arrays.
5076 */
5077 function mergeDeep<C>(
5078 collection: C,
5079 ...collections: Array<
5080 | Iterable<unknown>
5081 | Iterable<[unknown, unknown]>
5082 | { [key: string]: unknown }
5083 >
5084 ): C;
5085
5086 /**
5087 * Like `mergeDeep()`, but when two non-collections or incompatible
5088 * collections are encountered at the same key, it uses the `merger` function
5089 * to determine the resulting value. Collections are considered incompatible
5090 * if they fall into separate categories between keyed, indexed, and set-like.
5091 *
5092 * A functional alternative to `collection.mergeDeepWith()` which will also
5093 * work with plain Objects and Arrays.
5094 */
5095 function mergeDeepWith<C>(
5096 merger: (oldVal: unknown, newVal: unknown, key: unknown) => unknown,
5097 collection: C,
5098 ...collections: Array<
5099 | Iterable<unknown>
5100 | Iterable<[unknown, unknown]>
5101 | { [key: string]: unknown }
5102 >
5103 ): C;
5104 }
5105
5106 /**
5107 * Defines the main export of the immutable module to be the Immutable namespace
5108 * This supports many common module import patterns:
5109 *
5110 * const Immutable = require("immutable");
5111 * const { List } = require("immutable");
5112 * import Immutable from "immutable";
5113 * import * as Immutable from "immutable";
5114 * import { List } from "immutable";
5115 *
5116 */
5117 export = Immutable;
5118
5119 /**
5120 * A global "Immutable" namespace used by UMD modules which allows the use of
5121 * the full Immutable API.
5122 *
5123 * If using Immutable as an imported module, prefer using:
5124 *
5125 * import Immutable from 'immutable'
5126 *
5127 */
5128 export as namespace Immutable;