-
-
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 svgr with webpack and TypeScript #546
Comments
Hey @mutsys 👋, |
@mutsys where are you putting the declaration file? |
I typically have my ambient types defined in a fie named
It should be possible to export the definition from |
I don't think SVGR should provide typings affecting the global I am using it in projects where I load some svg with svgr and some differently (background-image, file-loader or whatever). // typings.d.ts
declare module 'src/assets/svgr/*.svg' {
import React from "react";
const SVG: React.VFC<React.SVGProps<SVGSVGElement>>;
export default SVG;
} Anyway, my point is that it is possible to opt-in for SVGR types (worth documenting probably?), but it would be more difficult to opt-out if SVGR was to force those types for all svg assets. Unless there are more elegant ways in TS to avoid "polluting" global namespaces, but this is a bit above my TS knowledges atm 🤷 |
@flo-sch yeah, good point. absolutely agree that no one should be coerced/forced into any particular configuration by surprise. then just how to make it easier for newcomers to opt-in to the best configuration for them from among the available options? |
Isn't the webpack typescript option supposed to kick out the definition so I don't need to include a custom.d.ts in every repo with an svg? use: [
{
loader: "@svgr/webpack",
options: {
typescript: true,
ext: "tsx",
}
},
... But I haven't had any success getting this to work with webpack in storybook. |
I am facing the same challenge trying to get this to play nice with webpack and storybook too! |
this is where i was running into trouble as well. i think storybook itself is stomping on something here. i can get things to work where my app is fine but storybook doesn't work or storybook works and my app doesn't. i have had trouble finding a config that works well for both.
|
#573 should fix it. |
Hello, for me I didn't have to use any import dynamic from 'next/dynamic';
import { FC, SVGAttributes } from 'react';
interface Props extends SVGAttributes<SVGElement> {
ext?: 'svg';
icon: string;
}
export const Icon: FC<Props> = ({ icon, ext = 'svg', ...props }) => {
const Icon = dynamic(() => import(`./${ext}/${icon}.svg`));
return <Icon {...props} />;
}; this is a component located at then you can use it as a simple components with all SVG for usage with React, which doesn't have the |
I think OP was mostly interested in this getting better awareness and documentation. HAs that been addressed? It took me quite a long time to fine this thread. Shouldn't this be prominently featured in the SVGR main docs? |
This isn't really a question or request for help, rather a suggestion that the docs be updated a bit to help others out so they don't spend quite as much time fumbling around as I did.
Setting up
svgr
as awebpack
loader is wonderfully easy and works right out of the box as described in the docs. However, if you are using TypeScript, you must an ambient type definition in order for the TypeScript compiler to understand what to do with svg files. The typical example looks like this:And this allows you to get the basics working:
However, if you want to add props to the
Logo
component, none of the expected props will be accepted. This is because the ambient type declaration doesn't adequately describe the component created by svgr. In order to have the component typed correctly, you need the following ambient type definition instead:Now, the TypeScript compiler understands that what svgr hands off to you via the import statement is a React component that generates an
<svg>
element with the full range of props available to you.One of those things that isn't immediately obvious and probably should be explicitly stated in the docs.
The text was updated successfully, but these errors were encountered: