Skip to content

Typescript return type of sort/sortBy on non-Ordered Collections isn't accurate #2012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cypherfunc opened this issue Aug 12, 2024 · 1 comment · Fixed by #2013
Closed

Typescript return type of sort/sortBy on non-Ordered Collections isn't accurate #2012

cypherfunc opened this issue Aug 12, 2024 · 1 comment · Fixed by #2013
Labels
awaiting-response Awaiting response from creator. Can't fix without it.

Comments

@cypherfunc
Copy link

What happened

When calling .sort() or .sortBy() on non-"Ordered" Collections like Maps and Sets, the return type is the same as the original Collection, when it actually returns the "Ordered" version.

According to the docs:

* When sorting collections which have no defined order, their ordered
* equivalents will be returned. e.g. `map.sort()` returns OrderedMap.
*
* <!-- runkit:activate -->
* ```js
* const { Map } = require('immutable')
* Map({ "c": 3, "a": 1, "b": 2 }).sort((a, b) => {
* if (a < b) { return -1; }
* if (a > b) { return 1; }
* if (a === b) { return 0; }
* });
* // OrderedMap { "a": 1, "b": 2, "c": 3 }
* ```

This matches the implementations:

immutable-js/src/Map.js

Lines 112 to 120 in 0c2d021

sort(comparator) {
// Late binding
return OrderedMap(sortFactory(this, comparator));
}
sortBy(mapper, comparator) {
// Late binding
return OrderedMap(sortFactory(this, comparator, mapper));
}

immutable-js/src/Set.js

Lines 153 to 161 in 0c2d021

sort(comparator) {
// Late binding
return OrderedSet(sortFactory(this, comparator));
}
sortBy(mapper, comparator) {
// Late binding
return OrderedSet(sortFactory(this, comparator, mapper));
}

However the TS types just use the inherited definitions from interface Collection<K, V>:

sort(comparator?: Comparator<V>): this;

sortBy<C>(
comparatorValueMapper: (value: V, key: K, iter: this) => C,
comparator?: Comparator<C>
): this;

It's possible to monkey-patch the Map and Set interfaces like this:

declare module "immutable" {
    export interface Map<K, V> {
        sort(comparator?: Comparator<V>): OrderedMap<K, V>;
        sortBy<C>(
            comparatorValueMapper: (value: V, key: K, iter: this) => C,
            comparator?: (valueA: C, valueB: C) => number
        ): OrderedMap<K, V>;
    }

    export interface Set<T> {
        sort(comparator?: Comparator<T>): OrderedSet<T>;
        sortBy<C>(
            comparatorValueMapper: (value: T, key: T, iter: this) => C,
            comparator?: (valueA: C, valueB: C) => number
        ): OrderedSet<T>;
    }
}

However, I don't know enough to recommend that as the real fix. This issue probably also affects some other types besides just Map and Set, and it might be better to add the accurate definitions on one of the abstract types, or some other interface. 🤷

How to reproduce

Call .sort() or .sortBy() on a Map or Set and look at the returned type.

@jdeniau
Copy link
Member

jdeniau commented Aug 13, 2024

Hi,

Thanks for the report. I think that the fix should be easy in the TS definition file.
We should override the function definition in Map, Set etc. interface, just like you did in your example.

If you are willing to open a PR, that would be great, otherwise I will try to implement this when I'm back home.

@jdeniau jdeniau linked a pull request Aug 18, 2024 that will close this issue
@jdeniau jdeniau added the awaiting-response Awaiting response from creator. Can't fix without it. label Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-response Awaiting response from creator. Can't fix without it.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants