-
-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: declaration style #76
Conversation
README.md
Outdated
@@ -164,3 +165,39 @@ MyComponent.propTypes = { | |||
}, | |||
} | |||
``` | |||
|
|||
### `--prefer-type-aliases` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I would prefer to use an option of --declaration-style
with the default value being interface
.
### `--prefer-type-aliases` | |
### `--declaration-style=type` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed
README.md
Outdated
@@ -13,6 +13,7 @@ Codemod to convert React PropTypes to TypeScript types. | |||
- Supports files with multiple components | |||
- Copies JSDoc comments to the generated TypeScript types | |||
- Option to remove or preserve PropTypes after converting to TS | |||
- Choose between interface and type alias |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a important feature for the highlights section.
- Choose between interface and type alias |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed
@@ -0,0 +1,11 @@ | |||
import PropTypes from "prop-types" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One test is plenty, definitely don't need to duplicate every single test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed
transform.ts
Outdated
@@ -260,7 +280,9 @@ function addFunctionTSTypes( | |||
componentTypes: CollectedTypes | |||
) { | |||
source.forEach((path) => { | |||
const typeName = createInterface(path, componentTypes) | |||
const typeName = options.preferTypeAliases | |||
? createTypeAlias(path, componentTypes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's just rename createInterface
to something more generic and do this as a conditional in that function. Don't want all the duplicate code this currently has.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed
j.identifier(typeName), | ||
j.tsInterfaceBody(types.types.map(createPropertySignature)) | ||
) | ||
options.declarationStyle === "type" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only check if the value is type
. In this way, in case of interface
or unaccepted value the default will be to output interface.
LMK if this behavior is ok or if you'd rather have a throw or else in case of not listed declaration style value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is perfect!
// --declaration-style=type | ||
defineTest( | ||
__dirname, | ||
transform, | ||
{ "declaration-style": "type" }, | ||
"declaration-style-type" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Way less tests than in the first draft ;)
LMK if you'd like some additional ones
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, this seems fine.
} | ||
|
||
h1 { | ||
font-size: 22px; | ||
margin: 0; | ||
margin-right: auto; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your feedbacks @mskelton ! |
Thanks for those updates, great work on this feature!!! |
Description
This PR introduce an option to transform prop-types into type aliases instead of interfaces.
How it works
A
--prefer-type-aliases
flag is introduced, which will then toggle type aliases generation in place of interfaces.When the flag is unused interfaces are emitted like before the patch.
Screenshot
Meta
Checklist