Tsdx: Support for generating prop types?

Created on 8 Jan 2020  ·  20Comments  ·  Source: formium/tsdx

Current Behavior

Right now, projects made with TSDX doesn't generate prop types automatically.

Desired Behavior

I'd like to have an option (or a way to extend the config) so that I can generate prop types in the development version of the package I create.

Suggested Solution

We could add a config option to add this babel plugin:

https://www.npmjs.com/package/babel-plugin-typescript-to-proptypes

Or write something similar, idk 🤷‍♂️

Who does this impact? Who is this for?

We're creating a component library in TypeScript, but many of our consumers don't use TypeScript. It would be great to add prop types as a fallback runtime verification for those that aren't using types.

This translates well to many packages created with TSDX, I would assume.

feature integration

Most helpful comment

TSDX is a great way to create packages, and my component library is just a bunch of packages, really. :) I don't see why I couldn't use TSDX for this.

All 20 comments

@selbekk you have such option: https://github.com/jaredpalmer/tsdx#babel

🤔 I tried to generate a repro:

  • bootstrap a new project with tsdx create example (with React)
  • add some types to the generated src/index.tsx component
  • install the babel-plugin-typescript-to-proptypes
  • add a .babelrc with the following config
{
  "plugins": ["babel-plugin-typescript-to-proptypes"]
}

And it didn't make a difference to the output at all. Am I missing an important step?

Here's a repro: https://github.com/selbekk/repro-tsdx-418

@selbekk thinking guess this plugin is not applicable with tsdx cause babel get already compiled sources after rollup (not original typescrpt) https://github.com/rollup/rollup-plugin-babel#why

I'll check it more deeply soon...

TSDX is a great way to create packages, and my component library is just a bunch of packages, really. :) I don't see why I couldn't use TSDX for this.

yep, tsdx is great! :) but not a silver bullet.. but this is interesting challenge so I'll try to solve it :)

@selbekk I have solution, but it's not so simple))
https://github.com/selbekk/repro-tsdx-418/pull/1
Screenshot from 2020-01-08 20-38-22

btw, I also prefer to use typescript only as type checker and type defs generator - not as transpiler and compiler - this is for babel which is first class transpiler :)

I'm happy that works, and thanks for the incredible effort solving that (!), but switching out the entire compiler is a bit convoluted. Is there no way this could be built in to tsdx itself? as a rollup plugin or whatever?

https://github.com/milesj/babel-plugin-typescript-to-proptypes/issues/9

as I see the only way is to extend rollup-plugin-typescript2 with custom typescript transformer
(like this one https://github.com/ambroseus/tsdx/pull/1/commits/f20dc6912d1d1cabba1c4daec2c68c84c47806e1)

but as @jaredpalmer says this is not tsdx way..

@selbekk found another solution: https://github.com/gcanti/prop-types-ts
but...

As I can see from the documentation, this is not a fully compatible PropTypes generator, but instead a replacement, which uses it's own implementation for type checking at runtime. I would prefer my PropTypes to be fully compatible with the official implementation though

...to many but :smile:

well....
https://stackoverflow.com/questions/54060057/generating-proptypes-for-react-components-written-in-typescript

As for me, I think I will have to keep both TypeScript typings and manual PropTypes in sync for now, because I'm not using Babel and I don't really like a custom PropTypes implementation. Maybe, one sunny day, we will find some resources to actually write our own PropTypes generator in our company and will share it with everyone

it looks like TypeScript guys doesn't see this as a viable use case. It looks like they think that TypeScript typings are a replacement for PropTypes. Even the discussion issue is locked on this subject..

There is also PropTypes.InferProps from the prop-types package, which is detailed well in https://dev.to/busypeoples/notes-on-typescript-inferring-react-proptypes-1g88

Hey guys! It will be awesome! When will you be in the roadmap?

@acasazza please read the comments.
There isn't currently a viable approach, so there can't be a roadmap.

@agilgur5, I tried babel-plugin-typescript-to-proptypes, and it seems the better solution. Can I make a tsdx template? What do you think?

@acasazza again, please read the comments before notifying everyone...

babel-plugin-typescript-to-proptypes is not compatible with TSDX.
It requires using @babel/preset-typescript to strip types whereas TSDX fully compiles the TypeScript with rollup-plugin-typescript2.

That was stated already in above comments and why I said it's not viable.

@andriyor thanks)
material-ui alredy use it https://github.com/mui-org/material-ui/pull/21497

Was this page helpful?
0 / 5 - 0 ratings