You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript ts(2307)错误:导入图片找不到模块或类型声明求助

解决TypeScript导入图片时的ts(2307)模块找不到警告

问题原因

TypeScript本身无法识别图片、字体这类非代码资源模块,因此会抛出“找不到模块或对应的类型声明”的警告——尽管代码能正常编译运行。

解决步骤

1. 创建图片类型声明文件

在项目的src目录下新建一个名为images.d.ts的文件(后缀必须为.d.ts,文件名可自定义),写入以下内容:

declare module '*.jpg' {
  const value: string;
  export default value;
}

declare module '*.jpeg' {
  const value: string;
  export default value;
}

// 若用到其他图片格式,比如png,可添加对应声明
declare module '*.png' {
  const value: string;
  export default value;
}

2. 修正tsconfig.json的拼写错误

你的tsconfig.json存在关键拼写错误:compilerOption应为复数形式compilerOptions,这个错误会导致部分配置无法生效。

3. 确保TypeScript识别声明文件

在tsconfig.json中添加include配置,让TypeScript扫描src目录下的所有文件(包括.d.ts声明文件)。修正后的完整配置如下:

{
  "compilerOptions" : {
    "target" : "esnext",
    "module" : "commonjs",
    "lib" : ["es2017"],
    "allowJs" : true,
    "jsx" : "react-native",
    "noEmit" : true,
    "strict" : true,
    "moduleResolution" : "node",
    "allowSyntheticDefaultImports" : true,
    "esModuleInterop" : true,
    "skipLibCheck" : false,
    "resolveJsonModule" : true
  },
  "include": ["src/**/*"],
  "exclude" : ["node_modules", "babel.config.js", "metro.config.js", "jest.config.js"]
}

4. 重启VS Code的TypeScript服务

完成以上操作后,按下Ctrl+Shift+P(Windows/Linux)或Cmd+Shift+P(Mac),输入TypeScript: Restart TS Server,重启服务后警告即可消失。

内容的提问来源于stack exchange,提问作者Jay Yu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 05:45:33