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
any
as a type declaration. -
TS Onlyno-empty-interface - Forbids empty interfaces.
-
-
-
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
-
-
TS Onlyno-namespace - Disallows use of internal
module
s andnamespace
s. -
TS Onlyno-non-null-assertion - Disallows non-null assertions using the
!
postfix operator. -
-
/// <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.
-
-
-
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.
-
-
-
Has Fixercurly - Enforces braces for
if
/for
/do
/while
statements. -
for ... in
statement to be filtered with anif
statement. -
-
import
andrequire
, or importing specific named exports of the specified modules, or using imports matching specified regular expression patterns. -
-
arguments.callee
. -
-
-
-
console
methods. -
String
,Number
, andBoolean
. -
debugger
statements. -
-
-
-
-
-
eval
function 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.
-
-
TS Only Requires Type Infono-inferred-empty-object-type - Disallow type inference of {} (empty object type) at function and constructor call sites
-
${
in non-template strings. -
this
keyword outside of classes. -
TS Onlyno-misused-new - Warns on apparent attempts to define constructors for interfaces or
new
for classes. -
Has Fixerno-null-keyword - Disallows use of the
null
keyword literal. -
TS Only Requires Type Infono-null-undefined-union - Disallows explicitly declared or implicitly returned union types with both
null
andundefined
as members. -
TS Onlyno-object-literal-type-assertion - Forbids an object literal to appear in a type assertion expression. Casting to
any
or tounknown
is 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
. -
-
-
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.
-
-
-
-
this
. -
TS Only Requires Type Infono-unbound-method - Warns when a method is used outside of a method call.
-
-
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. -
return
,continue
,break
andthrows
in finally blocks. -
-
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
var
keyword. -
Requires Type Infono-void-expression - Requires expressions of type
void
to appear in statement position. -
-
Has Fixerprefer-object-spread - Enforces the use of the ES2018 object spread operator over
Object.assign()
where appropriate. -
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
this
in 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-while
statements.
- 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
strictNullChecks
to work properly. -
default
case in allswitch
statements. -
===
and!==
in place of==
and!=
. -
typeof
is compared to correct string values -
-
TS Only Requires Type Infouse-default-type-parameter - Warns if an explicitly specified type argument is the default for that type parameter.
-
isNaN()
function to check for NaN references instead of a comparison to theNaN
constant.
Maintainability
These rules make code maintenance easier:
-
-
Requires Type Infodeprecation - Warns when deprecated APIs are used.
-
TS Onlyinvalid-void - Disallows usage of
void
type outside of generic or return types. Ifvoid
is used as return type, it shouldn’t be a part of intersection/union type. -
-
-
-
-
-
TS Onlyno-mergeable-namespace - Disallows mergeable namespaces in the same file.
-
require()
. -
-
Has Fixerprefer-const - Requires that variable declarations use
const
instead oflet
andvar
if possible. -
TS Only Requires Type Infoprefer-readonly - Requires that private variables are marked as
readonly
if 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
. -
-
TS Only Has Fixercallable-types - An interface or literal type with just a call signature can be written as a function type.
-
-
Has Fixercomment-format - Enforces formatting rules for single-line comments.
-
-
-
-
Has Fixerfile-header - Enforces a certain header comment for all files, matched by a regular expression.
-
-
-
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.
-
-
TS Only Has Fixerno-angle-bracket-type-assertion - Requires the use of
as Type
for 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 importfoo
anyway. -
x => f(x)
with justf
. To catch more cases, enableonly-arrow-functions
andarrow-return-shorthand
too. -
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.
-
-
Has Fixerordered-imports - Requires that import statements be alphabetized and grouped.
-
-
Has Fixerprefer-method-signature - Prefer
foo(): void
overfoo: () => void
in interfaces and types. -
switch
statement to anif
statement with simple===
comparisons. -
-
Has Fixerprefer-while - Prefer
while
loops instead offor
loops 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.
-
else
blocks followingif
blocks ending with abreak
,continue
,return
, orthrow
statement. -
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.
-
-
Has Fixerindent - Enforces indentation with tabs or spaces.
-
-
Has Fixerlinebreak-style - Enforces a consistent linebreak style.
-
-
Has Fixernewline-before-return - Enforces blank line before return when not the only line in the block.
-
new
keyword. -
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.