mskelton / ratchet

Codemod to convert React PropTypes to TypeScript types.
https://mskelton.dev/ratchet
ISC License
138 stars 14 forks source link

Issues with parsing destructured import #44

Open kandji-joe opened 2 years ago

kandji-joe commented 2 years ago

When using both the script and online tool, if I use a file with destructured prop-types the types that get generated are unknown.

For example:

import { bool, string } from 'prop-types'
...
Component.propTypes = { isDisabled: bool, name: string }

will generate

interface ComponentProps {
  isDisabled?: unknown;
  name?: unknown;
}
mskelton commented 2 years ago

Doing this would require more thorough scope analysis to determine what bool is since it could be like this:

const bool = PropTypes.string

Not saying that's likely, just that to do this properly would require properly checking the scope of variables passed down. If you'd like to take a stab at this feel free, but it's not something I've seen before so I'm not sure I'll end up taking this on.