跳轉到主要內容

概述

@typescript-eslint/eslint-plugin 包含超過 100 個規則,專門用於偵測專屬於 TypeScript 程式碼的最佳實務違規、錯誤和/或文法問題。我們的規則如下方清單所示。

提示

我們建議使用 預先定義的設定檔 之一來啟用建議規則的大型集合,而不是逐一啟用規則。

規則

規則依照字母順序排列。可以依據下列類別選擇性地篩選

配置群組 (⚙️)
資料

(這些類別說明在 下方更詳細.)

規則
⚙️
🔧
💭
🧱
📐
💀
@typescript-eslint/adjacent-overload-signatures
要求函數重載簽章連續
🎨
@typescript-eslint/array-type
針對陣列,要求一致使用 T[]Array<T>
🎨🔧
@typescript-eslint/await-thenable
禁止等待不可 Thenable 的值
💡💭
@typescript-eslint/ban-ts-comment
禁止 @ts-<指令> 註解,或要求指令後提供描述
💡
@typescript-eslint/ban-tslint-comment
禁止 // tslint:<規則標記> 註解
🎨🔧
@typescript-eslint/ban-types
禁止特定的類型
🔧
💡
@typescript-eslint/block-spacing
禁止或強制在區塊開啟後及關閉前於區塊內使用空白
🔧🧱📐💀
@typescript-eslint/brace-style
針對區塊強制使用一致的大括弧樣式
🔧🧱📐💀
@typescript-eslint/class-literal-property-style
強制類別上的字面量以一致的樣式顯示
🎨💡
@typescript-eslint/class-methods-use-this
強制類別方法使用 this
🧱
@typescript-eslint/comma-dangle
要求或禁止尾隨逗號
🔧🧱📐💀
@typescript-eslint/comma-spacing
在逗號前及後強制使用一致的間距
🔧🧱📐💀
@typescript-eslint/consistent-generic-constructors
針對構造函式呼叫的類型註解或構造函式名稱強制指定一般類型參數
🎨🔧
@typescript-eslint/consistent-indexed-object-style
要求或禁止 Record 類型
🎨🔧
@typescript-eslint/consistent-return
要求 return 陳述式永遠或永不指定值
💭🧱
@typescript-eslint/consistent-type-assertions
強制一致使用類型斷言
🎨🔧
💡
@typescript-eslint/consistent-type-definitions
強制類型定義一致使用 interfacetype
🎨🔧
@typescript-eslint/consistent-type-exports
強制一致使用類型匯出
🔧💭
@typescript-eslint/consistent-type-imports
強制一致使用類型匯入
🔧
@typescript-eslint/default-param-last
強制預設參數放最後
🧱
@typescript-eslint/dot-notation
只要可能,強制使用點表示法
🎨🔧💭🧱
@typescript-eslint/explicit-function-return-type
要求函數和類別方法有明確的回傳類型
@typescript-eslint/explicit-member-accessibility
要求類別屬性和方法有明確的可存取修飾子
🔧
💡
@typescript-eslint/explicit-module-boundary-types
要求已匯出的函數和類別公開類別方法有明確的回傳和參數類型
@typescript-eslint/func-call-spacing
要求或禁止函數識別碼和其呼叫之間的間距
🔧🧱📐💀
@typescript-eslint/indent
強制一致的縮排
🔧🧱📐💀
@typescript-eslint/init-declarations
要求或禁止變數宣告中的初始值
🧱
@typescript-eslint/key-spacing
在類型和介面上強制屬性名稱和類型註解之間使用一致的間距
🔧🧱📐💀
@typescript-eslint/keyword-spacing
在關鍵字前及後強制使用一致的間距
🔧🧱📐💀
@typescript-eslint/lines-around-comment
要求註解周圍留空行
🔧🧱📐💀
@typescript-eslint/lines-between-class-members
要求或禁止類別成員之間保留空行
🔧🧱📐💀
@typescript-eslint/max-params
在函數定義中強制限制參數數量
🧱
@typescript-eslint/member-delimiter-style
要求對介面和類型文字使用特定成員分隔符號樣式
🔧📐💀
@typescript-eslint/member-ordering
要求一致的成員宣告順序
@typescript-eslint/method-signature-style
強制使用特定方法簽章語法
🔧
@typescript-eslint/naming-convention
強制對整個程式庫中的所有內容使用命名慣例
💭
@typescript-eslint/no-array-constructor
禁止一般 Array 構造函式
🔧🧱
@typescript-eslint/no-array-delete
禁止對陣列值使用 delete 算子
🔒💡💭
@typescript-eslint/no-base-to-string
要求只在字串化時提供有用的資訊的物件上呼叫 .toString()
💭
@typescript-eslint/no-confusing-non-null-assertion
禁止在會造成混淆的位置使用非空斷言
🎨💡
@typescript-eslint/no-confusing-void-expression
要求 void 型別的表達式顯示在陳述式的位子
🔒🔧
💡
💭
@typescript-eslint/no-dupe-class-members
禁止重複的類別成員
🧱
@typescript-eslint/no-duplicate-enum-values
不允許多餘的 enum 成員值
@typescript-eslint/no-duplicate-type-constituents
不允許多餘的 union 或 intersection 類型的組成部分
🔧💭
@typescript-eslint/no-dynamic-delete
不允許在運算後的鍵值表達式中使用 delete 營運子
🔒🔧
@typescript-eslint/no-empty-function
不允許空函式
🎨🧱
@typescript-eslint/no-empty-interface
不允許宣告空介面
🎨🔧
💡
@typescript-eslint/no-explicit-any
不允許 any 型別
🔧
💡
@typescript-eslint/no-extra-non-null-assertion
不允許額外非空斷言
🔧
@typescript-eslint/no-extra-parens
不允許不必要的括號
🔧🧱📐💀
@typescript-eslint/no-extra-semi
不允許不必要的分號
🔧🧱💀
@typescript-eslint/no-extraneous-class
不允許被當作命名空間使用的 class
🔒
@typescript-eslint/no-floating-promises
要求 Promise 型式語句被適當處理
💡💭
@typescript-eslint/no-for-in-array
不允許使用 for-in 迴圈迭代陣列
💭
@typescript-eslint/no-implied-eval
不允許使用類似 eval() 的方法
💭🧱
@typescript-eslint/no-import-type-side-effects
在 import 僅有指定內嵌型別限定詞的說明符時,強制使用頂層 import 型別限定詞
🔧
@typescript-eslint/no-inferrable-types
不允許對初始化為數字、字串或布林值的變數或參數進行明確的型別宣告
🎨🔧
@typescript-eslint/no-invalid-this
不允許在 class 或類似 class 的物件之外使用 this 關鍵字
🧱
@typescript-eslint/no-invalid-void-type
不允許在泛型或回傳型別之外使用 void 型別
🔒
@typescript-eslint/no-loop-func
不允許函式宣告包含在迴圈陳述式內部的不安全參照
🧱
@typescript-eslint/no-loss-of-precision
不允許精確度會降低的字面數字
🧱
@typescript-eslint/no-magic-numbers
不允許魔力數字
🧱
@typescript-eslint/no-meaningless-void-operator
不允許 void 營運子,但用於捨棄值時除外
🔒🔧
💡
💭
@typescript-eslint/no-misused-new
強制正確定義 newconstructor
@typescript-eslint/no-misused-promises
不允許 Promises 出現在未設計為處理它們的地方
💭
@typescript-eslint/no-mixed-enums
不允許包含數字和字串成員的列舉
🔒💭
@typescript-eslint/no-namespace
不允許 TypeScript 命名空間
@typescript-eslint/no-non-null-asserted-nullish-coalescing
不允許在 Null 合併運算子的左操作數中使用非空斷言
🔒💡
@typescript-eslint/no-non-null-asserted-optional-chain
不允許在選擇性鍊式表達式後使用非空斷言
💡
@typescript-eslint/no-non-null-assertion
不允許使用 ! 後置運算子進行非空斷言
🔒💡
@typescript-eslint/no-redeclare
不允許重新宣告變數
🧱
@typescript-eslint/no-redundant-type-constituents
不允許不執行任何操作或覆寫型別資訊的 Unions 和 Intersections 成員
💭
@typescript-eslint/no-require-imports
不允許呼叫 require()
@typescript-eslint/no-restricted-imports
當透過 import 載入時,不允許指定的模組
🧱
@typescript-eslint/no-shadow
不允許變數宣告遮蔽在外層範圍宣告的變數
🧱
@typescript-eslint/no-this-alias
不允許給 this 複寫別名
@typescript-eslint/no-throw-literal
不允許將字面值拋出為例外
💭🧱💀
@typescript-eslint/no-type-alias
不允許型別別名
💀
@typescript-eslint/no-unnecessary-boolean-literal-compare
不允許與布林字面值進行不必要的等號比較
🔒🔧💭
@typescript-eslint/no-unnecessary-condition
不允許型別始終為真或始終為假的條件判斷式
🔒🔧💭
@typescript-eslint/no-unnecessary-qualifier
不允許不必要的命名空間限定詞
🔧💭
@typescript-eslint/no-unnecessary-template-expression
不允許不必要的範本表達式
🔒🔧💭
@typescript-eslint/no-unnecessary-type-arguments
不允許等於預設值的型別參數
🔒🔧💭
@typescript-eslint/no-unnecessary-type-assertion
不允許不改變表達式型別的型別斷言
🔧💭
@typescript-eslint/no-unnecessary-type-constraint
不允許對泛型型別進行不必要的限制
💡
@typescript-eslint/no-unsafe-argument
不允許使用型別為 any 的值呼叫函式
💭
@typescript-eslint/no-unsafe-assignment
不允許將型別為 any 的值指定給變數和屬性
💭
@typescript-eslint/no-unsafe-call
不允許呼叫型別為 any 的值
💭
@typescript-eslint/no-unsafe-declaration-merging
不允許不安全的宣告合併
@typescript-eslint/no-unsafe-enum-comparison
不允許比較列舉值與非列舉值
💡💭
@typescript-eslint/no-unsafe-member-access
不允許使用型別為 any 的值的成員存取
💭
@typescript-eslint/no-unsafe-return
不允許從函式回傳型別為 any 的值
💭
@typescript-eslint/no-unsafe-unary-minus
要求一元否定運算子接收數字
💭
@typescript-eslint/no-unused-expressions
不允許未使用的表達式
🧱
@typescript-eslint/no-unused-vars
不允許未使用的變數
🧱
@typescript-eslint/no-use-before-define
不允許在定義變數之前使用它們
🧱
@typescript-eslint/no-useless-constructor
不允許不必要的建構函式
🔒🧱
@typescript-eslint/no-useless-empty-export
禁止在模組檔案中不需要的輸出且不改變任何內容
🔧
@typescript-eslint/no-useless-template-literals
不允許不必要的範本表達式
🔧💭💀
@typescript-eslint/no-var-requires
禁止 require 敘述,除非在 import 敘述中
@typescript-eslint/non-nullable-type-assertion-style
執行在明確型態轉換上對於非 null 的斷言
🎨🔧💭
@typescript-eslint/object-curly-spacing
在括號內強制一致的空白
🔧🧱📐💀
@typescript-eslint/only-throw-error
禁止將非 Error 值擲回作為例外
🔒💭🧱
@typescript-eslint/padding-line-between-statements
要求或是不允許敘述間的填充行
🔧🧱📐💀
@typescript-eslint/parameter-properties
要求或是不允許類別建構函式中的參數屬性
@typescript-eslint/prefer-as-const
執行在字面型態上使用 as const
🔧
💡
@typescript-eslint/prefer-destructuring
在陣列和/或物件中要求解構
🔧💭🧱
@typescript-eslint/prefer-enum-initializers
要求每個列舉成員值都要明確地初始化
💡
@typescript-eslint/prefer-find
在尋找單一結果時,執行使用 Array.prototype.find() 代替 Array.prototype.filter() 後接 [0]
💡💭
@typescript-eslint/prefer-for-of
盡可能執行使用 for-of 循環代替標準 for 循環
🎨
@typescript-eslint/prefer-function-type
執行使用函式型態,而不是具有呼叫簽章的介面
🎨🔧
@typescript-eslint/prefer-includes
執行 includes 方法代替 indexOf 方法
🔒🔧💭
@typescript-eslint/prefer-literal-enum-member
要求所有列舉成員都是文字值
🔒
@typescript-eslint/prefer-namespace-keyword
要求使用 namespace 關鍵字而不是 module 關鍵字來宣告自訂 TypeScript 模組
🎨🔧
@typescript-eslint/prefer-nullish-coalescing
執行使用 nullish 合併運算子,而不是邏輯賦值或串聯
🎨💡💭
@typescript-eslint/prefer-optional-chain
執行使用簡潔的選擇性鏈結表現式,而不是鏈結邏輯與、否定的邏輯或,或空物件
🎨🔧
💡
💭
@typescript-eslint/prefer-promise-reject-errors
要求使用錯誤物件作為承諾拒絕的理由
🔒💭🧱
@typescript-eslint/prefer-readonly
如果私有成員永遠不會在建構函式外變更,要求標記為 readonly
🔧💭
@typescript-eslint/prefer-readonly-parameter-types
要求函式參數被標記為 readonly,以防止意外地變更輸入
💭
@typescript-eslint/prefer-reduce-type-parameter
執行在呼叫 Array#reduce 時使用型態參數,而不是轉換
🔒🔧💭
@typescript-eslint/prefer-regexp-exec
如果沒有提供全域旗標,執行 RegExp#exec 而非 String#match
🔧💭
@typescript-eslint/prefer-return-this-type
執行在只傳回 this 型態時使用 this
🔒🔧💭
@typescript-eslint/prefer-string-starts-ends-with
執行使用 String#startsWithString#endsWith,而不是用於檢查子字串的其他等效方法
🎨🔧💭
@typescript-eslint/prefer-ts-expect-error
執行使用 @ts-expect-error,而不是 @ts-ignore
🔧💀
@typescript-eslint/promise-function-async
要求任何傳回承諾的函式或方法標記為非同步
🔧💭
@typescript-eslint/quotes
執行一致地使用反引號、雙引號或單引號
🔧🧱📐💀
@typescript-eslint/require-array-sort-compare
要求 Array#sortArray#toSorted 呼叫永遠提供 compareFunction
💭
@typescript-eslint/require-await
禁止非同步函式,除非傳回承諾且有 await 表現式
💭🧱
@typescript-eslint/restrict-plus-operands
要求加法的兩個運算元為相同型態,且為 bigintnumberstring
💭
@typescript-eslint/restrict-template-expressions
執行明確文字表現式為 string 型態
💭
@typescript-eslint/return-await
執行一致等待傳回的承諾
🔧
💡
💭🧱
@typescript-eslint/semi
要求或是不允許分號,而不是 ASI
🔧🧱📐💀
@typescript-eslint/sort-type-constituents
執行強制型態聯集/交集的組成部分按字母順序排列
🔧
💡
💀
@typescript-eslint/space-before-blocks
執行一致的塊前空白
🔧🧱📐💀
@typescript-eslint/space-before-function-paren
執行一致的函式括號前空白
🔧🧱📐💀
@typescript-eslint/space-infix-ops
要求在中綴運算子周圍有空白
🔧🧱📐💀
@typescript-eslint/strict-boolean-expressions
禁止在布林表現式中使用特定型態
🔧
💡
💭
@typescript-eslint/switch-exhaustiveness-check
要求 switch-case 敘述要窮舉
💡💭
@typescript-eslint/triple-slash-reference
禁止用於 ES6 風格匯入宣告的特定三斜線指令
@typescript-eslint/type-annotation-spacing
要求在型態註解周圍有一致的空白
🔧📐💀
@typescript-eslint/typedef
在特定的地方要求型態註解
@typescript-eslint/unbound-method
實施未繫結方法的呼叫方式與預期的範圍相符
💭
@typescript-eslint/unified-signatures
禁止可以使用聯集或選擇性/餘數參數統一的兩個重載
🔒
@typescript-eslint/use-unknown-in-catch-callback-variable
.catch() 回呼函式中鍵入引數類型強制為 unknown
🔒🔧
💡
💭

