strothj / react-docgen-typescript-loader

Webpack loader to generate docgen information from Typescript React components.
Other
360 stars 47 forks source link

Imported `enum` types are displayed with propType "any" #12

Open justinanastos opened 6 years ago

justinanastos commented 6 years ago

If a component uses a prop of an enum that is imported from another file, the propType will be shown as any.

This will correctly label the color property as propType Color:

// SomeComponent.tsx
import * as React from 'react';

export enum Color {
  PRIMARY = 'primary',
  SECONDARY = 'secondary',
  COMPLEMENTARY = 'complementary',
  TERTIARY = 'tertiary',
  ANCILLARY = 'ancillary',
}

interface Props {
  /** The color */
  color?: Color;
}

const SomeComponent: React.SFC<Props> = (props) => {
  return 'test';
};

export default SomeComponent;

This will incorrectly label the color property as propType any:

// Color.ts
export enum Color {
  PRIMARY = 'primary',
  SECONDARY = 'secondary',
  COMPLEMENTARY = 'complementary',
  TERTIARY = 'tertiary',
  ANCILLARY = 'ancillary',
}
// SomeComponent.tsx
import * as React from 'react';
import { Color } from './Color';

interface Props {
  /** The color */
  color?: Color;
}

const SomeComponent: React.SFC<Props> = (props) => {
  return 'test';
};

export default SomeComponent;
strothj commented 6 years ago

Thank you for the issue report! I will investigate this tomorrow (Sunday).

justinanastos commented 6 years ago

Thanks @strothj !

strothj commented 6 years ago

Hello,

I've been trying to reproduce this issue but haven't had success. Can you upload a minimal example repo? Also are you sure you have the latest version installed, there was an update that bumped the package react-docgen-typescript which handles the parsing.

strothj commented 6 years ago

I've added a branch with your example: https://github.com/strothj/react-docgen-typescript-loader/tree/bug/component-with-enum/packages/react-docgen-typescript-loader-example/src/components/ComponentUsingEnum

Can you check that I was understanding correctly?

justinanastos commented 6 years ago

@strothj Thanks for looking at this! Sorry for not responding for a week, I let time get away from me. I will try to make a reproduction repo to demonstrate what I'm seeing.

realalexhomer commented 6 years ago

Hi, I've been experiencing a somewhat similar problem and thought it might help to let you know. I am still somewhat new to typescript so hopefully this makes sense :)

When I use Pick to declare my Props for a component all of the types are "any". If I declare them explicitly the types go through correctly.

For example:

interface A {
    someString: string;
}

type B = Pick<A, 'someString'>;

If my component uses A as props, someString will be correctly defined as string in storybook. B will show up as any.

strothj commented 5 years ago

@justinanastos I'm closing this issue for house keeping purposes. Feel free to open a new issue.

@realalexhomer Your issue appears to have been fixed upstream. It works fine now from what I can tell.

sierracrum-riskalyze commented 4 years ago

Hello! I'm actually having the exact same issue as @justinanastos. The example in his original post is exactly what I'm seeing. If the type is an imported enum, then it displays as any instead of the enum in the props table.

sierracrum-riskalyze commented 4 years ago

@strothj any update on a fix for this?