Skip to content
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

types are wrong for using plain arrays instead of iterable #1884

Open
antonkulaga opened this issue Oct 23, 2021 · 2 comments
Open

types are wrong for using plain arrays instead of iterable #1884

antonkulaga opened this issue Oct 23, 2021 · 2 comments

Comments

@antonkulaga
Copy link

antonkulaga commented Oct 23, 2021

I am having trouble with using OrderedMap(someArray) in typescript. Typical errors are like:

 Overload 1 of 2, '(collection?: Iterable<[string, number]> | undefined): OrderedMap<string, number>', gave the following error.
        Argument of type '(string | number)[][]' is not assignable to parameter of type 'Iterable<[string, number]>'.

This types of problems are valid for all arrays I tried, including Array<Array>.
While working in pure JS and sometimes with typescript in the browser it provides type errors for typescript on nodejs because OrderedMap does not have an overload for arrays. Please, improve type-script types in immutable js!

@jdeniau
Copy link
Member

jdeniau commented Oct 24, 2021

We know that typescript does not work well especially with Map.
There is an effort to improve that.
Can you please provide a reproducible example of your bug?

@cypherfunc
Copy link

cypherfunc commented Nov 19, 2021

That's more of a TS issue than an Immutable issue. You need to make sure collection is typed as an array of two-element tuples, instead of an array of arrays.

// TS infers the type to be (string|number)[][]
const array1 = [
    ["a", 1], ["b", 2], ["c", 3]
];

// According to that type, this is legal
const array2 = array1.concat([
    ["x", 7, "foo", "blarg"]
]);

// which is why you can't pass it to OrderedMap (this throws the type error you mentioned, as it should)
const om = OrderedMap(array1);

// instead, if you explicitly type the array as an array of tuples
const array3: [string, number][] = [
    ["a", 1], ["b", 2], ["c", 3]
];

// this will throw a type error, as it should
const array4 = array3.concat([
    ["x", 7, "foo", "blarg"]
]);

// and you can pass it to OrderedMap
const om2 = OrderedMap(array3);

Personally, I share the frustration with how TS defaults to union-array types instead of tuple types, but I doubt that's going to change anytime soon, and tuples are the right type for key-value pairs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants