TSLint Does Not Catch TypeScript Errors

Explains why linting alone isn’t enough—TSLint enforces style but won’t catch TypeScript type errors, so add tsc to your git hooks.

Sep 10, 2019 · 1 min read · 182 Words · -Views -Comments · Programming
TypeScript 🇨🇳 阅读中文版
Read with AI

Choose an AI assistant

ChatGPT Open in a new tab with the article link and prompt
Claude Copy the prompt, then open Claude

Open Claude
Trouble reading the article?

If the prompt is missing, copy the full prompt below into your AI assistant. If the AI cannot read the article, paste the visible passage you want to discuss and add your question.

The title says it all, but the details matter. TSLint enforces code style—it doesn’t guarantee your TypeScript compiles. Let’s walk through an example.

What TSLint Reports

lint output

Lint errors include the violated rule. Many editors auto-fix simple issues (like quote style), so the commit diff might be empty.

Type Errors Slip Through

Consider:

interface IStudent {
  name: string;
  sex: 'male' | 'female';
  age: number;
}

Instantiating IStudent with only one property produces a TypeScript error in the editor, but git commit still succeeds because TSLint is fine with it.

type error
commit succeeds

CI would fail later. To prevent that, run tsc before you commit.

Add tsc to Git Hooks

Example Husky config:

{
  "hooks": {
    "pre-commit": "tsc --noEmit && lint-staged",
    "pre-push": "yarn run test"
  }
}

Now the commit fails until the type error is fixed.

commit blocked

Note: --noEmit prevents output files. This slows commits slightly but catches type regressions early—a worthwhile trade-off.

Final Thoughts

Frequent refactors introduce type mistakes. Without tsc in the pipeline, you’ll ship broken builds. Adding it to your hooks keeps upstream code healthier.

References

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover