React-Native with TypeScript breaks on typecheck
See original GitHub issue🐛 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<SVGSVGElement>” 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
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Using TypeScript with React Native
My favourite is TypeScript, but React Native supports Flow out of the box. ... This does not affect code emit, just typechecking. */...
Read more >reactjs - Setting React children typescript definition breaks parent ...
I am trying to work out why setting the children prop for React/JSX components break the type-checking of the parent component.
Read more >The starting point for learning TypeScript
Find TypeScript starter projects: from Angular to React or Node.js and CLIs.
Read more >TypeScript for React Developers - freeCodeCamp
The browser does not understand TS code. It must be transcompiled into JS. JS has a dynamic type mapping value and TS has...
Read more >Strict Mode - React
findDOMNode can also be used on class components but this was breaking abstraction levels by allowing a parent to demand that certain children...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Ztarbox thank you for the workaround with the custom template, works very well! 😃
PR is waiting…