我一直在这个问题上停留太久了,无论我尝试什么,我都无法解决这个问题。
我有一个名为one.tsx
的简单页面。看起来像这样:
我收到以下棉绒错误:Missing return type on function.eslint(@typescript-eslint/explicit-function-return-type)
这是我的tsconfig.json文件:
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": false,
"checkJs": false,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"noUnusedLocals": true /* Report errors on unused locals. */,
"noUnusedParameters": true /* Report errors on unused parameters. */,
"noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
"noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */
},
"exclude": ["node_modules", ".next"],
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"]
}
这是我的.eslintrc.json文件:
{
"env": {
"browser": true,
"es6": true
},
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended",
"plugin:jest/recommended",
"prettier",
"airbnb"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": ["react", "@typescript-eslint", "prettier", "jest"],
"settings": {
"import/resolver": {
"node": {
"extensions": [".js", ".jsx", ".ts", ".tsx"]
}
}
},
"rules": {
"react/jsx-one-expression-per-line": "off",
"jsx-a11y/anchor-is-valid": "off", // <a> tags sometimes have their href attr set in <Link> from NextJS.
// Turn off this rule for ts and tsx files:
"react/jsx-filename-extension": [
1,
{ "extensions": [".js", ".jsx", ".ts", ".tsx"] }
],
"import/extensions": [
"error",
"ignorePackages",
{
// This is so that we can import non-js files.
"js": "never",
"jsx": "never",
"ts": "never",
"tsx": "never"
}
],
// Next.JS Doesn't require the import React statement at the top of its pages
"react/react-in-jsx-scope": "off"
}
}
这是我的package.json:
{
"name": "my_app",
"version": "1.0.0",
"private": true,
"scripts": {
"precommit": "npm run build && lint-staged",
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "eslint './**/*.{js,jsx,ts,tsx}' --max-warnings 0",
"test": "jest --passWithNoTests",
"test:watch": "jest --watchAll"
},
"lint-staged": {
"*.json": [
"prettier --write"
],
"./**/*.{js,jsx,ts,tsx,css,scss}": [
"prettier --write",
"npm run lint",
"npm run test"
]
},
"dependencies": {
"@testing-library/jest-dom": "^5.1.1",
"@testing-library/react": "^9.4.0",
"@types/jest": "^25.1.2",
"isomorphic-unfetch": "^3.0.0",
"jest": "^25.1.0",
"next": "9.2.1",
"react": "16.12.0",
"react-dom": "16.12.0",
"ts-jest": "^25.2.0"
},
"devDependencies": {
"@types/node": "^13.7.0",
"@types/react": "^16.9.19",
"@types/react-dom": "^16.9.5",
"@typescript-eslint/eslint-plugin": "^2.19.0",
"@typescript-eslint/parser": "^2.19.0",
"babel-eslint": "^10.0.3",
"eslint": "^6.8.0",
"eslint-config-airbnb": "^18.0.1",
"eslint-config-prettier": "^6.10.0",
"eslint-loader": "^3.0.3",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-jest": "^23.7.0",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-react": "^7.18.3",
"eslint-plugin-react-hooks": "^1.7.0",
"lint-staged": "^10.0.7",
"prettier": "^1.19.1",
"typescript": "^3.7.5"
}
}
这是我在控制台中看到的错误:
frontend | [ event ] build page: /one
frontend | [ warn ] ./pages/one.tsx
frontend | /usr/src/app/packages/frontend/pages/one.tsx
frontend | 22:23 warning Missing return type on function @typescript-eslint/explicit-function-return-type
frontend |
frontend | ✖ 1 problem (0 errors, 1 warning)
frontend | [ info ] ready on http://localhost:3000
frontend | [ warn ] ./pages/one.tsx
frontend | /usr/src/app/packages/frontend/pages/one.tsx
frontend | 22:23 warning Missing return type on function @typescript-eslint/explicit-function-return-type
frontend |
frontend | ✖ 1 problem (0 errors, 1 warning)
frontend | [ info ] ready on http://localhost:3000
我可以手动给它一个返回类型以清除错误,但是NextPage应该自动包括这些类型。这是我手动为其提供返回类型的代码段:TestServer.getInitialProps = async (): Promise<Props> => {
我已经确认存在next-env.d.ts文件,并且我的VSCode可以代码跳转到位于node_modules /下的NextPage类型定义文件。但是,eslint-typescript似乎并不承认这一点。我在做什么错了?
谢谢