babel-plugin-flow-react-proptypes icon indicating copy to clipboard operation
babel-plugin-flow-react-proptypes copied to clipboard

Feature: Interface support

Open kubijo opened this issue 6 years ago • 2 comments

Hi.

What is the state state of affairs with the support of interface types? It seems like that as of now, those do not get bpfrpt_proptype_${name} propType validators.

I can imagine that the full support mightn't be possible in a sensible complexity level (combination of indexers and methods *), however we could handle them essentially as object types...

interface Pager {
    goPrev(stepSize?: number): void,
    goNext(stepSize?: number): void,
    goto(number): void,
    hasNext(): boolean,
    hasPrev(): boolean,
}

would become:

// import PT from 'prop-types';
PT.shape({
    goPrev: PT.func.isRequired,
    goNext: PT.func.isRequired,
    goto: PT.func.isRequired,
    hasNext: PT.bool.isRequired,
    hasPrev: PT.bool.isRequired,
})

* indexers + properties

It actually is kind of possible to create a prop-type validator using prop-types, but it's pretty kinky. You'd essentially combine objectOf(...allPossibleTypesIncludingExplicit) & shape({ ...explicitAttributes }) or we could write a custom utility to check those (I wouldn't bother with multi-indexer ones)

kubijo avatar Jan 30 '18 15:01 kubijo

ps.: those pesky indexers could also be just skipped for simplicity sake

kubijo avatar Jan 30 '18 15:01 kubijo

Seems like a good thing to have, but I don't have time to implement it. PRs welcome.

If you take this, prefer a simple solution that works in the basic cases. Also, we don't read other files during the transform, so it'd likely end up exporting the same as a shape type.

// input
export type Foo = {
  x: number,
};

// output
var bpfrpt_proptype_Foo = {
  x: PropTypes.number.isRequired
};
import PropTypes from "prop-types";
export { bpfrpt_proptype_Foo };

I don't think there's any way to make it actually use instanceof checks, but this should be good enough.

brigand avatar Jan 30 '18 15:01 brigand