reactjs - Typescript typings.d.ts 因解析错误而失败
问题描述
当我运行 lint 我的打字稿代码时,它会引发解析错误:
2:3 error Parsing error: Only declares and type imports are allowed inside declare module
1 | declare module "*.json" {
> 2 | var value: any;
| ^
3 | export default value;
4 | }
我是 TypeScript 的新手,真的不知道如何解决这个问题?
我的 tsconfig 如下:
{
"compilerOptions": {
"isolatedModules": true, // Warn you if you write certain code that can’t be correctly interpreted by a single-file transpilation process.
"outDir": "./dist/",
"module": "commonjs",
"target": "ES5",
"jsx": "react",
"lib": ["ESNEXT", "DOM", "DOM.Iterable", "ES5"],
"allowJs": true, // Allow JavaScript files to be imported inside your project, instead of just .ts and .tsx
"checkJs": true, // When checkJs is enabled then errors are reported in JavaScript files. This is the equivalent of including // @ts-check at the top of all JavaScript files which are included in your project.
"allowSyntheticDefaultImports": true, // Allow default imports from modules with no default export
"skipLibCheck": true,
"types": ["node", "webpack-env", "@wdio/sync", "mocha", "expect-webdriverio"],
"strict": false // Enables all strict type checking options (This is too restrictive for the current code base)
},
"include": [
"src/**/**.ts",
"test/**/**.ts"
]
}
我应该排除它吗?这是一个带有 Webpack 的 React 代码库。
解决方案
error Parsing error: Only declares and type imports are allowed inside declare module
答案就在错误本身。
是declare module
声明类型,但是您在代码中所做的是定义一个变量(值而不是类型),因此它是不允许的,也没有任何意义。只需删除该行,错误就会消失。
如果你能说出你真正想要达到的目标,我们可以帮助你。
推荐阅读
- html - jQuery 代码不适用于动态附加的元素
- php - Laravel 在本地磁盘中查看图片
- magento-2.3 - Magento 2.3.6 p1 登录问题
- android - 如何使用给定的 XML 代码移动圆圈下方的文本?
- docker - Docker Compose 和 Volumes 问题
- python - 如何将输入与 Python 列表中的字符串进行比较?
- postgresql - PostgreSQL:如何统一版本?
- javascript - Material-UI 仅包含某些组件
- reactjs - Prettier 和 ESLint + Airbnb 报价问题
- javascript - 有没有更快的方法来显示数组的每个索引