Skip to content

Feature: Option to remove | undefined from optional prop types #1134

Description

@unional

Problem

When using TypeScript's explicit optional property pattern (prop?: Type | undefined), react-docgen displays the type as union instead of showing the actual type.

This pattern is common in codebases using exactOptionalPropertyTypes: true in tsconfig, which requires explicit | undefined for optional properties.

Example

interface ButtonProps {
  onClick?: ((event: MouseEvent) => void) | undefined
  variant?: 'primary' | 'secondary' | undefined
  icon?: ReactNode | undefined
}

In Storybook's props table (using react-docgen), these all display as "union" instead of:

  • (event: MouseEvent) => void
  • 'primary' | 'secondary'
  • ReactNode

Requested Feature

Add a parser option similar to react-docgen-typescript's shouldRemoveUndefinedFromOptional:

{
  shouldRemoveUndefinedFromOptional: true
}

When enabled, this would:

  1. Detect optional properties (those ending with ?)
  2. Strip | undefined from their type representation
  3. Display the actual meaningful type instead of "union"

Prior Art

react-docgen-typescript has this feature via the shouldRemoveUndefinedFromOptional option:
https://github.com/styleguidist/react-docgen-typescript#parseroptions

Workaround

Currently, Storybook users must manually specify table.type.summary for every affected prop:

argTypes: {
  onClick: {
    table: { type: { summary: '(event: MouseEvent) => void' } }
  }
}

This is tedious and error-prone for large component libraries.

Environment

  • react-docgen: latest
  • TypeScript: 7.x with exactOptionalPropertyTypes: true
  • Storybook: 10.x using reactDocgen: 'react-docgen'

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions