-
Notifications
You must be signed in to change notification settings - Fork 5
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
[ADS-6363][ASC-22] feat: add type definitions #1350
Conversation
b3e0ab7
to
45ec224
Compare
Codecov Report
@@ Coverage Diff @@
## master #1350 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 94 92 -2
Lines 1429 1429
Branches 362 362
=========================================
Hits 1429 1429
Continue to review full report at Codecov.
|
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 great work. 👍
i will find some time to locally test this.
cf74a51
to
be80af7
Compare
// Merge with the public folder | ||
if (process.env.NODE_ENV === 'production') { | ||
process.env.DEMO_ASSETS ? copyDemoAssets() : copyPublicFolder(); |
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 don't think copyPublicFolder()
is ever used (DEMO_ASSETS
and production
are always used together).
Can anyone confirm?
export interface AlertProps { | ||
/** | ||
* ['success', 'info', 'warning', 'danger'] | ||
*/ |
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 comment is a bit duplicated though, so is other similar one
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.
The redundant prop comments are for to the docs site's prop descriptions. The type generation just pulls all the comments from the 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.
When we move to storybook, the prop values will be extracted properly so we'll be able to remove these comments then.
Great work! |
4565d36
444df32
to
4565d36
Compare
e1b0916
to
b1bb5e3
Compare
console.log(chalk.green.bold(`Generated type defs for ${component.componentName}`)); | ||
|
||
const output = typesPostFixes(component.componentName, result); | ||
const prettifiedOutput = prettier.format(output, { parser: 'typescript', ...pkg.prettier }); |
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.
added prettier settings from package.json so output is consistently formatted
b13c4db
to
afeb5a3
Compare
afeb5a3
to
be74499
Compare
Description
react-to-typescript-definitions
to convert PropTypes to types. Types are saved tod.ts
files alongside each component.scripts/generate-types/typesPostFixes.js
A few surface changes were done for all of this to operate smoothly
Benefits
Type definitions unlock a huge boost for developer experience, even for plain JS projects. That's the driving motivator behind this PR.
With a typescript enabled code editor like VSCode, all component props and their definitions and descriptions are available and will autocomplete. Not only is this a huge productivity boost, it also reduces the chance for misspelled props or prop values.
The gifs below show some examples of: prop autocompletion/suggestion, prop descriptions via comments, type autocompletion/suggestion, type checking, member expression types. (
// @ts-check
is not needed, I'm just using it here to show when types would be incorrect.)Usage
Generate types for all components
npm run generate-types
More...
Generate types for a single component
npm run generate-types -- --only=Button
Generate types for a single component (this will run for all jsx files in the folder matching Button)
npm run generate-types -- --only=Button
Generate types for a specific file
npm run generate-types -- --only=Button/index.jsx
globs are accepted:
npm run generate-types -- --only="RichTe*
Copy generated types to es/*
npm run generate-types -- --copy
Generate types with debugging output
npm run generate-types -- --debug
Check validity of generated types
npm run type-check
Proposed workflow for updating/adding types
Maintaining consistency of the type definitions is important, and it's up to us as contributors to do so.
Note that I haven't added
generate-types
to thedist
script. This is to avoid any unintentional breaking changes slipping through. The onus is on the dev to generate and commit type definitions when making changes to or adding new components/props.In most cases it will be as simple as running
npm run generate-types
, then verifying and committing the changes.Of course, the Prop Types themselves are responsible for the type definitions, so please make sure Prop Types are as correct as possible (e.g, if you know the prop accepts certain strings only, the definition should be
PropTypes.oneOf(['string1', 'string2'])
as opposed toPropTypes.string
Prop descriptions
Comments (a la JSDoc) above a prop type will carry across to the type def, so feel free to describe the prop there to help other users!
Advanced usage
Advanced usage, e.g when wrapping types of a 3rd party library into a component, should be manually adjusted in
scripts/generate-types/typesPostFixes.js
.Caveats
PropTypes.shape
will not workThis works:
This doesn't work:
Other options to explore/discuss
@types/adslot-ui
to separate them furtherI feel that 1 would be potentially better due to being able to define the types as precisely as we need, without having to rely on modifications or be limited by the PropTypes lib – but would require more work and TS knowledge from contributors.
Does this PR introduce a breaking change?
Any typescript projects using
adslot-ui
may need to update types accordingly. E.g if custom types were added they should be replaced with the types from the library.Manual testing step?
Screenshots (if appropriate):