解决ts开发时导入图片报错:“Cannotfindxxxoritscorrespondingtypedeclaration”png';找不到模块“../../assets/images/foo.png”或其相应的类型声明。因为打字稿不识别非代码资源。我们需要主动声明这个模块方法一:来源:参考链接新建ts声明文件:images.d.tsdeclaremodule'*.svg'declaremodule'*.png'declaremodule'*。jpg'declaremodule'*.jpeg'declaremodule'*.gif'declaremodule'*.bmp'declaremodule'*.tiff'在工程编译时会自动读取.d.ts文件,所以我们不需要加载他们手动。放在tsconfig.json中include属性配置的文件夹下。方法2:如果你的项目是使用:npxcreate-react-appmy-app--templatetypescript#oryarncreatereact-appmy-app--templatetypescript然后你的src目录中会有一个react-app-env.d.ts文件。内容如下:///该文件使用三斜杠指令引入react-scripts的类型声明文件,在目标文件react-app.xml中声明各种图片。d.ts和其他类型的资源。文件内容://///////declarenamespaceNodeJS{interfaceProcessEnv{readonlyNODE_ENV:'发展'|'生产'|'测试';只读PUBLIC_URL:字符串;}}declaremodule'*.avif'{constsrc:string;exportdefaultsrc;}declaremodule'*.bmp'{constsrc:string;exportdefaultsrc;}declaremodule'*.gif'{constsrc:string;exportdefaultsrc;}declaremodule'*.jpg'{constsrc:string;exportdefaultsrc;}declaremodule'*.jpeg'{constsrc:string;exportdefaultsrc;}declaremodule'*.png'{constsrc:string;exportdefaultsrc;}declaremodule'*.webp'{constsrc:string;exportdefaultsrc;}declaremodule'*.svg'{import*asReactfrom'react';exportconstReactComponent:React.FunctionComponent&{title?:string}>;常量rc:字符串;exportdefaultsrc;}declaremodule'*.module.css'{constclasses:{readonly[key:string]:string};exportdefaultclasses;}declaremodule'*.module.scss'{constclasses:{readonly[key:string]:string};exportdefaultclasses;}declaremodule'*.module.sass'{constclasses:{readonly[key:string]:string};exportdefaultclasses;}只要tsconfig.json的include包含src目录,就可以正常导入