ESLint 规则不显示打字稿类型定义的错误

问题描述 投票:0回答:1

我有一个工作流程,通过运行

yarn lint
命令来检查 ESLint 错误。 工作流程如下:

name: Build and deploy
on:
  push:
    branches:
      - '**'
env:
  app_name: support_case_ui
  bucket_name_dev: support-ui-static-files-dev
  bucket_name_prod: support-ui-static-files-prod
  IS_FEATURE: ${{ github.ref != 'refs/heads/release' && github.ref != 'refs/heads/main'}}
  IS_DEV: ${{ github.ref == 'refs/heads/main' }}
  NPM_TOKEN: ${{ secrets.READER_TOKEN }}
jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20.x
      - uses: actions/cache@v4
        id: yarn-cache
        with:
          path: .yarn/cache
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-
      - name: Set environment variables for frontend build
        # Use environment variables from feature just for the build to go green. Using actual env variables in deploy step
        run: cat env/.env.dev >> $GITHUB_ENV 
      - name: Install yarn dependencies
        run: yarn --immutable
      - name: Check lint
        run: yarn lint
      - name: Run tests and build
        run: yarn test && yarn build

  deploy-to-feature:
    name: Deploy to feature
    runs-on: ubuntu-latest
    needs: build
    if: github.ref != 'refs/heads/main'
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20.x
      - uses: actions/cache@v4
        id: yarn-cache
        with:
          path: .yarn/cache
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-
      - name: Install yarn dependencies
        run: yarn --immutable
      - name: Set environment variables for build
        run: cat env/.env.dev >> $GITHUB_ENV
      - name: Build app
        run: yarn build
      - name: Login GCP dev
        uses: google-github-actions/auth@v1
        with:
          credentials_json: ${{ secrets.GCP_SERVICEUSER_KEY_DEV }}
      - name: Set up Cloud SDK
        uses: google-github-actions/setup-gcloud@v1
      - name: Upload static files to feature GCP bucket
        run: gsutil -m rsync -R -d dist gs://${{env.bucket_name_dev}}/${{env.app_name}}/feature/static

  deploy-to-main:
    name: Deploy to main
    runs-on: ubuntu-latest
    needs: build
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20.x
      - uses: actions/cache@v4
        id: yarn-cache
        with:
          path: .yarn/cache
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-
      - name: Install yarn dependencies
        run: yarn --immutable
      - name: Set environment variables for build
        run: cat env/.env.main >> $GITHUB_ENV
      - name: Build app
        run: yarn build
      - name: Login GCP dev
        uses: google-github-actions/auth@v1
        with:
          credentials_json: ${{ secrets.GCP_SERVICEUSER_KEY_DEV }}
      - name: Set up Cloud SDK
        uses: google-github-actions/setup-gcloud@v1
      - name: Upload static files to dev GCP bucket
        run: gsutil -m rsync -R -d dist gs://${{env.bucket_name_dev}}/${{env.app_name}}/main/static
  deploy-to-prod:
    name: Deploy to prod
    runs-on: ubuntu-latest
    needs: build
    if: github.ref == 'refs/heads/main'

    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20.x
      - uses: actions/cache@v4
        id: yarn-cache
        with:
          path: .yarn/cache
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-
      - name: Install yarn dependencies
        run: yarn --immutable
      - name: Set environment variables for build
        run: cat env/.env.prod >> $GITHUB_ENV
      - name: Build app
        run: yarn build
      - name: Login GCP dev
        uses: google-github-actions/auth@v1
        with:
          credentials_json: ${{ secrets.GCP_SERVICEUSER_KEY_PROD }}
      - name: Set up Cloud SDK
        uses: google-github-actions/setup-gcloud@v1
      - name: Upload static files to prod GCP bucket
        run: gsutil -m rsync -R -d dist gs://${{env.bucket_name_prod}}/${{env.app_name}}/static

对于这行代码:

type STEPSTYPE = { [key in Stepper]: number } | { [key in CaseStepper]: number };

运行工作流作业时出现错误:

Error:   52:86  error  'key' is defined but never used  @typescript-eslint/no-unused-vars

但是如果我在计算机上本地运行

yarn lint
,我不会收到任何错误。它运行没有任何错误:

yarn run v1.22.19
$ eslint ./src/**/*.{js,ts,tsx} package.json --quiet
✨  Done in 3.76s.

除了

no-unused-vars
之外,是否有任何 lint 规则可以避免打字稿类型定义的错误?我仍然希望收到未使用的变量的错误,但在我的情况下,它不是一个变量,只是抛出错误的类型。

这是我当前的 eslint 配置:

module.exports = {
    parser: "@typescript-eslint/parser", // Specifies the ESLint parser
    parserOptions: {
        ecmaVersion: 2020, // Allows for the parsing of modern ECMAScript features
        sourceType: "module", // Allows for the use of imports
        ecmaFeatures: {
            jsx: true, // Allows for the parsing of JSX
        },
    },
    settings: {
        react: {
            version: "detect", // Tells eslint-plugin-react to automatically detect the version of React to use
        },
        "json/sort-package-json": ["name", "version", "private", "scripts", "dependencies", "devDependencies"],
    },
    plugins: ["@typescript-eslint", "prettier", "json-format", "simple-import-sort", "unused-imports"],
    extends: [
        "eslint:recommended",
        "plugin:@typescript-eslint/eslint-recommended",
        "plugin:@typescript-eslint/recommended",
        "plugin:prettier/recommended",
    ],
    rules: {
        "sort-imports": "off",
        "@typescript-eslint/no-namespace": "off",
        "@typescript-eslint/ban-ts-comment": "off",
        "simple-import-sort/imports": "error",
        "simple-import-sort/exports": "error",
        "no-unused-vars": "off",
        "unused-imports/no-unused-imports": "error",
        "@typescript-eslint/no-var-requires": "warn",
        "@typescript-eslint/no-empty-function": "warn",
        "@typescript-eslint/ban-types": "warn",
        "unused-imports/no-unused-vars": [
            "warn",
            { vars: "all", varsIgnorePattern: "^_", args: "after-used", argsIgnorePattern: "^_" },
        ],
        eqeqeq: ["error", "smart"],
    },
    overrides: [
        {
            files: ["*.js"],
            rules: {
                "no-undef": "off",
            },
        },
    ],
};

另外,不确定为什么我在本地和 GitHub 中得到 2 个不同的输出? 我在本地运行

yarn install
并且工作流程也在运行它,因此这不应该是不同的依赖版本问题。我该如何解决这个问题?

typescript github-actions eslint
1个回答
0
投票
  • Node.js 可能运行不同版本的 eslint 或 打字稿。在您的工作流程文件中,node-version: 指定为 20.x。你可以检查这个
  • GitHub Actions 中使用的缓存可能会导致问题。你也可以检查一下。
  • 最后,您可以使用
    yarn list --pattern eslint
    命令比较两个环境中安装的版本。

我可以再给你提供一件我每次都会用到的东西: eslint 配置

"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_", "varsIgnorePattern": "^_", "ignoreRestSiblings": true, "caughtErrorsIgnorePattern": "^_", "ignoreRestSiblings": true, "destructuredArrayIgnorePattern": "^_", "ignoreTypeReferences": true }]

正如评论中提到的,您可以确保软件包已正确安装

通过运行

yarn install --immutable
命令,确保以与工作流程中相同的方式安装依赖项。

我希望这可以帮助您解决您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.