Skip to content

feat: read props from TypeScript function type annotations - #1201

Open
n-hiraha wants to merge 1 commit into
reactjs:mainfrom
n-hiraha:ts-call-signature-props
Open

n-hiraha wants to merge 1 commit into
reactjs:mainfrom
n-hiraha:ts-call-signature-props

Conversation

@n-hiraha

Copy link
Copy Markdown

Fixes #897.

What was wrong

When a component variable is typed with a function type instead of React.FC, react-docgen found no props:

interface Props {
  /** foo doc */
  foo: string;
}
type FnType = (props: Props) => React.ReactElement;

export const Fn1: FnType = (props) => <div>{props.foo}</div>;
// before: props is null
// after:  { foo: { required: true, tsType: { name: 'string' }, description: 'foo doc' } }

What this changes

findAssignedVariableType in getTypeFromReactComponent.ts already reads the props type from React.FC<Props> and friends. If the annotation is not one of those, it now takes the type of the first parameter of:

  • a function type, inline (const C: (props: Props) => X = ...) or through a type alias
  • the first call signature of a type literal or an interface (interface Fn { (props: Props): X })
  • a generic alias or interface, where the parameter is the type parameter (type Component<P> = (props: P) => X used as Component<Props>). Here the type argument is used.

Two behaviours follow the existing React.FC path, and I kept them the same:

  • If the arrow function's parameter is annotated as well, the props of both types are collected, as with const C: React.FC<A> = (props: B) => ... today.
  • With overloaded call signatures, the first one is used.

Tests

  • I added 7 tests to codeTypeHandler-test.ts. Without the change in getTypeFromReactComponent.ts, the 6 positive cases fail. The 7th checks that a type which is not a function type still gives no props.
  • pnpm test passes everywhere. react-docgen has 1521 tests, and the core, babel, typescript, hermes and cli packages pass too.
  • pnpm lint, tsc --noEmit and Prettier are clean on the changed files.
  • I also ran the example from the issue through the built parse(): Fn1 and the call-signature variant now list foo, where both used to give null.

@netlify

netlify Bot commented Nov 12, 2026

Copy link
Copy Markdown

👷 Deploy request for react-docgen pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 763148b

@changeset-bot

changeset-bot Bot commented Nov 12, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 763148b

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 Minor
@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