過濾

設定群組 (⚙️)

「設定群組」是指包含規則的預定義設定。從設定預設值延伸,允許一次啟用大量建議規則。

資料

  • 🔧 fixable 是指規則是否包含ESLint --fix 自動修正程式
  • 💡 has suggestions 是指規則是否包含 ESLint 建議修正程式。
    • 有時候,不能使用自動修正程式自動修正程式碼是安全的。但在這些情況下,我們通常可以猜測正確的修正方式,並提供建議給開發人員。
  • 💭 requires type information 是指規則是否需要鍵入式程式碼檢查
  • 🧱 extension rule 表示規則是ESLint 核心規則的延伸(請參閱延伸規則)。
  • 📐 formatting rule 表示規則與格式化相關。
  • 💀 deprecated rule 表示規則不應再使用,並將在未來的版本中從外掛中移除。

延伸規則

有些 ESLint 核心規則不支援 TypeScript 語法:它們會發生異常,忽略語法,或對語法回報錯誤。在這些情況下,我們會建立所謂的「延伸規則」:我們外掛中的一個規則,具有相同的執行功能,但也會支援 TypeScript。

延伸規則一般會完全取代 ESLint 核心中的基本規則。如果你從某個設定延伸,而其中啟用了基本規則,則需要停用基本規則

module.exports = {
extends: ['eslint:recommended'],
rules: {
// Note: you must disable the base rule as it can report incorrect errors
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': 'error',
},
};

在這個頁面搜尋🧱 extension rule以查看所有延伸規則。