You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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!
The text was updated successfully, but these errors were encountered:
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?
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.
I am having trouble with using OrderedMap(someArray) in typescript. Typical errors are like:
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!
The text was updated successfully, but these errors were encountered: