TSLint Does Not Catch TypeScript Errors

TSLint Does Not Catch TypeScript Errors

9月 10, 2019 · 1 分钟阅读时长 · 182 字 · -阅读 -评论

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

Alan H
Authors
开发者,数码产品爱好者,喜欢折腾,喜欢分享,喜欢开源