Skip to content

Can't import svg -- needs declaration #160

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

Closed
ramiel opened this issue Jul 4, 2019 · 3 comments
Closed

Can't import svg -- needs declaration #160

ramiel opened this issue Jul 4, 2019 · 3 comments
Labels
kind: support Asking for support with something or a specific use case solution: out-of-scope This is out of scope for this project solution: tsc behavior This is tsc's behavior as well, so this is not a bug with this plugin

Comments

@ramiel
Copy link

ramiel commented Jul 4, 2019

What happens and why it is wrong

I'm trying to include an SVG inside my tsx file. Something like

import thumbUp from './svg/thumb-up.svg';

export const ThumbUp = (props: IconProps) => <BaseIcon {...props} svg={thumbUp} />

In order to have the svg file I'm using rollup-svgo plugin as you can see in the rollup config below.

I have the error

semantic error TS2307: Cannot find module './svg/thumb-up.svg'

Environment

Versions

  • typescript: 3.5.2
  • rollup: 1.16.4
  • rollup-plugin-typescript2: 0.21.2

rollup.config.js

import typescript from "rollup-plugin-typescript2";
import svgo from 'rollup-plugin-svgo'
import pkg from "./package.json";

export default [
  {
    input: "src/index.ts",
    external: [
      'reakit',
      ...Object.keys(pkg.peerDependencies || {})
    ],
    plugins: [
      svgo(),
      typescript({ }),
    ],
    output: [
      { file: pkg.main, format: "cjs" },
      { file: pkg.module, format: "es" },
    ]
  },
];

tsconfig.json

package.json

plugin output with verbosity 3

@ezolenko
Copy link
Owner

ezolenko commented Jul 4, 2019

That's typescript saying it doesn't have a typing information for that svg file. You can either add //@ts-ignore above the import to silence the error, or you can use declare module './svg/thumb-up.svg' { ... } to actually define the typings.

@ramiel
Copy link
Author

ramiel commented Jul 5, 2019

Thank you. My bad, I thought it was a plugin issue. Fixed, great!

@ramiel ramiel closed this as completed Jul 5, 2019
@agilgur5 agilgur5 added solution: tsc behavior This is tsc's behavior as well, so this is not a bug with this plugin kind: support Asking for support with something or a specific use case solution: out-of-scope This is out of scope for this project labels May 8, 2022
@agilgur5 agilgur5 changed the title Include svg Can't import svg May 8, 2022
@agilgur5 agilgur5 changed the title Can't import svg Can't import svg -- needs declaration May 8, 2022
@agilgur5
Copy link
Collaborator

agilgur5 commented May 8, 2022

See also this StackOverflow question for more ways of creating declarations for SVGs depending on your stack etc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: support Asking for support with something or a specific use case solution: out-of-scope This is out of scope for this project solution: tsc behavior This is tsc's behavior as well, so this is not a bug with this plugin
Projects
None yet
Development

No branches or pull requests

3 participants