站長阿川

站長阿川私房教材:
學 JavaScript 前端,帶作品集去面試!

站長精心設計,帶你實作 63 個小專案,得到作品集!

立即開始免費試讀!

概述

在啟動 Next.js 專案時,許多人會按原樣使用預設的eslint.config.mjs

您知道next/typescript實際上做什麼嗎?

create-next-app時的eslint.config.mjs如下所示:

import { dirname } from "path";
import { fileURLToPath } from "url";
import { FlatCompat } from "@eslint/eslintrc";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const compat = new FlatCompat({
  baseDirectory: __dirname,
});

const eslintConfig = [
  ...compat.extends("next/core-web-vitals", "next/typescript"),
];

export default eslintConfig;

如果你查看官方文件,它會說:

這些規則是基於 plugin:@typescript-eslint/recommended。請參閱 typescript-eslint > 配置

了解更多詳情。

這些規則是基於 plugin:@typescript-eslint/recommended。更多資訊請參閱 typescript-eslint > Configs。

我知道它是基於@typescript-eslint/recommended ,但我不知道它是否完全繼承了它,是否有任何差異,或者是否加入了額外的規則。

於是就跟著原始碼寫了一篇文章,作為備忘。

目標受眾

  • 負責制定 ESLint 規則的人員

  • 不想直接複製貼上技術文章中設定檔的人

結論

  • @typescript-eslint/recommended幾乎相同

  • @typescript-eslint/no-unused-vars@typescript-eslint/no-unused-expressions剛剛從error變為warn

環境

| 依賴項 | 版本 |

|------------------------------|------------|

| 下一個 | 15.4.6 |

| eslint-config-next | 15.4.6 |

| @typescript-eslint/eslint插件 | 8.40.0 |

本文中的原始碼來自上述版本,將來可能會有所更改。

next/typescript內容

儘管eslint.config.mjs列出了next/typescript ,但實際使用的套件是eslint-config-next ,並且規則在該套件中名稱為typescrit.js的檔案中定義。

現在我們來看看@typescript-eslint/recommended的定義。

差異可以概括如下:[^1]

| | @typescript-eslint/recommended | eslint-config-next |

|------------------------------------------------|--------------------------------|--------------------|

| @typescript-eslint/no-unused-vars | 錯誤 | 警告 |

| @typescript-eslint/no-unused-expressions | 錯誤 | 警告 |

就我個人而言,我認為如果變化如此之小,則沒有必要建立單獨的next/typescript ......但我很高興我的疑慮得到了解決。

總結

  • @typescript-eslint/recommended幾乎相同

  • @typescript-eslint/no-unused-vars@typescript-eslint/no-unused-expressions剛剛從error變為warn


原文出處:https://qiita.com/kskwtnk/items/5e5f85b0d3d0e8811f93


共有 0 則留言


精選技術文章翻譯,幫助開發者持續吸收新知。
站長阿川

站長阿川私房教材:
學 JavaScript 前端,帶作品集去面試!

站長精心設計,帶你實作 63 個小專案,得到作品集!

立即開始免費試讀!