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

Prevent stripping of return type #556

Closed
haswalt opened this issue May 9, 2021 · 3 comments · Fixed by #619
Closed

Prevent stripping of return type #556

haswalt opened this issue May 9, 2021 · 3 comments · Fixed by #619
Labels

Comments

@haswalt
Copy link

haswalt commented May 9, 2021

I've been working on a component library that builds icon components from svgs. Building the components themselves are realy simple thanks for SVGR, but i'm having some issues with typescript support.

I'm using a custom template to add my own imports and adjustments to the output:

{
      plugins: ['@svgr/plugin-jsx', '@svgr/plugin-prettier'],
      ext: 'tsx',
      typescript: true,
      native: true,
      expandProps: false,
      dimensions: false,
      prettierConfig: {
        parser: 'typescript',
      },
      replaceAttrValues: {
        white: '{fill}',
      },
      svgProps: {
        width: '{width}',
        height: '{height}',
      },
      template: (
        { template },
        opts,
        { imports, interfaces, componentName, props, jsx, exports }
      ) => {
        const plugins = ['jsx'];

        if (opts.typescript) {
          plugins.push('typescript');
        }

        const typeScriptTpl = template.smart({
          plugins,
          preserveComments: true,
          retainLines: true,
          compact: false,
        });

        return typeScriptTpl.ast`
          ${`/**
              * AUTO-GENERATED - DO NOT EDIT!
              */\n`}
          ${imports}
          import {colours} from '@haswalt/tokens';
          ${'\n'}
          import {iconSizes} from '../config';
          ${'\n'}
          ${interfaces}
          ${'\n'}
          type Props = {
            colour?: string;
            size?: 'l' | 'm' | 's'| 'xs'|'xxs';
          }
          ${'\n'}
          export const ${componentName}: React.FC<Props> = ({colour = "foreground", size = "m"}) => {
            const {width, height} = iconSizes[size];
            const fill = colours[colour];
            ${'\n'}
            return (${jsx});
          }
          ${'\n'}
          ${exports}
        `;
      },
    },

Mostly this works fine, except the return type us being stripped from the output code: React.FC<Props> and I can't get it to keep it in place. This is required to make typechecking work as the props aren't correctly typed otherwise.

@open-collective-bot
Copy link

Hey @haswalt 👋,
Thank you for opening an issue. We'll get back to you as soon as we can.
Please, consider supporting us on Open Collective. We give a special attention to issues opened by backers.
If you use SVGR at work, you can also ask your company to sponsor us ❤️.

@stale
Copy link

stale bot commented Jul 9, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@gregberge
Copy link
Owner

Hello @haswalt,

It is actually a babel-template bug. As a workaround I made a PR #619, that gives you a way to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants