Skip to content

fix: read the type wrapped by TypeScript readonly arrays and tuples - #1213

Open
josunghoon53 wants to merge 1 commit into
reactjs:mainfrom
josunghoon53:fix/ts-readonly-type-operator
Open

josunghoon53 wants to merge 1 commit into
reactjs:mainfrom
josunghoon53:fix/ts-readonly-type-operator

Conversation

@josunghoon53

Copy link
Copy Markdown

Summary

Array and tuple types wrapped in the TypeScript readonly operator come out as unknown, because handleTSTypeOperator returns null for every operator except keyof.

type Props = {
  names: readonly string[];          // before: { name: 'unknown' }
  pair: readonly [string, number];   // before: { name: 'unknown' }
};

readonly string[] accepts the same values as string[]. So this change reads the wrapped type as is and keeps the original text, with readonly, only in raw. The result is Array and tuple, not a differently named type like ReadonlyArray<string>.

This was reported in storybookjs/storybook#24097. It is a different case from #1147, which handles Readonly<T> (a TSTypeReference, not a TSTypeOperator).

Changes

  • react-docgen: when the operator is readonly, handleTSTypeOperator reads the wrapped type and sets raw to the original text.
  • @react-docgen/typescript: it runs the same integration cases, so it gets the same handling. Before the change it returned only a string with readonly removed, like { name: 'string[]' }.
  • Added the integration case typescript/types/readonly-arrays. It covers readonly string[], readonly [string, number] and readonly (string | number)[].

AI use: I used Claude to find the cause and to draft the fix, the tests and this description. I ran the checks below myself.

Checks

  • The new integration case fails before the change in both react-docgen and @react-docgen/typescript, and passes after it.
  • pnpm test: all 6 projects pass (react-docgen 1,515 passed, @react-docgen/typescript 157 passed and 1 skipped).
  • pnpm lint, Prettier on the changed files and git diff --check pass.
  • With the built react-docgen, the output for string[], ReadonlyArray<string> and keyof is unchanged.

@netlify

netlify Bot commented Dec 6, 2026

Copy link
Copy Markdown

👷 Deploy request for react-docgen pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 3825d58

@changeset-bot

changeset-bot Bot commented Dec 6, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3825d58

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
react-docgen Patch
@react-docgen/cli Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

This branch has not been deployed

No deployments
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.

1 participant