-
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
(v0.70) Missing type annotation when using array methods with exported function #6151
Comments
Why use See the changelog:
|
Not an error, quite the opposite actually (it's quite deliberate). You export a function — Flow expects you to document the function's return type. Your function has no return type annotation. I mean, the error message even tells you! And use |
@lll000111 The error message doesn't tell anything about missing return type. It says that |
You say:
The error message says
I'd say "missing annotation" is quite clear. There is a missing annotation. You look at the function and you see — there is no annotation! You check your parameters and the return type, that's all that can be (and that must be) annotated. There is no ambiguity here. Okay, the "U"... so what? You don't need it. All you need is "Missing annotation". Ignore generic components. While it could be improved of course, I think a type system requires a bit of prerequisite knowledge, this isn't aimed at average users. This tool sure has more than enough unsolved issues (that nobody on the owner side even acknowledges, they seem to ignore this entire public "issues" section as far as I can tell), in comparison I'd put this error message quality issue at the bottom of the list. |
@lll000111 It asks you to annotate your |
This seems like inconsistent behavior. Either all exports should require explicit annotations or non should as long as the implicit types are valid. |
I'm pretty sure something seems wrong with exporting anything using an array method. Given this example https://flow.org/try
|
I'm seeing this too. You don't need to wrap the array in an object, as you can see here. Happens in 0.7.0, but not 0.6.9. Adding a type annotation before exporting gets rid of the error |
So where do 'U', 'T' or 'S' come from? Are they just randomly generated letters? In that case it would be very helpful to replace them with 'exported function return type' or something like that. |
@schumannd this for It's technically doing things as it should: you're returning an Array or an Array and it needs a type annotation for the type parameters of the returned array; the problem is it's a little cryptic since the user didn't directly define |
Well yes, flow is throwing an error and thus doing things as it should. I am strongly in favor of doing something about this. Operations on arrays are among the most common things to appear in any code. These cryptic error messages will cause a lot of new users trouble. |
I'm not sure it technically is doing the right thing. Return type annotations are not required on all exported methods -- that's easy to verify. So what's special about methods that use array generics? |
Not upgrading past that due to facebook/flow#6151.
Not upgrading past that due to facebook/flow#6151.
Not upgrading past that due to facebook/flow#6151.
Not upgrading past that due to facebook/flow#6151.
Not upgrading past that due to facebook/flow#6151.
I don't understand this entirely, but if you do |
@amypellegrini I ended up finding the same conclusion. I still don't completely understand but here is a screenshot that somewhat confirms it |
We already have |
@peeyush-ad2games What version of flow are you using? |
@functionalStoic we are using |
I also ran into the same issue. What's the status of this? |
So it's a great discussion, but how do we compile the code like that? |
@innokenty the usual setting is with Babel and Webpack, shout out if need more details. |
Flow v0.75 (which we'll upgrade to with RN v0.55) requires an explicit annotation on types in "input position" -- which includes the type parameter to `Array<>` -- in module exports. See discussion in these issue threads (the closest thing to documentation that exists, as is typical for Flow): facebook/flow#6828 facebook/flow#6151
I hope this will help someone out: An example:
|
I'm trying to upgrade my version to Maps like this are causing me the errors: renderDescriptions() {
return this.props.products.map((item, index) => (
<ol
key={`description-${index}`}
>
{this.renderDescriptionItem(item.descriptions)}
</ol>
));
} Changing to this solves the problem: renderDescriptions() {
const descriptions: React.Element<'ol'>[] = this.props.products.map((item, index) => (
<ol
key={`description-${index}`}
>
{this.renderDescriptionItem(item.descriptions)}
</ol>
));
return descriptions;
} But i'm still curious about something. There is a Medium post by Flow about this change, in which they state:
So why are we getting errors in built-in map functions, like the one above? My |
@luislhl In your first example... Flow knows that In your second code block, you specify the type of |
With an array of
using
I fixed this error using: |
I think if you use $ReadOnlyArray it doesn't require annotation |
I ran into this issue today with the following code: configuredFlags() {
return this.props.availableFlags.filter((flag: string) => {
return this.initialFlagStatus(flag) !== 'default';
});
} The solution was to define a return type for the configuredFlags(): Array<string> {
return this.props.availableFlags.filter((flag: string) => {
return this.initialFlagStatus(flag) !== 'default';
});
} |
Today I ran on this weird error. I had this:
And I was getting this error:
Luckly I was able to properly "Cast" by doing this:
But I don't understand why flow was not capable of inferring what was already declared, slice should not change the data in any way, and it doesn't allow you to provide parameters, so this was kind of a dead end until I came to this funny cast. |
We now requires annotation on exported stuff unconditionally. |
On flow v0.70, array methods / generic methods seem to be broken. This only happens when the function is exported, and uses an array method. Marking a return type of
any[]
makes flow pass.Reproduction here:
Errors:
The text was updated successfully, but these errors were encountered: