Doo*_*mel 5 typescript reactjs eslint
是否可以export * from './X'在 react/typescript 中使用绝对导入路径和 eslint?我想在 components 文件夹中有一个 index.ts 文件,它可以导出其他所有内容,但是当我尝试这样做时,eslint 给了我奇怪的错误。
我目前的结构是这样的:
src/components/Example1/Example1.tsx
import React from 'react';
export type Props = {};
export const Example1 = (props: Props) => <div></div>
Run Code Online (Sandbox Code Playgroud)
src/components/Example1/index.ts
export * from './Example1';
Run Code Online (Sandbox Code Playgroud)
src/components/index.ts
export * from './Example1';
Run Code Online (Sandbox Code Playgroud)
这两个index文件都给我错误:
0:0 error Parsing error: Cannot read property 'name' of undefined
此外,当我尝试components/index使用绝对路径从文件中导入任何内容时,import { Example1 } from 'components';出现错误:
1:43 error Parse errors in imported module 'components': Cannot read property 'name' of undefined (undefined:undefined) import/namespace
1:43 error Parse errors in imported module 'components': Cannot read property 'name' of undefined (undefined:undefined) import/named
Run Code Online (Sandbox Code Playgroud)
这是我的.eslintrc.json文件
{
"env": {
"browser": true,
"es6": true,
"node": true
},
"root": true,
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:import/warnings",
"plugin:import/errors",
"plugin:import/typescript",
"prettier/@typescript-eslint",
"prettier",
"prettier/react"
],
"plugins": [
"react",
"react-hooks",
"jsx-a11y",
"@typescript-eslint",
"prettier"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2018,
"sourceType": "module"
},
"settings": {
"react": {
"version": "detect"
},
"import/resolver": {
"node": {
"extensions": [".js", ".jsx", ".ts", ".tsx"],
"moduleDirectory": ["node_modules", "src/"]
}
}
},
"rules": {
"prettier/prettier": "error",
"import/newline-after-import": "error",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": [
"warn",
{ "args": "after-used", "argsIgnorePattern": "^_" }
]
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react",
"baseUrl": "./src"
},
"include": ["src"]
}
Run Code Online (Sandbox Code Playgroud)
我的当前package.json:
{
"name": "frontend",
"version": "0.0.0",
"license": "MIT",
"private": true,
"dependencies": {
"@apollo/react-hooks": "^3.1.3",
"@material-ui/core": "^4.9.5",
"@material-ui/icons": "^4.9.1",
"@material-ui/styles": "^4.9.0",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"@types/jest": "^24.0.0",
"@types/material-ui": "^0.21.7",
"@types/node": "^12.0.0",
"@types/react": "^16.9.0",
"@types/react-dom": "^16.9.0",
"@types/react-router-dom": "^5.1.3",
"@types/styled-components": "^5.0.1",
"@types/yup": "^0.26.32",
"apollo-boost": "^0.4.7",
"env-cmd": "^10.1.0",
"formik": "^2.1.4",
"graphql": "^14.6.0",
"i18next": "^19.3.3",
"i18next-browser-languagedetector": "^4.0.2",
"react": "^16.13.0",
"react-dom": "^16.13.0",
"react-i18next": "^11.3.3",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.0",
"styled-components": "^5.0.1",
"typescript": "~3.7.2",
"yup": "^0.28.3"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^2.23.0",
"@typescript-eslint/parser": "^2.23.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-prettier": "^3.1.2",
"prettier": "^1.19.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"build:staging": "env-cmd -f .env.staging yarn build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"lint": "tsc --noEmit && eslint . --ext .js,.tsx,.ts --max-warnings=0",
"lint:fix": "eslint . --ext .js,.tsx,.ts --fix",
"watch": "react-scripts start",
"deploy": "yarn build && firebase deploy",
"deploy:staging": "yarn build:staging && firebase use staging && firebase deploy"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Run Code Online (Sandbox Code Playgroud)
我还使用 lerna 和纱线工作区来轻松处理 MERN 应用程序。
删除 eslint 包/配置,node_modules 没有帮助我,我仍然有Cannot read property 'name' of undefined错误。这非常奇怪,因为在干净的 CRA 中一切正常。我花了最后 4 个小时试图解决这个问题,最后使用了一种解决方法,将类型导入根索引文件并将它们重新导出为新类型,但这绝对是一个痛苦的解决方案。我需要保持isolatedModules。也许有人有类似的问题?
问题是@typescript-eslint包,查看该问题以获取更多信息: https ://github.com/typescript-eslint/typescript-eslint/issues/1746
快速修复此问题的方法是将软件包降级到版本 2.22.0:
@typescript-eslint/eslint-plugin
@typescript-eslint/parser
@编辑这有助于解决 lint 错误,但我的 CRA 构建仍然崩溃,我想我将不得不等待包修复
| 归档时间: |
|
| 查看次数: |
4277 次 |
| 最近记录: |