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

React-Native with TypeScript breaks on typecheck #465

Closed
Ztarbox opened this issue Jul 7, 2020 · 7 comments · Fixed by #488
Closed

React-Native with TypeScript breaks on typecheck #465

Ztarbox opened this issue Jul 7, 2020 · 7 comments · Fixed by #488
Labels

Comments

@Ztarbox
Copy link
Contributor

Ztarbox commented Jul 7, 2020

🐛 Bug Report

If I use the flags --typescript together with --native the generated code contains type check errors.
The props argument is annotated as "React.SVGProps" this is ok for react, but not 'react-native-svg' which uses the prop type 'SvgProps'

To Reproduce

transform using

npx @svgr/cli --filename-case camel --typescript --native 

run tsc on the file

Expected behavior

On the options "typescript" and "native", the "props" should be of type "SvgProps" from 'react-native-svg'
I was able to "workaround" this by using a costum template:

function defaultTemplate(
  { template, types: t },
  opts,
  { imports, interfaces, componentName, props, jsx, exports },
) {
  props[0] = { 
    ...props[0], 
    typeAnnotation: t.typeAnnotation(t.genericTypeAnnotation(t.identifier('SvgProps'))) 
  }
  const rnsvgImport = imports.filter(i => i.source.value === 'react-native-svg');
  if (rnsvgImport && rnsvgImport.length > 0) {
    rnsvgImport[0].specifiers.push(
      t.importSpecifier(t.identifier('SvgProps'), t.identifier('SvgProps')));
  }
  [...]
  `
}
module.exports = defaultTemplate;
## System:
 - OS: Windows 10 10.0.19042
 - CPU: (8) x64 Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
 - Memory: 12.85 GB / 31.96 GB
## Binaries:
 - Node: 12.18.2 - C:\Program Files\nodejs\node.EXE
 - Yarn: 1.22.4 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
 - npm: 6.9.0 - C:\Program Files\nodejs\npm.CM
@open-collective-bot
Copy link

Hey @Ztarbox 👋,
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 ❤️.

@gregberge
Copy link
Owner

Hello @Ztarbox, it is a bug, do you want to contribute with a PR on it?

@Ztarbox
Copy link
Contributor Author

Ztarbox commented Jul 27, 2020

I'll try

@jgo80
Copy link

jgo80 commented Aug 26, 2020

I was able to "workaround" this by using a costum template:

function defaultTemplate(
  { template, types: t },
  opts,
  { imports, interfaces, componentName, props, jsx, exports },
) {
  props[0] = { 
    ...props[0], 
    typeAnnotation: t.typeAnnotation(t.genericTypeAnnotation(t.identifier('SvgProps'))) 
  }
  const rnsvgImport = imports.filter(i => i.source.value === 'react-native-svg');
  if (rnsvgImport && rnsvgImport.length > 0) {
    rnsvgImport[0].specifiers.push(
      t.importSpecifier(t.identifier('SvgProps'), t.identifier('SvgProps')));
  }
  [...]
  `
}
module.exports = defaultTemplate;

@Ztarbox thank you for the workaround with the custom template, works very well! 😃

@stale
Copy link

stale bot commented Oct 25, 2020

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.

@stale stale bot added the wontfix label Oct 25, 2020
@lucasmotta
Copy link

Still an issue and I don't believe it's stale.

@stale stale bot removed the wontfix label Oct 25, 2020
@Ztarbox
Copy link
Contributor Author

Ztarbox commented Oct 26, 2020

PR is waiting...

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.

4 participants