-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Cannot pass Array<T> to a function expecting Array<?T> #4251
Comments
I believe the issue is that if the function takes |
If the necessary subtyping were allowed, then const lengths = (strings: Array<?string>): Array<number> => {
strings[0] = null; // I've just corrupted `externalStrings`
strings.map((s) => s ? s.length : 0);
}
let externalStrings: Array<string> = ['hello', 'world']
lengths(externalStrings) // Flow error |
You can use $ReadOnlyArray now apparently, though not sure if it's documented: |
ahhhhhhhh. right. mutability. never caused anyone any problems. 🙄 thank you – that makes perfect sense. |
I just discovered some behavior regarding arrays of maybe types that I found surprising:
An
Array<T>
cannot be passed as an argument for a parameter of typeArray<?T>
, even though all values in anArray<T>
are valid in anArray<?T>
. 👉 try flowCasting via
any
works, but is nasty. Is this expected behavior? Is there a workaround other than casting toany
?I'm sorry if this is already a reported issue; I couldn't find it.
The text was updated successfully, but these errors were encountered: