What is tslint?
The tslint npm package is a static analysis tool that checks TypeScript code for readability, maintainability, and functionality errors. It is widely used to enforce a consistent code style by checking the code against a set of linting rules.
What are tslint's main functionalities?
Linting TypeScript Files
This feature allows you to lint TypeScript files by specifying a configuration file and a pattern to match files. The command will process all TypeScript files in the 'src' directory and its subdirectories.
tslint -c tslint.json 'src/**/*.ts'
Fixing Linting Errors Automatically
This feature automatically fixes linting errors that can be corrected without human intervention. It is useful for fixing simple issues like whitespace or semicolon usage.
tslint --fix -c tslint.json 'src/**/*.ts'
Custom Rules
This feature allows you to use custom linting rules in addition to the predefined rules. You can specify a directory containing custom rule definitions to be applied to your code.
tslint -c tslint.json 'src/**/*.ts' --rules-dir custom_rules
Other packages similar to tslint
eslint
ESLint is a popular linting tool for JavaScript and TypeScript. It is highly configurable and extendable, with a large ecosystem of plugins. ESLint has effectively replaced TSLint as the preferred linter for TypeScript after TSLint's deprecation.
prettier
Prettier is an opinionated code formatter that supports many languages, including TypeScript. While it does not perform static code analysis, it formats code to a consistent style. Prettier can be used alongside linters like ESLint.
stylelint
Stylelint is a modern linter that helps you avoid errors and enforce conventions in your stylesheets. Although it is primarily used for CSS, it can be used in conjunction with PostCSS to lint SCSS, Sass, Less, and other CSS-like languages.
TSLint
TSLint is an extensible static analysis tool that checks TypeScript code for readability, maintainability, and functionality errors. It is widely supported across modern editors & build systems and can be customized with your own lint rules, configurations, and formatters.
TSLint supports:
- custom lint rules
- custom formatters (failure reporters)
- inline disabling and enabling of rules with comment flags
- configuration presets (
tslint:latest
, tslint-react
, etc.) and plugin composition - automatic fixing of formatting & style violations
- integration with MSBuild, Grunt, Gulp, Atom, Eclipse, Emacs, Sublime, Vim, Visual Studio 2015, Visual Studio 2017, Visual Studio code, WebStorm and more
Installation & Usage
Please refer to the full installation & usage documentation on the TSLint website. There, you'll find information about
Custom Rules & Plugins
Custom rule sets from Palantir
If we don't have all the rules you're looking for, you can either write your own custom rules or use rules implementations developed by the community. The repos below are a good source of custom rules:
Development
Requirements:
Quick Start
git clone git@github.com:palantir/tslint.git --config core.autocrlf=input --config core.eol=lf
yarn
yarn compile
yarn test
Creating a new release
- Bump the version number in
package.json
and src/linter.ts
- Add release notes in
CHANGELOG.md
- Use
./scripts/generate-changelog.js
(after building it with tsc -p scripts
) to generate the changelog diff. This script expects a Github.com personal access token to exist at ~/github_token.txt
with "repo" permissions.
- Commit with message
Prepare release <version>
- Push your branch to GitHub and make a PR
- Once your PR is merged, wait for the tests to pass on CircleCI for develop
- Create a "Release" on GitHub with the proper tag version and notes from the changelog.
- The tag should be identical to the version in
package.json
- Run
yarn run publish:local
v5.8.0
:warning: Deprecations
- [deprecation]
typeof-compare
is deprecated because typescript already does that check (#3286) - [deprecation] CLI argument
--type-check
is no longer necessary and will be removed in the next major version (#3322)
Updates to tslint:latest
configuration
+ "ban-comma-operator": true,
+ "jsdoc-format": {
+ options: "check-multiline-start",
+ },
+ "no-duplicate-switch-case": true,
+ "no-implicit-dependencies": true,
+ "no-return-await": true,
:tada: Features
:hammer_and_wrench: Bugfixes & enhancements
- [bugfix]
no-empty-interface
allows providing type arguments for extended type (#3260) - [bugfix] Fixed line switches to not disable failures in the next line following the disabled line (#3177)
- [bugfix]
return-undefined
handles union return types in async functions (#3298) - [bugfix]
deprecation
checks correct constructor overload (#3203) - [bugfix]
return-undefined
declared return type takes precedence over contextual type (#3298) - [bugfix] Correctly mark
inputFilePath
as an optional parameter in Configuration.findConfiguration()
(#3195) - [bugfix]
return-undefined
fixed regressions: once again allows anything if return type is any
(#3298) - [bugfix]
only-arrow-functions
allow function if this
is used in parameter initializer (#3315) - [bugfix]
no-conditional-assignment
: exclude intentional assignments, e.g. inside functions (#2629) - [bugfix]
no-angle-bracket-type-assertion
fixer adds parentheses when necessary (#3301) - [bugfix]
no-angle-bracket-type-assertion
fixed order when autofixing consecutive assertions (#3301) - [bugfix]
vso
formatter no longer duplicates output for fixed failures (#3348) - [bugfix]
no-unbound-method
: Allow negation of method (#3349) - [bugfix]
arrow-parens
with option "ban-single-arg-parens"
no longer produces invalid code when fixed (#3247) - [bugfix] Fixed regression where the lookup of
tslint.json
stopped at the current directory. (#3309) - [bugfix]
--test
works correctly with any compilerOptions.target
(#3296) - [bugfix]
whitepace
handles files with BOM and other irregular whitespace (#3305) - [bugfix]
callable-types
auto fix produces invalid results (#3342) - [bugfix]
no-string-literal
correctly fix property names with leading underscores (#3184) - [bugfix]
variable-name
fixed crash on empty variable name (#3292) - [bugfix]
trailing-comma
fixed crash on arrow function without parens (#3246) - [bugfix] Fix
space-before-function-paren
for anonymous/arrow generic functions (#3085) - [bugfix] Removed warning printed to console when using the
no-unused-variable
along with the noUnusedLocals
and noUnusedParameters
compiler options (#3227) - [bugfix]
no-invalid-this
ignores functions with a this
param (#3267) - [enhancement] Sort failures by line and character for formatters (#3345)
- [enhancement]
import-blacklist
also checks exports and dynamic imports (#3258) - [enhancement]
no-conditional-assignment
added check for conditional (ternary) expressions (#2629) - [enhancement] Allow
completed-docs
to list doc tags that mark a node as not requiring a documentation body. Tags can also provide a regexp matcher to validate that their contents are docs-valid. (#2415) - [enhancement]
await-promise
enforces that for-await-of
is only used with AsyncIterable
(#3297) - [enhancement]
one-line
checks type alias declarations (#3200) - [enhancement]
deprecation
checks object destructuring (#3318) - [enhancement]
no-submodule-imports
also checks exports (#3258) - [enhancement]
restrict-plus-operands
: More specific error message when arguments include strings (#3220) - [enhancement]
no-unsafe-any
checks more expressions, for example destructuring, yield
, property initializer (#3196) - [enhancement]
object-literal-sort-keys
: allow grouping of object properties via additional blank lines when using alphabetical ordering. (#3191) - [enhancement] Migrated CLI from using
colors
module to chalk
module (#3171) - [enhancement]
no-unused-variable
applies the ignorePattern to imports (#3187)
Thanks to our contributors!
- Klaus Meinhardt
- Charles Samborski
- Donald Pipowitch
- Josh Goldberg
- mmkal
- Erik
- Csaba Miklos
- Dominik Moritz
- Khalid Saifullah
- Lukas Spieß
- Merott Movahedi
- Bowen Ni
- ksvitkovsky
- Hutson Betts
- Caleb Eggensperger
- Brent Erickson
- Trivikram
- Brandon Furtwangler
- Pavel Zet
- aervin_
- Holger Jeromin
- Danny Guo
- Jeremy Morton
- Cyril Gandon
- Andy Hanson
- yadan