Code Monkey home page Code Monkey logo

Comments (12)

masihjahangiri avatar masihjahangiri commented on May 22, 2024 44

I use this type definition for it:

declare module '*.svg' {
  const ReactComponent: React.FC<React.SVGProps<SVGSVGElement>>;
  const content: string;

  export { ReactComponent };
  export default content;
}

from svgr.

atdrago avatar atdrago commented on May 22, 2024 12

I needed to use the above type, so I created an SvgrComponent interface for it:

interface SvgrComponent extends React.StatelessComponent<React.SVGAttributes<SVGElement>> {}

declare module '*.svg' {
    const value: SvgrComponent;
    export default value;
}

It's then global, and can be used elsewhere for things like type assertions:

const MySvg = mySvgProp as SvgrComponent;
<MySvg />

from svgr.

JorritPosthuma avatar JorritPosthuma commented on May 22, 2024 6

I changed the extension to tsx and use the following template, which works for me.
Note: This template ignores the expandProps and ref options (I didn't need them). It also doesn't have the webpack specific code from the original template (see https://github.com/smooth-code/svgr/blob/master/src/transforms/wrapIntoComponent.js)

Running svgr

svgr --ext tsx --template svgrTypescript.js

svgrTypescript.js

module.exports = () => {
    return (code, state) => {
        return `import * as React from 'react'\n\n` +
               `const ${state.componentName} = (props: any) => ${code}\n\n` +
               `export default ${state.componentName}`;
    }
};

I'll try to find time to make a more complete template, and create a PR. But as a disclaimer, I'm by no means a TS expert (day 2 :P)

from svgr.

a7madgamal avatar a7madgamal commented on May 22, 2024 4

interface SvgrComponent extends React.StatelessComponent<React.SVGAttributes> {}

declare module '*.svg' {
const value: SvgrComponent;
export default value;
}

thanks! I made a slightly improved version to avoid using as

interface SvgrComponent
  extends React.StatelessComponent<React.SVGAttributes<SVGElement>> {}

declare module "*.svg" {
  const ReactComponent: SvgrComponent;

  export { ReactComponent };
}

from svgr.

atdrago avatar atdrago commented on May 22, 2024 1

@developer-lindner This seems more like a JSX issue than a TypeScript issue. In other words, it looks like Webpack doesn't understand it needs to parse this as JSX. If you're already working with .tsx files, it's possible you just need to change the extension on your file to .tsx, but it's hard to say. A little more information about how you're using SVGR would be helpful. Are you using the CLI to generate these components?

from svgr.

gregberge avatar gregberge commented on May 22, 2024 1

@marquesdev nice! I have just released v2!

from svgr.

gregberge avatar gregberge commented on May 22, 2024

Thanks for reporting it! I do not use TypeScript myself, this is why this package is not TypeScript friendly. If a TypeScript expert could give a look at this issue and maybe propose a mention about TypeScript in the README it would be very nice!

from svgr.

developer-lindner avatar developer-lindner commented on May 22, 2024

A TypeScript example would be nice:
bildschirmfoto 2018-01-24 um 17 10 44

from svgr.

marques-kevin avatar marques-kevin commented on May 22, 2024

Hello everyone :)

I'm working on implementing this feature, I think it will be ready in two days :)

from svgr.

kelly-tock avatar kelly-tock commented on May 22, 2024

@neoziro @marquesdev is there a TS specific feature I can help with? we are wanting to turn on noImplicitAny and can't. the template above accomplishes this, but I would think that if there were a ts option the : any would suffice.

from svgr.

marques-kevin avatar marques-kevin commented on May 22, 2024

@kelly-tock Hello !

I'm not a user of ts but i'm working on implementig this feature.

You can see it in this PR #130.

The CI does'nt allow the merge because the code covering is not good enough.

But if you want, or have time, you can fork this PR and complete the code covering.

I don't know when I will have the time to do it.

from svgr.

gregberge avatar gregberge commented on May 22, 2024

See #89.

from svgr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.