[TypeScript] 错误集锦
2019-12-11 本文已影响0人
小黄人get徐先生
默认导入报错

错误信息:can only be default-imported using the 'esModuleInterop' flag"
错误原因:需要在 tsconfig.json
文件中添加 "esModuleInterop": true,
;
参考文档:https://www.typescriptlang.org/v2/docs/handbook/release-notes/typescript-2-7.html
已经安装了 react-router-dom,但是还是显示找不到 react-router-dom 模块
错误信息: TS2307: Cannot find module 'react-router-dom'.
错误原因:需要同时安装 @types/react-router-dom
// 确保两个都有安装
> yarn add react-router-dom -d
> yarn add @type/react-router-dom -D
tsx 文件不能直接 import Comp from './Comp'
导入,必须写全路径导入(即:import Comp from './Comp.tsx'
)
错误信息:ERROR in ./src/index.tsx
Module not found: Error: Can't resolve './components/BlogList' in '/Users/xuqiang/easyops/node-blog/koa2-front/src'
@ ./src/index.tsx 4:0-49 7:91-99
错误原因: 当我们之前以 import Comp from './Comp'
导入模块的时候,实际上我们是不能确定导入的文件类型是什么,可能是 ts
,可能是 tsx
,也可能是 json
。所以我们需要在 webpack
里面配置 resolve.extensions
来帮助我们解析。具体配置如下图:
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx']
},
参数文档:resolve.extensions