immutable-js-oss / immutable-js

Immutable persistent data collections for Javascript which increase efficiency and simplicity.
https://immutable-js-oss.github.io/immutable-js/
MIT License
37 stars 6 forks source link

Can't assign string to string | null for object in immutable map #179

Closed Methuselah96 closed 4 years ago

Methuselah96 commented 4 years ago

From @JoshuaLamusga on Thu, 10 Sep 2020 20:24:13 GMT

What happened

With typescript 3.9.7 and immutable-js version 3.8.2, the following code unexpectedly throws an error:

type TestAbcType = IImmutableMap<{ a: boolean, b: string | null }>
const testAbc = createImmutableMap({ a: true, b: null })
const testXyz: TestAbcType = testAbc

With the error being:

Type 'IImmutableMap<{ a: boolean; b: null; }>' is not assignable to type 'TestAbcType'.
  Type 'string | null' is not assignable to type 'null'.
    Type 'string' is not assignable to type 'null'.

This is surprising because according to the type definition, b can be a string or null. In this case, b is null and should satisfy the condition of string | null. This code works if it's not wrapped with IImmutableMap, indicating the problem lies with this library.

Copied from original issue: https://github.com/immutable-js/immutable-js/issues/1788

Methuselah96 commented 4 years ago

Closing since needed details were not provided and it's been more than a week.