flow icon indicating copy to clipboard operation
flow copied to clipboard

Strange index signature error if you use $Keys<O> instead of the equivalent literal union inside $ReadOnlyArray

Open mwiencek opened this issue 2 years ago • 1 comments

Flow version: 0.219.2

type O = {a: 1, b: 2};

type K1 = 'a' | 'b';

// should be equivalent to K1
type K2 = $Keys<O>;

// gives incompatible-cast (expected)
('ab'.split(''): $ReadOnlyArray<K1>);

// gives props-missing (???)
('ab'.split(''): $ReadOnlyArray<K2>);

Expected behavior

I'd expect the ('ab'.split(''): $ReadOnlyArray<K2>); cast above to throw an incompatible-cast error, like the first one does.

Actual behavior

A prop-missing error is given instead:

12: ('ab'.split(''): $ReadOnlyArray<K2>);
     ^ Cannot cast `'ab'.split(...)` to read-only array type because an index signature declaring the expected key / value type is missing in `O` [1] in array element. [prop-missing]
References:
12: ('ab'.split(''): $ReadOnlyArray<K2>);
                                    ^ [1]

I ran into this issue because I previously had a // $FlowIgnore[incompatible-cast] to suppress the first error, but that stopped working once I started using $Keys.

  • Link to Try-Flow or Github repo: https://flow.org/try/#1N4Igxg9gdgZglgcxALlAIwIZoKYBsD6uEEAztvhgE6UYCe+JADpdhgCYowa5kA0I2KAFcAtiRQAXSkOz9sADwxgJ+NPTbYuQ3BMnTZA+Y2yU4IwRO4A6SFBIrGVDGM7c+IFkolXpUCWewUEAwhCQgRDH8wEH4hMnwROHlsNnw4KHwwSLAAC3wANyo4LFxscWQuHgMNZmwsiRSAWglaY1cq-hIAa2wJXNpG4Vxcdvdu3v7B0RxKUYMhKDBSqmbWwIq3eagoOrKSKgH0wtMMPznY7d2SfcoBiEZ-aG5G3Ix085AF-ZhsRoRehqUEiNMgSQHlSruBZxJrMcJwMhzAC+-EgGiCLWMAAIAPJYgC8WOAGGQWIAjLwsWhSQAmJEAbgAOlBmZjsFiANJkglYgDkGF5WIAPny0LymSyoAB6KVYkg5CDaNhU9nYACOQjghVKfixYU5ZNZa05NJ5ABIOdhaCQADw4gB8EuZMqxCC1ZSx6UgIkc-jQpReGHsWIAFApjMoUgBKZkh-liqxMXBwCRx3lR0lmgBKrDYOKguFoAEFqHQbVz7VGndLZW78h64YxgYlrukEKGAPxdmNQONYXmJxjJ1O89OZnPsfOFks0WjlmmViUxED1oFwaBBfIABisNLJAE5dyAkUA

mwiencek avatar Oct 19 '23 12:10 mwiencek

I can understand why that's happening, but I agree that the error message is certainly not good.

SamChou19815 avatar Oct 25 '23 17:10 SamChou19815