-
-
Notifications
You must be signed in to change notification settings - Fork 425
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
Using with typescript #473
Comments
Hey @farzinmirzaie 👋, |
So I had the exact same problem but with nextjs. It seems that when I added // custom.d.ts
declare module '*.svg' {
import * as React from 'react';
const ReactComponent: React.FunctionComponent<React.SVGProps<SVGSVGElement>>;
export default ReactComponent;
} // next.config.js (where I inject webpack support to next webpack config)
module.exports = {
webpack(config) {
config.module.rules.push({
test: /\.svg$/,
use: [
{
loader: '@svgr/webpack',
},
],
});
return config;
},
}; and now type any prop for svg component, the only downside is it doesnt propose any props in IDE, but maybe its my fault, I'm writing in typescript for about a week :) |
@michalpokojski Thank you but still not working for me! I see you are no passing |
@farzinmirzaie sure, i've deleted {
"compilerOptions": {
"target": "es6",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"baseUrl": "."
},
"exclude": ["node_modules", ".next", "out"],
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "**/*.js", "custom.d.ts"]
} I even tried to include |
@michalpokojski Thank you for sharing, But i couldn't make it work! |
FYI if you're configuring your SVGR to work in tandem with the file loader and by default, when importing an SVG file you'll get the file's URL by default you have to import the declare module "*.svg" {
import * as React from "react";
const ReactComponent: React.FunctionComponent<React.SVGProps<SVGSVGElement>>;
export { ReactComponent };
export default string;
} Thought it could be useful to folks. |
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. |
Hi, I was just having this problem, and this is how I got it to work: ✅ .svgrrc.js
typescript-template.js
CLI Command:
Hope it serves you well! |
@bernardoamim thank you so much for providing your config! It works beautifully for 90% of the SVGs that I have tried it on, but there are some edge cases where it doesn't not copy over the correct paths. Try running your command on
And the output is broken like
Whereas it should look like this instead:
|
@famouspotatoes Hi! I'm sorry I didn't answer sooner. I haven't checked out these issues in a while. I don't think that is a problem with my solution. Actually, I don't see that as a problem at all. I applied your code to see if there was any unexpected behaviour, but the result was the same. Here are both SVGs place aside from each other: As you can see, there is no difference. I just configured my icon as follows Original SVG:
**Converted SVG (This.tsx): ** `import React from 'react'; const SvgThis: React.FC<React.SVGProps> = ( export default SvgThis; Configured TSX as an imported component:
|
Fixed in #573 |
🐛 Bug Report
I'm using
@svgr/webpack
inside a TypeScript react project and everything works fine without doing anything fancy! But if i pass props to svg component then i face exceptions. I found many same issues here but all closed without any answer so i'm opening a new one.Note: I'm inside a monorepo and everything works fine on ReactNative using react-native-svg-transformer with TypeScript.
To Reproduce
<Icon />
This works fine<Icon width={100} height={100} fill='red' />
This cause exceptionSo i tried to pass
typescript: true
option in my.svgrrc.js
file to generates .tsx files with TypeScript typings but i get following exception and even using<Icon />
without passing props wont work anymore.I tried to use custom template to generate .tsx file but still face same problem.
Im returning
null
instead of${jsx}
just to make error message more readableExpected behavior
The
typescript: true
option should fix the issue.Paste the results here:
The text was updated successfully, but these errors were encountered: