TSLint core rules
Lint rules encode logic for syntactic & semantic checks of TypeScript source code.
TypeScript-specific
These rules find errors related to TypeScript features:
-
TS Onlyadjacent-overload-signatures - Enforces function overloads to be consecutive.
-
TS Onlyban-ts-ignore - Bans “// @ts-ignore” comments from being used.
-
TS Onlyban-types - Bans specific types from being used. Does not ban the corresponding runtime objects from being used.
-
TS Only Has Fixermember-access - Requires explicit visibility declarations for class members.
-
Has Fixermember-ordering - Enforces member ordering.
-
TS Onlyno-any - Disallows usages of
anyas a type declaration. -
TS Onlyno-empty-interface - Forbids empty interfaces.
-
no-for-in - Ban the usage of for…in statements.
-
no-import-side-effect - Avoid import statements with side-effect.
-
TS Only Has Fixerno-inferrable-types - Disallows explicit type declarations for variables or parameters initialized to a number, string, or boolean.
-
TS Only Has Fixerno-internal-module - Disallows internal
module -
no-magic-numbers - Disallows the use constant number values outside of variable assignments. When no list of allowed values is specified, -1, 0 and 1 are allowed by default.
-
TS Onlyno-namespace - Disallows use of internal
modules andnamespaces. -
TS Onlyno-non-null-assertion - Disallows non-null assertions using the
!postfix operator. -
no-parameter-reassignment - Disallows reassigning parameters.
-
no-reference - Disallows
/// <reference path=>imports (use ES6-style imports instead). -
TS Only Has Fixer Requires Type Infono-unnecessary-type-assertion - Warns if a type assertion does not change the type of an expression.
-
TS Onlyno-var-requires - Disallows the use of require statements except in import statements.
-
only-arrow-functions - Disallows traditional (non-arrow) function expressions.
-
prefer-for-of - Recommends a ‘for-of’ loop over a standard ‘for’ loop if the index is only used to access the array being iterated.
-
Requires Type Infopromise-function-async - Requires any function or method that returns a promise to be marked async.
-
TS Onlytypedef - Requires type definitions to exist.
-
TS Onlyunified-signatures - Warns for any two overloads that could be unified into one by using a union or an optional/rest parameter.
Functionality
These rules catch common errors in JS programming or otherwise confusing constructs that are prone to producing bugs:
-
TS Only Requires Type Infoawait-promise - Warns for an awaited value that is not a Promise.
-
ban-comma-operator - Disallows the comma operator to be used.
-
ban - Bans the use of specific functions or global methods.
-
Has Fixercurly - Enforces braces for
if/for/do/whilestatements. -
forin - Requires a
for ... instatement to be filtered with anifstatement. -
function-constructor - Prevents using the built-in Function constructor.
-
import-blacklist - Disallows importing the specified modules via
importandrequire, or importing specific named exports of the specified modules, or using imports matching specified regular expression patterns. -
label-position - Only allows labels in sensible locations.
-
no-arg - Disallows use of
arguments.callee. -
no-async-without-await - Functions marked async must contain an await or return statement.
-
no-bitwise - Disallows bitwise operators.
-
no-conditional-assignment - Disallows any type of assignment in conditionals.
-
no-console - Bans the use of specified
consolemethods. -
no-construct - Disallows access to the constructors of
String,Number, andBoolean. -
no-debugger - Disallows
debuggerstatements. -
no-duplicate-super - Warns if ‘super()’ appears twice in a constructor.
-
no-duplicate-switch-case - Prevents duplicate cases in switch statements.
-
no-duplicate-variable - Disallows duplicate variable declarations in the same block scope.
-
no-dynamic-delete - Bans usage of the delete operator with computed key expressions.
-
no-empty - Disallows empty blocks.
-
no-eval - Disallows
evalfunction invocations. -
TS Only Requires Type Infono-floating-promises - Promises returned by functions must be handled appropriately.
-
Requires Type Infono-for-in-array - Disallows iterating over an array with a for-in loop.
-
no-implicit-dependencies - Disallows importing modules that are not listed as dependency in the project’s package.json
-
TS Only Requires Type Infono-inferred-empty-object-type - Disallow type inference of {} (empty object type) at function and constructor call sites
-
no-invalid-template-strings - Warns on use of
${in non-template strings. -
no-invalid-this - Disallows using the
thiskeyword outside of classes. -
TS Onlyno-misused-new - Warns on apparent attempts to define constructors for interfaces or
newfor classes. -
Has Fixerno-null-keyword - Disallows use of the
nullkeyword literal. -
TS Only Requires Type Infono-null-undefined-union - Disallows explicitly declared or implicitly returned union types with both
nullandundefinedas members. -
TS Onlyno-object-literal-type-assertion - Forbids an object literal to appear in a type assertion expression. Casting to
anyor tounknownis still allowed. -
TS Only Requires Type Infono-promise-as-boolean - Warns for Promises that are used for boolean conditions.
-
Requires Type Infono-restricted-globals - Disallow specific global variables.
-
Has Fixerno-return-await - Disallows unnecessary
return await. -
no-shadowed-variable - Disallows shadowing variable declarations.
-
no-sparse-arrays - Forbids array literals to contain missing elements.
-
Has Fixerno-string-literal - Forbids unnecessary string literal property access. Allows
obj["prop-erty"](can’t be a regular property access). Disallowsobj["property"](should beobj.property). -
Has Fixerno-string-throw - Flags throwing plain strings or concatenations of strings.
-
no-submodule-imports - Disallows importing any submodule.
-
no-switch-case-fall-through - Disallows falling through case statements.
-
no-tautology-expression - Enforces that relational/equality binary operators does not take two equal variables/literals as operands. Expression like 3 === 3, someVar === someVar, “1” > “1” are either a tautology or contradiction, and will produce an error.
-
no-this-assignment - Disallows unnecessary references to
this. -
TS Only Requires Type Infono-unbound-method - Warns when a method is used outside of a method call.
-
no-unnecessary-class - Disallows classes that are not strictly necessary.
-
TS Only Requires Type Infono-unsafe-any - Warns when using an expression of type ‘any’ in a dynamic way. Uses are only allowed if they would work for
{} | null | undefined. Downcasting to unknown is always safe. Type casts and tests are allowed. Expressions that work on all values (such as"" + x) are allowed. -
no-unsafe-finally - Disallows control flow statements, such as
return,continue,breakandthrowsin finally blocks. -
no-unused-expression - Disallows unused expression statements.
-
TS Only Has Fixer Requires Type Infono-unused-variable - Disallows unused imports, variables, functions and private class members. Similar to tsc’s –noUnusedParameters and –noUnusedLocals options, but does not interrupt code compilation.
-
Requires Type Infono-use-before-declare - Disallows usage of variables before their declaration.
-
Has Fixerno-var-keyword - Disallows usage of the
varkeyword. -
Requires Type Infono-void-expression - Requires expressions of type
voidto appear in statement position. -
prefer-conditional-expression - Recommends to use a conditional expression instead of assigning to the same thing in each branch of an if statement.
-
Has Fixerprefer-object-spread - Enforces the use of the ES2018 object spread operator over
Object.assign()where appropriate. -
radix - Requires the radix parameter to be specified when calling
parseInt. -
Requires Type Inforestrict-plus-operands - When adding two variables, operands must both be of type number or of type string.
-
Has Fixerstatic-this - Ban the use of
thisin static methods. -
TS Only Requires Type Infostrict-boolean-expressions - Restricts the types allowed in boolean expressions. By default only booleans are allowed. The following nodes are checked:
- Arguments to the
!,&&, and||operators - The condition in a conditional expression (
cond ? x : y) - Conditions for
if,for,while, anddo-whilestatements.
- Arguments to the
-
Requires Type Infostrict-comparisons - Only allow comparisons between primitives.
-
TS Only Has Fixer Requires Type Infostrict-string-expressions - Disable implicit toString() calls
-
TS Only Requires Type Infostrict-type-predicates - Warns for type predicates that are always true or always false. Works for ‘typeof’ comparisons to constants (e.g. ‘typeof foo === “string”’), and equality comparison to ‘null’/’undefined’. (TypeScript won’t let you compare ‘1 === 2’, but it has an exception for ‘1 === undefined’.) Does not yet work for ‘instanceof’. Does not warn for ‘if (x.y)’ where ‘x.y’ is always truthy. For that, see strict-boolean-expressions. This rule requires
strictNullChecksto work properly. -
switch-default - Require a
defaultcase in allswitchstatements. -
triple-equals - Requires
===and!==in place of==and!=. -
typeof-compare - Makes sure result of
typeofis compared to correct string values -
unnecessary-constructor - Prevents blank constructors, as they are redundant.
-
TS Only Requires Type Infouse-default-type-parameter - Warns if an explicitly specified type argument is the default for that type parameter.
-
use-isnan - Enforces use of the
isNaN()function to check for NaN references instead of a comparison to theNaNconstant.
Maintainability
These rules make code maintenance easier:
-
cyclomatic-complexity - Enforces a threshold of cyclomatic complexity.
-
Requires Type Infodeprecation - Warns when deprecated APIs are used.
-
TS Onlyinvalid-void - Disallows usage of
voidtype outside of generic or return types. Ifvoidis used as return type, it shouldn’t be a part of intersection/union type. -
max-classes-per-file - A file may not contain more than the specified number of classes
-
max-file-line-count - Requires files to remain under a certain number of lines
-
no-default-export - Disallows default exports in ES6-style modules.
-
no-default-import - Disallows importing default members from certain ES6-style modules.
-
no-duplicate-imports - Disallows multiple import statements from the same module.
-
TS Onlyno-mergeable-namespace - Disallows mergeable namespaces in the same file.
-
no-require-imports - Disallows invocation of
require(). -
object-literal-sort-keys - Checks ordering of keys in object literals. When using the default alphabetical ordering, additional blank lines may be used to group object properties together while keeping the elements within each group in alphabetical order. To opt out of this use ignore-blank-lines option.
-
Has Fixerprefer-const - Requires that variable declarations use
constinstead ofletandvarif possible. -
TS Only Requires Type Infoprefer-readonly - Requires that private variables are marked as
readonlyif they’re never modified outside of the constructor.
Style
These rules enforce consistent style across your codebase:
-
TS Only Has Fixerarray-type - Requires using either ‘T[]’ or ‘Array
' for arrays. -
Has Fixerarrow-return-shorthand - Suggests to convert
() => { return x; }to() => x. -
binary-expression-operand-order - In a binary expression, a literal should always be on the right-hand side if possible. For example, prefer ‘x + 1’ over ‘1 + x’.
-
TS Only Has Fixercallable-types - An interface or literal type with just a call signature can be written as a function type.
-
class-name - Enforces PascalCased class and interface names.
-
Has Fixercomment-format - Enforces formatting rules for single-line comments.
-
comment-type - Allows a limited set of comment types
-
completed-docs - Enforces JSDoc comments for important items be filled out.
-
encoding - Enforces UTF-8 file encoding.
-
Has Fixerfile-header - Enforces a certain header comment for all files, matched by a regular expression.
-
file-name-casing - Enforces a consistent file naming convention
-
increment-decrement - Enforces using explicit += 1 or -= 1 operators.
-
TS Onlyinterface-name - Requires interface names to begin with a capital ‘I’
-
TS Only Has Fixerinterface-over-type-literal - Prefer an interface declaration over a type literal (
type T = { ... }) -
TS Only Requires Type Infomatch-default-export-name - Requires that a default import have the same name as the declaration it imports. Does nothing for anonymous default exports.
-
newline-per-chained-call - Requires that chained method calls be broken apart onto separate lines.
-
TS Only Has Fixerno-angle-bracket-type-assertion - Requires the use of
as Typefor type assertions instead of<Type>. -
TS Only Has Fixer Requires Type Infono-boolean-literal-compare - Warns on comparison to a boolean literal, as in
x === true. -
TS Onlyno-parameter-properties - Disallows parameter properties in class constructors.
-
TS Onlyno-redundant-jsdoc - Forbids JSDoc which duplicates TypeScript functionality.
-
TS Onlyno-reference-import - Don’t
<reference types="foo" />if you importfooanyway. -
no-unnecessary-callback-wrapper - Replaces
x => f(x)with justf. To catch more cases, enableonly-arrow-functionsandarrow-return-shorthandtoo. -
Has Fixerno-unnecessary-initializer - Forbids a ‘var’/’let’ statement or destructuring initializer to be initialized to ‘undefined’.
-
TS Only Has Fixer Requires Type Infono-unnecessary-qualifier - Warns when a namespace qualifier (
A.x) is unnecessary. -
Has Fixerobject-literal-key-quotes - Enforces consistent object literal property quote style.
-
Has Fixerobject-literal-shorthand - Enforces/disallows use of ES6 object literal shorthand.
-
Has Fixerone-line - Requires the specified tokens to be on the same line as the expression preceding them.
-
one-variable-per-declaration - Disallows multiple variable definitions in the same declaration statement.
-
Has Fixerordered-imports - Requires that import statements be alphabetized and grouped.
-
prefer-function-over-method - Warns for class methods that do not use ‘this’.
-
Has Fixerprefer-method-signature - Prefer
foo(): voidoverfoo: () => voidin interfaces and types. -
prefer-switch - Prefer a
switchstatement to anifstatement with simple===comparisons. -
prefer-template - Prefer a template expression over string literal concatenation.
-
Has Fixerprefer-while - Prefer
whileloops instead offorloops without an initializer and incrementor. -
Requires Type Inforeturn-undefined - Prefer
return;in void functions andreturn undefined;in value-returning functions. -
Has Fixerspace-before-function-paren - Require or disallow a space before function parenthesis
-
Has Fixerspace-within-parens - Enforces spaces within parentheses or disallow them. Empty parentheses () are always allowed.
-
Has Fixerswitch-final-break - Checks whether the final clause of a switch statement ends in
break;. -
TS Only Has Fixertype-literal-delimiter - Checks that type literal members are separated by semicolons. Enforces a trailing semicolon for multiline type literals.
-
Requires Type Infounnecessary-bind - Prevents unnecessary and/or misleading scope bindings on functions.
-
unnecessary-else - Disallows
elseblocks followingifblocks ending with abreak,continue,return, orthrowstatement. -
variable-name - Checks variable names for various errors.
Format
These rules enforce consistent use of whitespace and punctuation:
-
Has Fixeralign - Enforces vertical alignment.
-
Has Fixerarrow-parens - Requires parentheses around the parameters of arrow function definitions.
-
Has Fixereofline - Ensures the file ends with a newline.
-
import-spacing - Ensures proper spacing between import statement keywords
-
Has Fixerindent - Enforces indentation with tabs or spaces.
-
jsdoc-format - Enforces basic format rules for JSDoc comments.
-
Has Fixerlinebreak-style - Enforces a consistent linebreak style.
-
max-line-length - Requires lines to be under a certain max length.
-
Has Fixernewline-before-return - Enforces blank line before return when not the only line in the block.
-
new-parens - Requires parentheses when invoking a constructor via the
newkeyword. -
Has Fixerno-consecutive-blank-lines - Disallows one or more blank lines in a row.
-
Has Fixerno-irregular-whitespace - Disallow irregular whitespace within a file, including strings and comments.
-
Has Fixerno-trailing-whitespace - Disallows trailing whitespace at the end of a line.
-
Has Fixernumber-literal-format - Checks that decimal literals should begin with ‘0.’ instead of just ‘.’, and should not end with a trailing ‘0’.
-
Has Fixerquotemark - Enforces quote character for string literals.
-
Has Fixersemicolon - Enforces consistent semicolon usage at the end of every statement.
-
Has Fixertrailing-comma - Requires or disallows trailing commas in array and object literals, destructuring assignments, function typings, named imports and exports and function parameters.
-
TS Only Has Fixertypedef-whitespace - Requires or disallows whitespace for type definitions.
-
Has Fixerwhitespace - Enforces whitespace style conventions.