extract-react-types
extract-react-types copied to clipboard
Add converter for TSTypeOperator
We have a complicated type that looks like:
// HtmlAttributes = AllHTMLAttributes - OnlyButtonProps
// We do this so onClick, and other props that overlap with html attributes,
// have the type defined by OnlyButtonProps.
type HtmlAttributes = Pick<
React.AllHTMLAttributes<HTMLElement>,
Exclude<keyof React.AllHTMLAttributes<HTMLElement>, keyof OnlyButtonProps>
>;
// This ends up being the Button prop API
type ButtonProps = HtmlAttributes & OnlyButtonProps
Running extract-react-types on this type throws the following error:
../packages/core/button/src/components/Button.tsx (../node_modules/extract-react-types-loader!../packages/core/button/src/components/Button.tsx) Module build failed (from ../node_modules/extract-react-types-loader/index.js): Error: Missing converter for: TSTypeOperator
It's pretty understandable something like this would not be supported by extract-react-types
. This issue is really to discuss how type system specific features should (or shouldn't) be supported. My workaround at the moment is just creating a new file with a dummy component with the prop types I want extracted.
Maybe this highlights a need for a magic comment that extract-react-types
will look for and extract if it exists.
In general, we’ve put magic into pretty-proptypes (you can override all type definitions using its API).
Since it’s an AST type, we should support it. What part is actually the TSTypeOperator that is causing the fail? Once we have a generic converter for that, we can look at supporting specific custom cases.
Precedent for this is how we use Array<>
, which is technically not a type, but we specifically look for it and treat it differently.
There’s also things such as how we resolve type combinations or spread operators, where we do logic to resolve these to the intended type.
If you don’t want to start work on solving this, a link to ast explorer showing the simplest possible instance of TSTypeOperator and it’s AST, so we can look at what hits would need to be preserved to be able to move forwards.
(I assume the complexity here is not the type operator, but the fact you may want to resolve the pick, right?)
Oh, Pick and Exclude.
And Omit