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

Add further support for Flow prop types. #1138

Closed
wants to merge 4 commits into from

Conversation

phpnode
Copy link
Contributor

@phpnode phpnode commented Apr 3, 2017

This adds support for constructions like this:

type Props = {name: string};
class X extends React.Component<void, Props, void> {
  ...
}

Which is the preferred way to declare prop types using Flow.

The less preferred syntax is still supported:

class Z extends React.Component {
  props: Props;
}

Fixes #453, #913

phpnode added 2 commits April 3, 2017 20:18
This adds support for constructions like this:

    type Props = {name: string};
    class X extends React.Component<void, Props, void> {
      ...
    }

Which is the preferred way to declare prop types using Flow.

The less preferred syntax is still supported:

    class Z extends React.Component {
      props: Props;
    }

Fixes jsx-eslint#453
@ljharb
Copy link
Member

ljharb commented Apr 3, 2017

Why "void, Props, void"? props are the first argument to the constructor; what's the first void for?

@phpnode
Copy link
Contributor Author

phpnode commented Apr 3, 2017

@ljharb the first parameter is the default properties, e.g.

type Props = {
  name: string
};
const defaultProps = {
  name: 'Foo'
};

type State = {
  foo: string
}

class Y extends React.Component<typeof defaultProps, Props, State> {
  state = { foo: 'bar' };
  static defaultProps = defaultProps;
  //  ...
}

@ljharb
Copy link
Member

ljharb commented Apr 3, 2017

can you explain a bit about how Flow handles that syntax in a generic way, such that I'd use it on a non-React component?

@phpnode
Copy link
Contributor Author

phpnode commented Apr 3, 2017

@ljharb I guess there are two things here, the type parameters and $Diff.

Type Parameters

// So this is a generic container
// `T` can be anything
class Container<T> {
  items: T[];
  
  push (...args: T[]) {
    this.items.push(...args);
    return this;
  }
}

// When extending we can pass the type parameter
// to the super class, making the type more specialized
class StringContainer extends Container<string> {
  
}

const c = new Container();
c.push(true, 123, 'this is fine'); // all good

const sc = new StringContainer();
sc.push('this is fine'); 
sc.push(false); // flow error

$Diff<A, B>

It takes two (object) types and returns the properties that exist in one and not the other, hopefully this is helpful:

type APIOptions = {
  hostname: string,
  port: number
};

const defaultOptions = {
  port: 80  
};


// Note: I don't think $Subtype<> should really be required,
// and it's probably a flow bug. You may be able to omit it.
type RequiredOptions = $Subtype<$Diff<APIOptions, typeof defaultOptions>>;

class API {
  opts: APIOptions;

  constructor (options: RequiredOptions) {
	this.opts = Object.assign({}, defaultOptions, options);
  }

  getURL () {
    return this.opts.hostname + ':' + this.opts.port;
  }
}

const api = new API({
  hostname: 'foo'
});

console.log(api.getURL());

@phpnode
Copy link
Contributor Author

phpnode commented Apr 11, 2017

@ljharb @yannickcr friendly ping - this is working locally on quite a large codebase so quite confident it works, anything else I need to do before it can be merged?

ljharb
ljharb previously requested changes May 2, 2017
Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general this looks fine, but I don't use Flow, so I don't think I'm qualified to review this rule.

if (seen === void 0) {
// Keeps track of annotations we've already seen to
// prevent problems with cyclic types.
seen = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we support node 4+ now, so let's use a Set instead of an array here

@ljharb ljharb requested review from yannickcr and EvHaus May 2, 2017 21:11
@EvHaus
Copy link
Collaborator

EvHaus commented May 2, 2017

LGTM. We use Flow a lot so I can definitely keep a close eye on this once it gets released to see if it causes any issues, but the test coverage looks good already.

@ljharb ljharb dismissed their stale review May 2, 2017 22:03

Deferring to other reviewers

Copy link
Contributor

@MoOx MoOx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, huge Flow user, I would be able to test this on large codebases quickly.

@keithkml
Copy link

Very excited for this to launch!

@igorline
Copy link

Waiting for this to be merged!

@ljharb
Copy link
Member

ljharb commented Jun 11, 2017

@phpnode would you mind rebasing this on top of latest master? #1236 may also be related/overlap with this PR.

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs a rebase and then a rereview

@MoOx
Copy link
Contributor

MoOx commented Sep 5, 2017

Maybe not relevant since #1377 has been merged.

@ljharb
Copy link
Member

ljharb commented Sep 5, 2017

@jseminck can you confirm that #1377 made this PR obsolete?

@jseminck
Copy link
Contributor

jseminck commented Sep 5, 2017

I was not aware of this PR!

Yes. It should cover the same functionality as this PR and more ( the new syntax ). This PR contains more test cases though. I will add them as well to make sure they all pass.

Edit: I was a bit confused initially. I thought this PR was about no-unused-prop-types rule but it was about prop-types rule.

Just to clarify:

Even though this PR was made before, the support for Flow in prop-types was added in #382 and then enhanced in #1377

It seems all the functionality that this PR added is already supported for prop-types. But for the no-unused-prop-types rule we are still missing some functionalities, but #1412 is the first step to adding that support.

@ljharb ljharb closed this Sep 5, 2017
jetpacmonkey added a commit to jetpacmonkey/eslint-plugin-react that referenced this pull request Jan 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

7 participants