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

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

Open
mwiencek opened this issue Oct 19, 2023 · 1 comment

Comments

@mwiencek
Copy link

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.

@SamChou19815
Copy link
Contributor

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

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

2 participants