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

Functions defined as namespaces with multiple declarations not resolved correctly #2876

Closed
michaldudak opened this issue Feb 26, 2025 · 1 comment
Milestone

Comments

@michaldudak
Copy link

Search terms

prop-types, namespace, react

Expected Behavior

Function (React component in this case) parameter types should be documented when the function is also a namespace with multiple definitions.

Actual Behavior

I have a React component defined as a plain function with its props defined in a namespace:

// This works

export function MyComponent(props: MyComponent.Props) {
  const { children } = props;

  return (
    <div>{children}</div>
  );
}

export namespace MyComponent {
  export interface Props {
    children?: React.ReactNode;
  }
}

This is processed as expected. However, as soon as I add prop-types declaration to the function, Typedoc throws a warning: [warning] MyComponent.Props, defined in ./src/index.tsx, is referenced by MyComponent.props but not included in the documentation.

// This doesn't

import PropTypes from 'prop-types';

export function MyComponent(props: MyComponent.Props) {
  const { children } = props;

  return (
    <div>{children}</div>
  );
}

export namespace MyComponent {
  export interface Props {
    children?: React.ReactNode;
  }
}

MyComponent.propTypes = {
  children: PropTypes.node,
};

I suspect it may be caused by something failing to merge namespace definitions, as the resulting .d.ts looks like this:

import PropTypes from 'prop-types';
export declare function MyComponent(props: MyComponent.Props): import("react/jsx-runtime").JSX.Element;
export declare namespace MyComponent {
    var propTypes: {
        children: PropTypes.Requireable<PropTypes.ReactNodeLike>;
    };
}
export declare namespace MyComponent {
    interface Props {
        children?: React.ReactNode;
    }
}

This actually also fails if another namespace (non-type-only) is declared with the same name (so it's not specific to prop-types):

export function MyComponent(props: MyComponent.Props) {
  const { children } = props;

  return (
    <div>{children}</div>
  );
}

export namespace MyComponent {
  export interface Props {
    children?: React.ReactNode;
  }
}

export namespace MyComponent {
  export var extraData: string;
}

Steps to reproduce the bug

TypeStrong/typedoc-repros#50

Environment

  • Typedoc version: 0.27.9
  • TypeScript version: 5.7.3
  • Node.js version: 22.13.1
  • OS: Windows 11
@Gerrit0
Copy link
Collaborator

Gerrit0 commented Feb 27, 2025

Declaration merging with namespaces is bad enough... mixing namespaces and expando properties hurts me.

Workaround:

export function MyComponent(props: MyComponent.Props) {
    return;
}

export namespace MyComponent {
    export interface Props {
        children?: {};
    }
    export const propTypes = { children: {} };
}

@Gerrit0 Gerrit0 added this to the v0.28.0 milestone Feb 27, 2025
Gerrit0 added a commit that referenced this issue Feb 27, 2025
@Gerrit0 Gerrit0 closed this as completed Feb 27, 2025
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

No branches or pull requests

2 participants