Skip to content
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

Add support for intersection types/interfaces #915

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

rvetere
Copy link
Contributor

@rvetere rvetere commented Apr 26, 2024

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

  • Modify "handleTSIntersectionType" in "getTSType" to fully resolve all involved types
  • Add new "handleTSParenthesizedType" to "getTSType" to fully resolve all involved types
  • 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"
  • Support TS union types in "codeTypeHelper"
  • Adjust existing test "does not support union proptypes" into "does support union proptypes"
  • Add tests for the new utility "mergeTSIntersectionTypes"
  • Create changeset, this is definitely a new version

Fixes #914

Copy link

changeset-bot bot commented Apr 26, 2024

⚠️ No Changeset found

Latest commit: ae337cc

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

Copy link

netlify bot commented Apr 26, 2024

Deploy Preview for react-docgen ready!

Name Link
🔨 Latest commit ae337cc
🔍 Latest deploy log https://app.netlify.com/sites/react-docgen/deploys/66557c242816c0000844812f
😎 Deploy Preview https://deploy-preview-915--react-docgen.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@danez
Copy link
Collaborator

danez commented Apr 28, 2024

Thanks a lot, i will try to find some time to review this in the coming weeks. Probably will take a little while.

@rvetere rvetere marked this pull request as draft April 29, 2024 06:12
@rvetere
Copy link
Contributor Author

rvetere commented Apr 29, 2024

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 ;-)

@rvetere rvetere changed the title Add support for union interfaces Add support for intersection types/interfaces May 2, 2024
@rvetere rvetere marked this pull request as ready for review May 14, 2024 06:03
@rvetere
Copy link
Contributor Author

rvetere commented May 14, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mo support for typescript intersection and parenthesized types
2 participants