strothj / react-docgen-typescript-loader

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

import React from 'react' doesn't work #62

Open TheKnarf opened 5 years ago

TheKnarf commented 5 years ago

Changing the following:

-import * as React from 'react';
+import React from 'react';

Seems to break react-docgen-typescript-loader's ability to generate docgen for components in the file.

wollardj commented 5 years ago

Example of this is

interface IProps {
  onChange: (event: React.ChangeEvent<HTMLInputElement>);
}

... this will render as (event: any) => void) with import React from 'react', but renders properly with import * as React from 'react'

TheKnarf commented 5 years ago

Normally Typescript disallows import React from 'react', however you can activate it in tsconfig.json by setting the following:

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
  }
}

I assume that this project simply haven't set that option and it therefor fails for every project who have set it too true.

edit It seems to be set for this project, so then I'm unsure what it is.

dizel3d commented 5 years ago

Specify path to your tsconfig.json in tsconfigPath.

{
  loader: require.resolve('react-docgen-typescript-loader'),
  options: {
    tsconfigPath: path.resolve(__dirname, '../tsconfig.json')
  }
},
wollardj commented 5 years ago

@dizel3d - Thanks! That did the trick! I thought I'd specified that, but it turns out I'd only specified it for ts-loader and forgot to add it to react-docgen-typescript-loader.