milesj / babel-plugin-typescript-to-proptypes

Generate React PropTypes from TypeScript interfaces or type aliases.
MIT License
367 stars 25 forks source link

Does not work with Pick<> #15

Closed city41 closed 3 years ago

city41 commented 5 years ago

This is a great plugin, thanks for making it!

I have found it doesn't work with Pick<>. I have forked the repo and added a fixture

import React from 'react';

interface SomeType {
  someString: string;
  someNumber: number;
}

type PropData = Pick<SomeType, 'someString'>;

type Props = PropData & {additionalString: string};

export const PickFromTypeFunctionComponent: React.FunctionComponent<
  Props
> = () => {
  return null;
};

and the resulting snapshot is

exports[`babel-plugin-typescript-to-proptypes transforms ./fixtures/pick-from-type.ts 1`] = `
"import _pt from 'prop-types';
import React from 'react';
interface SomeType {
  someString: string;
  someNumber: number;
}
type PropData = Pick<SomeType, 'someString'>;
type Props = PropData & {
  additionalString: string;
};
export const PickFromTypeFunctionComponent: React.FunctionComponent<Props> = () => {
  return null;
};
PickFromTypeFunctionComponent.propTypes = {
  additionalString: _pt.string.isRequired
};"
`;

I am currently investigating and seeing if I can add support for this. If I succeed, I can send a PR your way.

city41 commented 5 years ago

On the surface this doesn't look very straightforward. It's possible to dig in and detect Pick, but it just snowballs from there, like what about Pick<A, keyof Foo>? need to detect keyof, etc.

milesj commented 5 years ago

@city41 Yeah this might be a bit complicated. May be one of those features that require the TS type checker to work correctly.

milesj commented 3 years ago

Not going to support this.