iTranslated by AI
Next.js + TypeScript: Imported SVG type becomes 'any'
Prerequisite: Importing SVGs with TypeScript + Next.js + svgr
In the Next.js application I am involved in, I use svgr to allow importing SVGs in the same way as other React components.
While it's slightly off-topic from the main point of this article, here is how to introduce svgr into Next.js.
↓ Installation
$ npm install --save-dev @svgr/webpack
↓ Set @svgr/webpack as a Webpack loader. For detailed settings, check the svgr documentation.
module.exports = {
webpack: (config) => {
config.module.rules.push({
test: /\.svg$/,
use: ['@svgr/webpack'],
});
return config;
},
}
↓ Declare the type for *svg so it can be handled like other components.
declare module '*.svg' {
const content: React.FC<React.SVGProps<SVGElement>>;
export default content;
}
↓ Now you can import SVGs.
import Icon from "./assets/icon.svg"
export const Foo = () => {
return <div><Icon widht={16} height={16} /></div>
}
Main Issue: Imported SVGs result in the any type
This is based on Next.js v11.1.0. When importing SVG files using the method introduced above, they are somehow treated as the any type.
import Icon from "./assets/icon.svg"
// 👆 The type of <Icon /> becomes any
This means the declare module '*.svg' { ... } that I wrote is not being reflected.
Tracking down why it becomes any, it seems that node_modules/next/image-types/global.d.ts in Next.js also has a declaration for *.svg, and that one is taking precedence.
// node_modules/next/image-types/global.d.ts
declare module '*.svg' {
/**
* Use `any` to avoid conflicts with
* `@svgr/webpack` plugin or
* `babel-plugin-inline-react-svg` plugin.
*/
const content: any
export default content
}
Disabling Next.js type definitions for image imports
After some investigation, I found the following description in the official documentation:
Disable Static Imports
The default behavior allows you to import static files such as import icon from './icon.png and then pass that to the src property.
In some cases, you may wish to disable this feature if it conflicts with other plugins that expect the import to behave differently.
You can disable static image imports with the following configuration below.
next.config.jsmodule.exports = { images: { disableStaticImages: true, }, }
Next.js provides type definitions for image files like jpeg, png, and svg in next/image-types/global.d.ts by default to enable importing them.
In Next.js v11.1.0, the svg import is set to any, and this was taking priority over my own declaration.
Since I don't import image files other than SVGs in my project, I decided to disable the setting as described in the documentation.
module.exports = {
images: {
disableStaticImages: true,
},
}
With this, the declare module '*.svg' { ... } I prepared now works correctly.
Discussion
next.config.jsに対して下記のように型定義が効くように起票した場合。imagesで添付画像のようなエラーが出てしまいました。🤔
何か考えられる原因等もし知っていたらお知恵をお借りできたらと思っております。
現状は
// @ts-checkを外して対応エラーが出ないようにしてあります。▽ vs codeのエラー

型定義情報を調べたところ
ImageConfigには、disableStaticImages がちゃんとありました...自分のプロジェクトでは
.jsファイルを型エラーのチェック対象としていないため、ちょっと分からないですね…。ただ
ImageConfigの型定義をimageSizedなどプロパティがoptionalになっていないため、images.disableStaticImagesだけ指定すれば確かに型エラーとなるはずです。Next.js側でそのうち修正される気がしますね。
Next.jsにIssueとPRを作っておきました
確かに、必須項目部分がありますね。
見落としていました👀
issueとPRの作成ありがとうございます!
こちらマージされたので今後のリリースで修正されると思います!