react-docgen
react-docgen copied to clipboard
Add support for intersection types/interfaces
This change adds support for complex union interfaces like the following:
import { MyType } from 'MyType';
import { MySecondType } from 'MySecondType';
type SuperType = { name: string } & (MyType | MySecondType);
const x: SuperType = {};
Expected result would be something like:
{
"elements": [
{
"key": "name",
"value": {
"name": "string",
"required": true,
},
},
{
"key": "a",
"value": {
"name": "number",
"required": true,
},
},
{
"key": "b",
"value": {
"name": "string",
"required": false,
},
},
],
"name": "intersection",
"raw": "{ name: string } & (MyType | MySecondType)",
}
I have added a test to address this, and i was fighting pretty hard with the existing types (chat gpt was a big help) - but i'm pretty sure there is room for improvement, help would be appreciated :)
TODOS
- [x] Modify "handleTSIntersectionType" in "getTSType" to fully resolve all involved types
- [x] Add new "handleTSParenthesizedType" to "getTSType" to fully resolve all involved types
- [x] Add new test for "getTSType", "deep resolve intersection types"
- [ ] Get rid of "any" typings and stupid typeguards in "getTSType" i have added to "make it work"
- [x] Support TS union types in "codeTypeHelper"
- [x] Adjust existing test "does not support union proptypes" into "does support union proptypes"
- [x] Add tests for the new utility "mergeTSIntersectionTypes"
- [ ] Create changeset, this is definitely a new version
Fixes #914
⚠️ No Changeset found
Latest commit: 155dcb7fcb4e444b65b87a4d08359eb43591eb67
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
This PR includes no changesets
When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR
Deploy Preview for react-docgen ready!
| Name | Link |
|---|---|
| Latest commit | 155dcb7fcb4e444b65b87a4d08359eb43591eb67 |
| Latest deploy log | https://app.netlify.com/sites/react-docgen/deploys/67b43d99ef80ad0008890297 |
| Deploy Preview | https://deploy-preview-915--react-docgen.netlify.app |
| Preview on mobile | Toggle QR Code...Use your smartphone camera to open QR code link. |
To edit notification comments on pull requests, go to your Netlify site configuration.
Thanks a lot, i will try to find some time to review this in the coming weeks. Probably will take a little while.
Hey i needed to convert the PR to a draft for the moment. I realized that the codeHelper needs similar modifications to support union types and i'm not done yet. And i'm pretty sure i have not tested all possible cases yet - i want to extend the tests to fully cover that ;-)
Hi @danez, i'm happy now with the output produced - with these changes in place, our team would be unblocked to finally migrate from react-docgen-typescript to react-docgen. But i'm sure there are still missing pieces. For one, the changeset. I think we should create a new minor version for that, what do you think?
The other thing is the typing - i'm not happy with that what i've done in getTSType. Another interesting find for me - after adding the support of "intersection types" in getTSType, i still got the same output from the parsed component. I had to add the same kind of changes to the codeTypeHandler too until i've got the output i want. So i guess, my extension of getTSType was somewhat useless? :D
And finally, please help me out with flow types - i have never really worked with flow typing and i already failed trying to setup an example repo for that. For now i'm going to publish a fork for this package with all changes so we can test it thoroughly in our e-commerce platform.
Any update on this?
Mhm sorry i moved away from the project as it is extremely far away from complete typescript support. On the other hand, the project here seems dead as no maintainer is really active anymore