tsComments
Reports problematic TypeScript comment directives or requires descriptions after directives.
✅ This rule is included in the ts logical presets.
TypeScript provides several comment directives that can be used to alter how it processes files. Using these to suppress TypeScript compiler errors reduces the effectiveness of TypeScript overall. Instead, it’s generally better to correct the types of code, to make directives unnecessary.
This rule can report on uses of the following comment directives:
@ts-expect-error@ts-ignore@ts-nocheck
By default, all directives are allowed. Use the options to ban specific directives or require descriptions.
Examples
Section titled “Examples”Prefer @ts-expect-error over @ts-ignore
Section titled “Prefer @ts-expect-error over @ts-ignore”@ts-ignore permanently suppresses errors even after they’re fixed.
@ts-expect-error will report if the next line has no error, helping identify stale suppressions.
// @ts-ignoreconst value: string = 123;// @ts-expect-error - assigning number to string for testingconst value: string = 123;Avoid @ts-nocheck
Section titled “Avoid @ts-nocheck”@ts-nocheck disables type checking for the entire file.
Prefer fixing errors or using targeted suppressions.
// @ts-nocheckconst value = unknownFunction();// @ts-expect-error - unknownFunction is not definedconst value = unknownFunction();Options
Section titled “Options”Each directive option can be configured with one of the following values:
true— Ban the directive entirelyfalse— Allow the directive (default)"allow-with-description"— Allow only if a description is provided{ descriptionFormat: "regex" }— Allow only if the description matches the regex
allowTsExpectError
Section titled “allowTsExpectError”Configuration for @ts-expect-error directives.
Default: false (allowed).
// With { allowTsExpectError: "allow-with-description" }
// ❌ Invalid - no description// @ts-expect-errorconst value: string = 123;
// ✅ Valid - has description// @ts-expect-error: testing number assignmentconst value: string = 123;allowTsIgnore
Section titled “allowTsIgnore”Configuration for @ts-ignore directives.
Default: false (allowed).
When banned with true, this rule provides a fixer that replaces @ts-ignore with @ts-expect-error.
// With { allowTsIgnore: true }
// ❌ Invalid - ts-ignore is banned, will suggest @ts-expect-error// @ts-ignoreconst value: string = 123;
// With { allowTsIgnore: "allow-with-description" }
// ❌ Invalid - no description// @ts-ignoreconst value: string = 123;
// ✅ Valid - has description// @ts-ignore: legacy code that will be fixed in ISSUE-123const value: string = 123;allowTsNocheck
Section titled “allowTsNocheck”Configuration for @ts-nocheck directives.
Default: false (allowed).
// With { allowTsNocheck: true }
// ❌ Invalid - ts-nocheck is banned// @ts-nocheckconst value = unknownFunction();minimumDescriptionLength
Section titled “minimumDescriptionLength”The minimum length required for a description when using "allow-with-description".
Default: 10.
// With { allowTsExpectError: "allow-with-description", minimumDescriptionLength: 15 }
// ❌ Invalid - description too short// @ts-expect-error: testconst value: string = 123;
// ✅ Valid - description is long enough// @ts-expect-error: testing number to string assignmentconst value: string = 123;descriptionFormat
Section titled “descriptionFormat”When using an object configuration, you can specify a descriptionFormat regex that the description must match:
// With { allowTsExpectError: { descriptionFormat: "^: TS\\d+ because .+$" } }
// ❌ Invalid - description doesn't match format// @ts-expect-error: some random reasonconst value: string = 123;
// ✅ Valid - description matches format// @ts-expect-error: TS2322 because we need to test thisconst value: string = 123;Example Configuration
Section titled “Example Configuration”{ "rules": { "ts/tsComments": { "options": { "allowTsExpectError": "allow-with-description", "allowTsIgnore": true, "allowTsNocheck": true, "minimumDescriptionLength": 10 } } }}When Not To Use It
Section titled “When Not To Use It”If you have legacy code with many @ts-ignore comments that you cannot immediately migrate,
you may want to disable this rule temporarily.