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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

rvetere
Copy link

@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"
  • Add more test cases to make sure everything is working like we are used to with "react-docgen-typescript"
  • Make sure all of this is also working with flow types
  • Create changeset, this is definitely a new version

Fixes #914

Copy link

changeset-bot bot commented Apr 26, 2024

⚠️ No Changeset found

Latest commit: 79bc139

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 79bc139
🔍 Latest deploy log https://app.netlify.com/sites/react-docgen/deploys/66309c1cb4073200081721e0
😎 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
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
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.

babel parser plugin "typescript" does not behave like typescript compiler
2 participants