V. Kuo
V. Kuo

Reputation: 78

NextJS & Typescript getInitialProps linting error (return type)

I've been stuck with this issue for way too long now and no matter what I try I can't get this to work.

I have a simple page called one.tsx. It looks like this:

enter image description here

I'm getting the following linting error: Missing return type on function.eslint(@typescript-eslint/explicit-function-return-type)

Here is my tsconfig.json file:

{
  "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"]
}

And here is my .eslintrc.json file:

{
  "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"
  }
}

Here is my 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"
  }
}

Here is the error I get in the console:

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

I can manually give it a return type to clear the error, but NextPage should include the types automatically. Here is the snippet of me manually giving it the return type: TestServer.getInitialProps = async (): Promise<Props> => {

I've confirmed that next-env.d.ts file exists, and my VSCode can code jump to the NextPage type definition file located under node_modules/. However, eslint-typescript doesn't seem to acknowledge it. What am I doing wrong?

Thanks

Upvotes: 4

Views: 5193

Answers (2)

Ali Eslamifard
Ali Eslamifard

Reputation: 575

You need to return a Promise with your backendResponse type.

One.getInitialProps = async (): Promise<Props> => ({ backendResponse: 'testing' });

Upvotes: 2

Afsanefda
Afsanefda

Reputation: 3339

Based on this it's a rule and I think you have to obey it or disable it! and I think your question is a duplicate of this.

Upvotes: 0

Related Questions