An extensible static analysis linter for the TypeScript language
—
TSLint provides a comprehensive command-line interface for linting TypeScript and JavaScript files with extensive configuration options.
tslint [options] [files...]# Lint specific files
tslint src/app.ts src/utils.ts
# Lint with glob patterns
tslint src/**/*.ts
# Lint TypeScript and JavaScript files
tslint "src/**/*.{ts,tsx,js,jsx}"
# Lint all files in project
tslint --project tsconfig.json# Auto-fix violations
tslint --fix src/**/*.ts
# Generate initial configuration
tslint --init
# Check configuration for a file
tslint --print-config src/app.ts
# Run in test mode (for rule development)
tslint --test test/rules/# Configuration
-c, --config <path> # Configuration file path (default: tslint.json)
-p, --project <path> # TypeScript project path (tsconfig.json)
--print-config <file> # Print resolved configuration for file
# File Processing
-e, --exclude <pattern> # Exclude file patterns (repeatable)
--files <file> # Read file list from file
# Output Control
-o, --out <path> # Output file (default: stdout)
-s, --format <format> # Output format (default: stylish)
--formatters-dir <path> # Custom formatters directory
-q, --quiet # Show errors only, suppress warnings
--outputAbsolutePaths # Use absolute paths in output
# Rule Management
-r, --rules-dir <path> # Custom rules directory (repeatable)
--fix # Auto-fix select rule violations
--force # Return exit code 0 even with errors
# Special Modes
--init # Generate initial tslint.json
--test [dir] # Test mode for rule development
-t, --type-check # Enable type checking (deprecated)
# Information
--version # Show version number
-h, --help # Show help message# Use specific configuration file
tslint -c custom-tslint.json src/**/*.ts
# Use configuration relative to each file (default behavior)
tslint src/**/*.ts
# Print resolved configuration for debugging
tslint --print-config src/components/App.tsx# Lint entire TypeScript project
tslint --project .
# Lint project with custom config
tslint --project . -c strict-tslint.json
# Lint specific files from project
tslint --project . src/specific-file.ts# Exclude single pattern
tslint --exclude "node_modules/**" src/**/*.ts
# Exclude multiple patterns
tslint --exclude "**/*.spec.ts" --exclude "**/*.d.ts" src/**/*.ts
# Use configuration file exclusions
# In tslint.json:
# {
# "linterOptions": {
# "exclude": ["node_modules/**", "dist/**"]
# }
# }# Use built-in formatters
tslint -s json src/**/*.ts # JSON format
tslint -s checkstyle src/**/*.ts # Checkstyle XML
tslint -s junit src/**/*.ts # JUnit XML
tslint -s prose src/**/*.ts # Prose format
# Output to file
tslint -s json -o lint-results.json src/**/*.ts
# Use custom formatter
tslint -s my-custom --formatters-dir ./formatters src/**/*.ts
# Absolute paths in output
tslint --outputAbsolutePaths src/**/*.ts# Auto-fix all fixable violations
tslint --fix src/**/*.ts
# Auto-fix with specific configuration
tslint --fix -c tslint.json src/**/*.ts
# Combine with other options
tslint --fix --project . --format stylishTSLint uses standard exit codes to indicate results:
# Exit code meanings:
# 0 - Success (no lint errors found, or --force used)
# 1 - Lint errors found
# 2 - Configuration error or invalid usage# Ignore lint errors in CI (not recommended)
tslint --force src/**/*.ts
echo "Exit code: $?" # Always 0
# Proper CI usage
tslint src/**/*.ts
if [ $? -eq 0 ]; then
echo "Linting passed"
else
echo "Linting failed"
exit 1
fi# Different configs for different file types
tslint -c tslint-strict.json src/core/**/*.ts
tslint -c tslint-loose.json src/legacy/**/*.ts
# Environment-specific linting
tslint -c tslint.production.json src/**/*.ts # Production rules
tslint -c tslint.development.json src/**/*.ts # Development rules# Use custom rules directory
tslint -r ./custom-rules src/**/*.ts
# Multiple rules directories
tslint -r ./custom-rules -r ./team-rules src/**/*.ts
# Combine with npm packages
tslint -r node_modules/tslint-react/rules src/**/*.tsx# Create file list
find src -name "*.ts" -not -path "*/node_modules/*" > files.txt
# Lint from file list
tslint --files files.txt
# Programmatic file list generation
git diff --name-only --diff-filter=AM master | grep "\.tsx\?$" | xargs tslint# GitLab CI example
tslint --format checkstyle --out tslint-report.xml src/**/*.ts
# GitHub Actions example
tslint --format json --out tslint-results.json src/**/*.ts
# Jenkins integration
tslint --format junit --out TEST-tslint.xml src/**/*.ts# Generate basic configuration
tslint --init
# Generates tslint.json:
{
"defaultSeverity": "error",
"extend": [
"tslint:recommended"
],
"jsRules": {},
"rules": {},
"rulesDirectory": []
}# Create custom init script
cat > init-tslint.sh << 'EOF'
#!/bin/bash
tslint --init
# Customize generated config
sed -i 's/"tslint:recommended"/"tslint:latest"/' tslint.json
echo "Custom TSLint configuration created"
EOF
chmod +x init-tslint.sh
./init-tslint.sh# Test all rules in directory
tslint --test test/rules/
# Test specific rule
tslint --test test/rules/no-console/
# Test with custom rules directory
tslint --test test/rules/ --rules-dir custom-rules/
# Verbose test output
tslint --test test/rules/ 2>&1 | tee test-results.log# 1. Create test files
mkdir -p test/rules/my-new-rule
cat > test/rules/my-new-rule/test.ts.lint << 'EOF'
console.log('test');
~~~~~~~~~~~~~~~~~~~ [Console statements are not allowed]
EOF
# 2. Create test configuration
cat > test/rules/my-new-rule/tslint.json << 'EOF'
{
"rules": {
"my-new-rule": true
}
}
EOF
# 3. Run tests
tslint --test test/rules/my-new-rule/ --rules-dir src/rules/# Use project-based linting for type checking
tslint --project tsconfig.json # Faster than individual files
# Exclude unnecessary files
tslint --exclude "**/*.d.ts" --exclude "node_modules/**" src/**/*.ts
# Parallel processing with xargs
find src -name "*.ts" | xargs -P 4 -I {} tslint {}# Batch processing script
for dir in src/*/; do
echo "Linting $dir"
tslint --project "$dir" --format json >> lint-results.json
done
# Memory-efficient processing
find src -name "*.ts" -print0 | xargs -0 -n 50 tslint{
"scripts": {
"lint": "tslint --project .",
"lint:fix": "tslint --project . --fix",
"lint:check": "tslint-config-prettier-check ./tslint.json",
"lint:ci": "tslint --project . --format checkstyle --out tslint-report.xml",
"pretest": "npm run lint",
"precommit": "tslint --project . --format stylish"
}
}# Makefile
.PHONY: lint lint-fix lint-ci
lint:
@echo "Running TSLint..."
@tslint --project . --format stylish
lint-fix:
@echo "Auto-fixing TSLint violations..."
@tslint --project . --fix
lint-ci:
@echo "Running TSLint for CI..."
@tslint --project . --format checkstyle --out tslint-report.xml
@tslint --project . --format json --out tslint-results.json
check: lint
@echo "All checks passed!"# Pre-commit hook (.git/hooks/pre-commit)
#!/bin/sh
echo "Running TSLint on staged files..."
# Get staged TypeScript files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep "\.tsx\?$")
if [ "$STAGED_FILES" = "" ]; then
echo "No TypeScript files staged. Skipping TSLint."
exit 0
fi
# Run TSLint on staged files
echo "$STAGED_FILES" | xargs tslint
LINT_RESULT=$?
if [ $LINT_RESULT -ne 0 ]; then
echo "TSLint failed. Fix errors before committing."
echo "Run 'tslint --fix <files>' to auto-fix some errors."
exit 1
fi
echo "TSLint passed!"
exit 0# Dockerfile
FROM node:14-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
# Lint during build
RUN npx tslint --project . --format stylish
# Or create linting stage
FROM node:14-alpine AS linting
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npx tslint --project . --format json --out /tmp/lint-results.json
FROM node:14-alpine AS production
COPY --from=linting /app .
# Continue with production build# Error: Could not find config file at: tslint.json
# Solutions:
tslint --init # Create default config
tslint -c ./path/to/config.json # Specify config path# Error: Cannot read property 'getSourceFile' of undefined
# Solutions:
tslint --project . # Use project-based linting
tslint --type-check src/**/*.ts # Enable type checking (deprecated)# Slow linting on large codebases
# Solutions:
tslint --project . --exclude "**/*.d.ts" # Exclude declaration files
tslint src/**/*.ts # Avoid project mode if not needed# Debug configuration resolution
tslint --print-config src/app.ts
# Verbose output
DEBUG=* tslint src/**/*.ts
# Check file discovery
tslint --project . --files /dev/stdout
# Validate configuration
node -e "console.log(JSON.parse(require('fs').readFileSync('./tslint.json')))"# Check for deprecated options
grep -r "type-check\|no-unused-variable" tslint.json
# Convert to ESLint (when migrating)
npx tslint-to-eslint-config
# Update configurations
npx tslint-config-prettier-check ./tslint.json--project for type-aware rulesnode_modules, build outputs, and declaration filesjson for CI, stylish for development# Good CI script
set -e # Exit on any error
echo "Installing dependencies..."
npm ci
echo "Running TSLint..."
npx tslint --project . --format checkstyle --out tslint-report.xml
echo "TSLint passed successfully!"# Development cycle
npm run lint # Check for issues
npm run lint:fix # Auto-fix what's possible
# Manual fixes for remaining issues
npm run lint # Verify all issues resolved
git add . && git commit -m "Fix linting issues"Install with Tessl CLI
npx tessl i tessl/npm-tslint