-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Flow 0.53 prop types #1376
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
Comments
We currently have test cases like this. Are these still valid with Flow 0.53? type Person = {
firstname: string
};
class Hello extends React.Component<void, { person: Person }, void> {
render () {
return <div>Hello {this.props.person.lastname}</div>;
}
} |
Why |
@ljharb I was wondering about that too. However, Flow's official documentation says this change to the Not sure if we can get someone from the Flow team to chime in, but it seems totally unnecessary to me as well (and potentially troublesome when using Webpack's Tree Shaking). |
That's horrific; it's one of the least usable parts of TypeScript. |
and yes, it also makes tree-shaking impossible unless you can statically determine every key accessed on a ModuleRecord. |
Yep, this might not be needed for the dummy example provided above, but will be as soon as you need one of the exposed types. https://github.com/facebook/flow/blob/v0.53/Changelog.md#0530 |
I assume they are using the For example, import * as React from 'react';
let element: React.Node = ...; is equivalent to import React from 'react';
import type {Node} from 'react';
let element: Node = ...; However, the first example is "cleaner". |
If the types are in the |
This is also a problem I have come across after upgrading to I think Flow wants you explicitly use Reacts utility types, and import React, { type Node } form 'react' Note, theres a brief outline of this towards the end of the Components section on Flow docs |
The outline makes sense; in that case it's a poor choice for a codemod unless it's super smart about when it uses *. |
Temp fix appears to be to use both the new required generics approach enforced by [email protected], and the (now) legacy approach. For example: import React, { type Element } from 'react'
type Props = {
exA: number
};
type State = {
exB: string
};
//
// Latest version of Flow wants you to use generics
// but current version of Eslint plugin warns against react/prop-types
class Foo extends React.Component<Props, State>{
props: Props; // This will keep "eslint-plugin-react" happy without interfering with Flow new generics req.
state = {
exB: 'initial val'
};
render(): Element<'div'>{
const { exA } = this.props;
const { exB } = this.state;
return (
<div>
<ul>
<li>Props val: { exA }</li>
<li>(initial) State val: { exB }</li>
</ul>
</div>
);
}
} |
To summarize with the exact reasoning: import React from 'react'; // `React` is an object
import * as React from 'react'; // `React` is a namespace Types can exist in namespaces, but not in objects. You can also import specific bindings for both JavaScript values and Flow types: import { Component } from 'react';
import type { Node } from 'react';
import { type Node } from 'react'; And combine them if you want to import both the object and a type: import React, { type Node } from 'react'; |
Because the type parameter signature changes in Flow 0.53 from: React.Component<DefaultProps, Props, State> to: React.Component<Props>
// or
React.Component<Props, State> This ESlint plugin could be updated to pull props from: React.Component { props: Props; }
React.Component<X, Props, X> {}
React.Component<Props, X> {}
React.Component<Props> {} And it will support all versions of Flow |
@thejameskyle in flow <0.53, was this also valid syntax: If so, then we cannot distinguish properly between the old and the new syntax. If we expect Ofcourse we could do it so that if there are 3 arguments we check the second argument, and if there are 2 or 1 then we check the first. I just assumed that people may already have code like Edit: We could also enforce the name to be
The current idea we discussed in the PR is to introduce a |
No, in previous versions of Flow, you could only have no args or three args: // Flow <0.53
class extends React.Component {}
class extends React.Component<DefaultProps, Props, State> {}
class extends React.Component { props: Props; }
class extends React.Component<DefaultProps, Props, State> { props: Props; }
// Flow >=0.53
class extends React.Component {}
class extends React.Component<Props> {}
class extends React.Component<Props, State> {}
class extends React.Component { props: Props; }
class extends React.Component<Props> { props: Props; }
class extends React.Component<Props, State> { props: Props; } |
Great. I think we can use that approach then! 👍 3 arguments: check the second argument. Thanks for your input. 😄 |
@oliviertassinari this is because you're supposed to use the |
@ljharb It's a different topic. This change introduces the warnings with the -import React from 'react';
+import * as React from 'react'; And it's fixed with the |
ohhh i see what you mean, gotcha. that seems like a reason to use |
This is now in master. It would be great if someone could try on their codebase(s) and confirm everything works! |
@jseminck I pulled in https://github.com/yannickcr/eslint-plugin-react#e2f6460a63bd0cd0dca0c8308e35ff831d5161ed but I'm still not getting prop validation detection with flow types. I also added:
to my |
Thanks for testing it, @johnhaley81. Appreciate that! 🎉 Can you confirm you are using the master release? I did this to get it: rm -rf node_modules/eslint-plugin-react
yarn add https://github.com/yannickcr/eslint-plugin-react -D One way to test it is to execute this command: const versionUtil = require('../util/version');
propsParameterPosition = versionUtil.testFlowVersion(context, '0.53.0') ? 0 : 1; You shouldn't have to add the Here is my example which does seem to work. |
Hi,
with the latest
[email protected]
the Type detection for prop types is broken.Before
After
The text was updated successfully, but these errors were encountered: