Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typescript cannot find module when import svg file

It's web application created with Typescript, React and webpack. I want to use svg file through webpack. However, I got

    TS2307: Cannot find module '~/images/slide.svg'. 

Typescript files are put on /frontend/src/, and typescript files are build successfully.

Then, I tried some solutions on website. But I could not solve this problem. What I tried is described briefly as follows.

I added a file where defined types in 'typeRoots'. tsconfig.json

{
  "compilerOptions": {
    "outDir": "./app/assets/javascripts/bundles",
    "sourceMap": true,
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "noImplicitAny": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "moduleResolution": "node",
    "module": "es2015",
    "target": "es6",
    "jsx": "react",
    "removeComments": true,
    "baseUrl": ".",
    "paths": {
      "~/*": ["./frontend/src/*"]
    },
    "typeRoots": ["types", "node_modules/@types"]
  },
  "awesomeTypescriptLoaderOptions": {
    "useCache": true,
    "cacheDirectory": "tmp/build_caches/awesome-typescript-loader"
  },
  "include": [
    "./frontend/src/*"
  ],
  "exclude": [
    "node_modules",
    "bundles",
    "**/*.js"
  ]
}

types/images.d.ts

declare module '*.svg' {
  const content: any;
  export default content;
}

I use 'file-loader' on webpack to build svg fild.

Please help me!!

like image 602
hideto Avatar asked Nov 06 '19 08:11

hideto


People also ask

How do I import an SVG file into TypeScript?

Importing SVG files as React Components in TypeScript svg files as React Components in a TypeScript project. import React from "react"; import { ReactComponent as SVGIcon } from "~/icons/icon.

What type is SVG TypeScript?

SVG elements is SVGElement which is documented on MDN. Since TypeScript 3. x, TypeScript's "standard library" of typings for the HTML DOM includes the SVG DOM interfaces in lib/lib.

Is SVG an image?

A svg (Scalable Vector Graphics) file is a vector image file format. A vector image uses geometric forms such as points, lines, curves and shapes (polygons) to represent different parts of the image as discrete objects.


2 Answers

I think it is because of your include section in the Typescript config. Try this:

"include": [
  "./frontend/src/*/**"
],

Otherwise you are only including typing files which are in src folder top level. And your svg typing file is in a sublevel so Typescript will not include it.

like image 91
J. Meier Avatar answered Nov 15 '22 02:11

J. Meier


If you are using Nextjs, you can create a new file additional.d.ts (same level as next-env.d.ts) and declare modules for image files. You should then include this file in your tsconfig.json.

additional.d.ts:

declare module "*.png";
declare module "*.jpg";
declare module "*.jpeg";
declare module "*.svg";
declare module "*.gif";

tsconfig.json:

{
  ...
  "include": ["next-env.d.ts", "additional.d.ts", "**/*.ts", "**/*.tsx"],
}
like image 39
Killian Huyghe Avatar answered Nov 15 '22 04:11

Killian Huyghe