Shared TSLint config to enforce a consistent code style for LoopBack development
npx @tessl/cli install tessl/npm-loopback--tslint-config@2.1.0@loopback/tslint-config provides shared TSLint configurations to enforce consistent code style and quality standards for LoopBack TypeScript development. It offers two main configuration files: a core common configuration for essential development-time linting, and an extended build configuration that includes type-checking rules for comprehensive build-time validation.
npm install @loopback/tslint-configThis package doesn't provide programmatic imports. Instead, configurations are consumed through TSLint's extends mechanism:
Basic tslint.json:
{
"$schema": "http://json.schemastore.org/tslint",
"extends": ["@loopback/tslint-config/tslint.common.json"]
}Build-time tslint.build.json:
{
"$schema": "http://json.schemastore.org/tslint",
"extends": ["@loopback/tslint-config/tslint.build.json"]
}After installing the package, configure TSLint to extend the LoopBack configurations:
For development-time linting (editor integration):
{
"$schema": "http://json.schemastore.org/tslint",
"extends": ["@loopback/tslint-config/tslint.common.json"],
"rules": {
// Add any project-specific rule overrides here
}
}For comprehensive build-time linting:
{
"$schema": "http://json.schemastore.org/tslint",
"extends": ["@loopback/tslint-config/tslint.build.json"],
"rules": {
// Add any project-specific rule overrides here
}
}Core TSLint configuration with essential rules for TypeScript development that work without type checking.
{
"$schema": "http://json.schemastore.org/tslint",
"rulesDirectory": ["tslint-consistent-codestyle"],
"rules": {
"adjacent-overload-signatures": true,
"prefer-for-of": true,
"unified-signatures": true,
"no-any": true,
"label-position": true,
"no-arg": true,
"no-construct": true,
"no-duplicate-variable": true,
"no-invalid-this": true,
"no-misused-new": true,
"no-shadowed-variable": true,
"no-string-throw": true,
"no-unused": [true, "ignore-parameters"],
"no-unused-expression": true,
"no-var-keyword": true,
"triple-equals": [true, "allow-null-check", "allow-undefined-check"]
}
}Configuration Path: @loopback/tslint-config/tslint.common.json
Rule Categories:
adjacent-overload-signatures, prefer-for-of, unified-signatures, no-anylabel-position, no-arg, no-construct, no-duplicate-variable, no-invalid-this, no-misused-new, no-shadowed-variable, no-string-throwno-unused, no-unused-expression, no-var-keyword, triple-equalsDependencies Required: tslint-consistent-codestyle plugin for enhanced code style consistency.
Extended TSLint configuration that includes all common rules plus additional type-checking rules for build-time validation.
{
"$schema": "http://json.schemastore.org/tslint",
"extends": ["./tslint.common.json"],
"rules": {
"await-promise": [true, "PromiseLike", "RequestPromise"],
"no-floating-promises": [true, "PromiseLike", "RequestPromise"],
"no-unused-variable": false,
"no-void-expression": [true, "ignore-arrow-function-shorthand"]
}
}Configuration Path: @loopback/tslint-config/tslint.build.json
Inherits: All rules from tslint.common.json
Additional Rules:
await-promise, no-floating-promises (supports PromiseLike and RequestPromise types)no-void-expression with arrow function shorthand supportno-unused-variable in favor of the no-unused rule from common configUse Case: Build pipelines and comprehensive type-checking validation where full TypeScript compilation context is available.
TypeScript-specific rules:
adjacent-overload-signatures: Ensures overloaded functions are grouped togetherprefer-for-of: Recommends for-of loops over traditional for loops when possibleunified-signatures: Warns for overloads that could be unified into a single signatureno-any: Prohibits usage of the any typeError prevention rules:
no-arg: Disallows access to arguments.calleeno-construct: Disallows calling constructor of String, Number, and Booleanno-duplicate-variable: Disallows duplicate variable declarationsno-invalid-this: Disallows using this in classes incorrectlyno-misused-new: Warns on apparent attempts to define constructors for interfacesno-shadowed-variable: Disallows shadowing variable declarationsno-string-throw: Flags throwing plain strings or concatenations of stringsno-unused-expression: Disallows unused expression statementstriple-equals: Requires === and !== over == and !=Build-time specific rules:
await-promise: Warns for awaiting a value that is not a Promiseno-floating-promises: Requires Promise-returning functions to be handled appropriatelyno-void-expression: Requires expressions of type void to be used in statement positionRuntime Dependencies:
{
"dependencies": {
"tslint-consistent-codestyle": "^1.14.1"
}
}Peer Dependencies:
{
"peerDependencies": {
"tslint": ">=5.11.0"
}
}Environment Requirements:
{
"engines": {
"node": ">=8.9"
}
}Create a tslint.json file in your project root:
{
"$schema": "http://json.schemastore.org/tslint",
"extends": ["@loopback/tslint-config/tslint.common.json"],
"rules": {
// Override specific rules if needed
"no-console": true
}
}Create a separate tslint.build.json for your build process:
{
"$schema": "http://json.schemastore.org/tslint",
"extends": ["@loopback/tslint-config/tslint.build.json"],
"rules": {
// Build-specific overrides
}
}Then run TSLint with type checking:
tslint --project tsconfig.json --config tslint.build.json 'src/**/*.ts'Most TypeScript-enabled editors will automatically use tslint.json for real-time linting. The common configuration is optimized for this use case as it doesn't require type information and provides fast feedback.
You can extend the LoopBack configuration with your own rules:
{
"$schema": "http://json.schemastore.org/tslint",
"extends": ["@loopback/tslint-config/tslint.common.json"],
"rules": {
// Disable a rule from the base config
"no-any": false,
// Add additional rules
"max-line-length": [true, 120],
"prefer-const": true
}
}