From 800d47e56a81fc0f105a73be6a988924263753ee Mon Sep 17 00:00:00 2001 From: Albert Siddhartha Slawinski Date: Sat, 25 Aug 2018 19:34:42 +0200 Subject: [PATCH] Remove the map duplication through higher-order types inspired by https://github.com/Microsoft/TypeScript/issues/25947#issuecomment-407929455 --- type-definitions/Immutable.d.ts | 300 +++++++------------------------- 1 file changed, 62 insertions(+), 238 deletions(-) diff --git a/type-definitions/Immutable.d.ts b/type-definitions/Immutable.d.ts index e493a78ba..32a6ad1b5 100644 --- a/type-definitions/Immutable.d.ts +++ b/type-definitions/Immutable.d.ts @@ -99,6 +99,56 @@ declare module Immutable { + const CollectionID: unique symbol + type CollectionID = typeof CollectionID + const CollectionKeyedID: unique symbol + type CollectionKeyedID = typeof CollectionKeyedID + const CollectionIndexedID: unique symbol + type CollectionIndexedID = typeof CollectionIndexedID + const CollectionSetID: unique symbol + type CollectionSetID = typeof CollectionSetID + + const ListID: unique symbol + type ListID = typeof ListID + const MapID: unique symbol + type MapID = typeof MapID + const OrderedMapID: unique symbol + type OrderedMapID = typeof OrderedMapID + const SetID: unique symbol + type SetID = typeof SetID + const OrderedSetID: unique symbol + type OrderedSetID = typeof OrderedSetID + const StackID: unique symbol + type StackID = typeof StackID + + const SeqID: unique symbol + type SeqID = typeof SeqID + const SeqKeyedID: unique symbol + type SeqKeyedID = typeof SeqKeyedID + const SeqIndexedID: unique symbol + type SeqIndexedID = typeof SeqIndexedID + const SeqSetID: unique symbol + type SeqSetID = typeof SeqSetID + + interface Collections { + [CollectionID]: Collection + [CollectionKeyedID]: Collection.Keyed + [CollectionIndexedID]: Collection.Indexed + [CollectionSetID]: Collection.Set + + [ListID]: List + [MapID]: Map + [OrderedMapID]: OrderedMap + [SetID]: Set + [OrderedSetID]: OrderedSet + [StackID]: Stack + + [SeqID]: Seq + [SeqKeyedID]: Seq.Keyed + [SeqIndexedID]: Seq.Indexed + [SeqSetID]: Seq.Set + } + /** * Lists are ordered indexed dense collections, much like a JavaScript * Array. @@ -545,23 +595,6 @@ declare module Immutable { concat(...valuesOrCollections: Array | C>): List; merge(...collections: Array>): List; - /** - * Returns a new List with values passed through a - * `mapper` function. - * - * - * ```js - * List([ 1, 2 ]).map(x => 10 * x) - * // List [ 10, 20 ] - * ``` - */ - map( - mapper: (value: T, key: number, iter: this) => M, - context?: any - ): List; - /** * Flat-maps the List, returning a new List. * @@ -758,7 +791,7 @@ declare module Immutable { export function Map(): Map; export function Map(): Map; - export interface Map extends Collection.Keyed { + export interface Map = MapID> extends Collection.Keyed { /** * The number of entries in this Map. @@ -1316,18 +1349,6 @@ declare module Immutable { // Sequence algorithms - /** - * Returns a new Map with values passed through a - * `mapper` function. - * - * Map({ a: 1, b: 2 }).map(x => 10 * x) - * // Map { a: 10, b: 20 } - */ - map( - mapper: (value: V, key: K, iter: this) => M, - context?: any - ): Map; - /** * @see Collection.Keyed.mapKeys */ @@ -1416,7 +1437,7 @@ declare module Immutable { export function OrderedMap(): OrderedMap; export function OrderedMap(): OrderedMap; - export interface OrderedMap extends Map { + export interface OrderedMap extends Map { /** * The number of entries in this OrderedMap. @@ -1472,21 +1493,6 @@ declare module Immutable { // Sequence algorithms - /** - * Returns a new OrderedMap with values passed through a - * `mapper` function. - * - * OrderedMap({ a: 1, b: 2 }).map(x => 10 * x) - * // OrderedMap { "a": 10, "b": 20 } - * - * Note: `map()` always returns a new instance, even if it produced the same - * value at every step. - */ - map( - mapper: (value: V, key: K, iter: this) => M, - context?: any - ): OrderedMap; - /** * @see Collection.Keyed.mapKeys */ @@ -1708,18 +1714,6 @@ declare module Immutable { // Sequence algorithms - /** - * Returns a new Set with values passed through a - * `mapper` function. - * - * Set([1,2]).map(x => 10 * x) - * // Set [10,20] - */ - map( - mapper: (value: T, key: T, iter: this) => M, - context?: any - ): Set; - /** * Flat-maps the Set, returning a new Set. * @@ -1807,18 +1801,6 @@ declare module Immutable { // Sequence algorithms - /** - * Returns a new Set with values passed through a - * `mapper` function. - * - * OrderedSet([ 1, 2 ]).map(x => 10 * x) - * // OrderedSet [10, 20] - */ - map( - mapper: (value: T, key: T, iter: this) => M, - context?: any - ): OrderedSet; - /** * Flat-maps the OrderedSet, returning a new OrderedSet. * @@ -2049,21 +2031,6 @@ declare module Immutable { */ concat(...valuesOrCollections: Array | C>): Stack; - /** - * Returns a new Stack with values passed through a - * `mapper` function. - * - * Stack([ 1, 2 ]).map(x => 10 * x) - * // Stack [ 10, 20 ] - * - * Note: `map()` always returns a new instance, even if it produced the same - * value at every step. - */ - map( - mapper: (value: T, key: number, iter: this) => M, - context?: any - ): Stack; - /** * Flat-maps the Stack, returning a new Stack. * @@ -2440,7 +2407,7 @@ declare module Immutable { // Reading values - has(key: string): key is keyof TProps; + has(key: string): key is ((keyof TProps) & string); /** * Returns the value associated with the provided key, which may be the @@ -2656,7 +2623,7 @@ declare module Immutable { export function Keyed(): Seq.Keyed; export function Keyed(): Seq.Keyed; - export interface Keyed extends Seq, Collection.Keyed { + export interface Keyed extends Seq, Collection.Keyed { /** * Deeply converts this Keyed Seq to equivalent native JavaScript Object. * @@ -2690,24 +2657,6 @@ declare module Immutable { concat(...collections: Array>): Seq.Keyed; concat(...collections: Array<{[key: string]: C}>): Seq.Keyed; - /** - * Returns a new Seq.Keyed with values passed through a - * `mapper` function. - * - * ```js - * const { Seq } = require('immutable') - * Seq.Keyed({ a: 1, b: 2 }).map(x => 10 * x) - * // Seq { "a": 10, "b": 20 } - * ``` - * - * Note: `map()` always returns a new instance, even if it produced the - * same value at every step. - */ - map( - mapper: (value: V, key: K, iter: this) => M, - context?: any - ): Seq.Keyed; - /** * @see Collection.Keyed.mapKeys */ @@ -2776,7 +2725,7 @@ declare module Immutable { export function Indexed(): Seq.Indexed; export function Indexed(collection: Iterable): Seq.Indexed; - export interface Indexed extends Seq, Collection.Indexed { + export interface Indexed extends Seq, Collection.Indexed { /** * Deeply converts this Indexed Seq to equivalent native JavaScript Array. */ @@ -2802,24 +2751,6 @@ declare module Immutable { */ concat(...valuesOrCollections: Array | C>): Seq.Indexed; - /** - * Returns a new Seq.Indexed with values passed through a - * `mapper` function. - * - * ```js - * const { Seq } = require('immutable') - * Seq.Indexed([ 1, 2 ]).map(x => 10 * x) - * // Seq [ 10, 20 ] - * ``` - * - * Note: `map()` always returns a new instance, even if it produced the - * same value at every step. - */ - map( - mapper: (value: T, key: number, iter: this) => M, - context?: any - ): Seq.Indexed; - /** * Flat-maps the Seq, returning a a Seq of the same type. * @@ -2925,7 +2856,7 @@ declare module Immutable { export function Set(): Seq.Set; export function Set(collection: Iterable): Seq.Set; - export interface Set extends Seq, Collection.Set { + export interface Set extends Seq, Collection.Set { /** * Deeply converts this Set Seq to equivalent native JavaScript Array. */ @@ -2954,23 +2885,6 @@ declare module Immutable { */ concat(...collections: Array>): Seq.Set; - /** - * Returns a new Seq.Set with values passed through a - * `mapper` function. - * - * ```js - * Seq.Set([ 1, 2 ]).map(x => 10 * x) - * // Seq { 10, 20 } - * ``` - * - * Note: `map()` always returns a new instance, even if it produced the - * same value at every step. - */ - map( - mapper: (value: T, key: T, iter: this) => M, - context?: any - ): Seq.Set; - /** * Flat-maps the Seq, returning a Seq of the same type. * @@ -3021,7 +2935,7 @@ declare module Immutable { export function Seq(obj: {[key: string]: V}): Seq.Keyed; export function Seq(): Seq; - export interface Seq extends Collection { + export interface Seq = SeqID> extends Collection { /** * Some Seqs can describe their size lazily. When this is the case, @@ -3062,43 +2976,6 @@ declare module Immutable { // Sequence algorithms - /** - * Returns a new Seq with values passed through a - * `mapper` function. - * - * ```js - * const { Seq } = require('immutable') - * Seq([ 1, 2 ]).map(x => 10 * x) - * // Seq [ 10, 20 ] - * ``` - * - * Note: `map()` always returns a new instance, even if it produced the same - * value at every step. - */ - map( - mapper: (value: V, key: K, iter: this) => M, - context?: any - ): Seq; - - /** - * Returns a new Seq with values passed through a - * `mapper` function. - * - * ```js - * const { Seq } = require('immutable') - * Seq([ 1, 2 ]).map(x => 10 * x) - * // Seq [ 10, 20 ] - * ``` - * - * Note: `map()` always returns a new instance, even if it produced the same - * value at every step. - * Note: used only for sets. - */ - map( - mapper: (value: V, key: K, iter: this) => M, - context?: any - ): Seq; - /** * Flat-maps the Seq, returning a Seq of the same type. * @@ -3192,7 +3069,7 @@ declare module Immutable { export function Keyed(collection: Iterable<[K, V]>): Collection.Keyed; export function Keyed(obj: {[key: string]: V}): Collection.Keyed; - export interface Keyed extends Collection { + export interface Keyed = CollectionKeyedID> extends Collection { /** * Deeply converts this Keyed collection to equivalent native JavaScript Object. * @@ -3240,24 +3117,6 @@ declare module Immutable { concat(...collections: Array>): Collection.Keyed; concat(...collections: Array<{[key: string]: C}>): Collection.Keyed; - /** - * Returns a new Collection.Keyed with values passed through a - * `mapper` function. - * - * ```js - * const { Collection } = require('immutable') - * Collection.Keyed({ a: 1, b: 2 }).map(x => 10 * x) - * // Seq { "a": 10, "b": 20 } - * ``` - * - * Note: `map()` always returns a new instance, even if it produced the - * same value at every step. - */ - map( - mapper: (value: V, key: K, iter: this) => M, - context?: any - ): Collection.Keyed; - /** * Returns a new Collection.Keyed of the same type with keys passed through * a `mapper` function. @@ -3349,7 +3208,7 @@ declare module Immutable { */ export function Indexed(collection: Iterable): Collection.Indexed; - export interface Indexed extends Collection { + export interface Indexed = CollectionIndexedID> extends Collection { /** * Deeply converts this Indexed collection to equivalent native JavaScript Array. */ @@ -3567,24 +3426,6 @@ declare module Immutable { */ concat(...valuesOrCollections: Array | C>): Collection.Indexed; - /** - * Returns a new Collection.Indexed with values passed through a - * `mapper` function. - * - * ```js - * const { Collection } = require('immutable') - * Collection.Indexed([1,2]).map(x => 10 * x) - * // Seq [ 1, 2 ] - * ``` - * - * Note: `map()` always returns a new instance, even if it produced the - * same value at every step. - */ - map( - mapper: (value: T, key: number, iter: this) => M, - context?: any - ): Collection.Indexed; - /** * Flat-maps the Collection, returning a Collection of the same type. * @@ -3639,7 +3480,7 @@ declare module Immutable { */ export function Set(collection: Iterable): Collection.Set; - export interface Set extends Collection { + export interface Set = CollectionSetID> extends Collection { /** * Deeply converts this Set collection to equivalent native JavaScript Array. */ @@ -3668,23 +3509,6 @@ declare module Immutable { */ concat(...collections: Array>): Collection.Set; - /** - * Returns a new Collection.Set with values passed through a - * `mapper` function. - * - * ``` - * Collection.Set([ 1, 2 ]).map(x => 10 * x) - * // Seq { 1, 2 } - * ``` - * - * Note: `map()` always returns a new instance, even if it produced the - * same value at every step. - */ - map( - mapper: (value: T, key: T, iter: this) => M, - context?: any - ): Collection.Set; - /** * Flat-maps the Collection, returning a Collection of the same type. * @@ -3735,7 +3559,7 @@ declare module Immutable { export function Collection(collection: Iterable): Collection.Indexed; export function Collection(obj: {[key: string]: V}): Collection.Keyed; - export interface Collection extends ValueObject { + export interface Collection = CollectionID> extends ValueObject { // Value equality @@ -4079,7 +3903,7 @@ declare module Immutable { map( mapper: (value: V, key: K, iter: this) => M, context?: any - ): Collection; + ): Collections[ConcreteCollectionID]; /** * Note: used only for sets, which return Collection but are otherwise