milesj / babel-plugin-typescript-to-proptypes

Generate React PropTypes from TypeScript interfaces or type aliases.
MIT License
367 stars 25 forks source link

TypeError: Cannot read property 'name' of null #34

Closed smeijer closed 4 years ago

smeijer commented 4 years ago

I'm trying to use this plugin, to get the proptypes visible in Storybook. But unfortunately, I cannot seem to start storybook with this plugin installed:

ERROR in ./src/context/withContext.tsx
Module build failed (from ./node_modules/babel-loader/lib/index.js):
TypeError: Cannot read property 'name' of null
    at FunctionDeclaration (/home/smeijer/dev/myproject/node_modules/babel-plugin-typescript-to-proptypes/lib/index.js:164:57)

I'm not sure what info you need to get a better understanding of the error, but this is the webpack config I use:

    config.module.rules.push({
      test: /\.(ts|tsx)$/,
      loader: require.resolve('babel-loader'),
      options: {
        presets: [[ 'react-app', { flow:false, typescript:true }]],
        plugins: [['babel-plugin-typescript-to-proptypes', { comments: true }]],
      },
    });

    config.resolve.extensions.push('.ts', '.tsx');

I'm using babel-loader: 8.0.6 in storybook: 5.3.17

milesj commented 4 years ago

@smeijer It sounds like you have a function without a name, can you confirm?

smeijer commented 4 years ago

Yes, that I can confirm. We have a bunch of components defined without name. The file I mentioned in the opening post, holds something like:

import React, { ReactElement, FC } from 'react';
import MyContext from './MyContext';

export default function(Component): FC {
  return function WrappedWithMyContext(props): ReactElement {
    return (
      <MyContext.Consumer>
        {context => <Component {...props} mapContext={context} />}
      </MyContext.Consumer>
    );
  };
}
milesj commented 4 years ago

Ok cool, should be an easy fix. Thanks for the quick response.

smeijer commented 4 years ago

No problem. Let me know if there is a potential fix that I can test.

milesj commented 4 years ago

@smeijer Can you try the latest patch version?

smeijer commented 4 years ago

Thanks, no more errors on this side :tada:

milesj commented 4 years ago

Sweet!