Skip to main content

No Inline Type Annotation

Require named type aliases instead of inline type annotations in code files so every type is defined in a .d.ts file.

Summary

The no-inline-type-annotation rule reports any type annotation or as assertion that is not a plain TSTypeReference without concrete type arguments. It also reports const/let declarations inside function or method bodies that have no type annotation, requiring a named type instead of relying on inference; top-level (module-scope) declarations are not required to carry an annotation.

Arrays, unions, intersections, object literals, tuples, generics, function types, and primitive keywords are all flagged — both in the : Type annotation position and the as Type assertion position.

Generic type arguments that reference enclosing type parameters (e.g., as SomeType<T> inside a function f<T>()) are allowed because the dependency is dynamic.

The rule skips .d.ts files where inline types are expected.

Files that hold <script> tags (astro, md, markdown, html, htm, vue, svelte) are skipped silently, because processor-based ESLint plugins extract each plain <script> block into a .js virtual file, where the TypeScript annotations this rule demands could never parse.

Why Use This Rule?

  1. Centralizes all types in .d.ts files so the type layer is clearly separated from code.
  2. Makes refactoring safer because types have single definitions rather than scattered inline copies.
  3. Produces descriptive type names that document what each variable represents.

Examples

Configuration

Options

warning

ignoreFiles is an escape hatch for files where this rule genuinely does not apply. It is not intended for routine use.

OptionTypeDefaultDescription
ignoreFilesstring[][]File names to skip. Supports a leading-* suffix (*.test.ts), an exact path, or a trailing path segment; ** globs are not supported.

Autofix

Autofix is not available for this rule. Flagged code must be updated manually.

Troubleshooting

  • Warning fires on a .d.ts file — The rule skips files ending in .d.ts. If the warning fires anyway, verify that the file extension is correct and that the filename reaches the rule.
  • Simple type references like : MyType are allowed — Only plain TSTypeReference without type arguments passes. Generics like : Map<K, V> are still flagged because the type arguments are inline.
  • as const is allowed — The as const assertion is a plain type reference with no type arguments, so it passes.
  • as SomeType<T> is allowed when T is a type parameter — Generic type arguments that reference enclosing type parameters are considered dynamic dependencies and are not flagged.
  • Type predicates like value is MyType are allowedTSTypePredicate annotations (type-guard return types) are not flagged.
  • Destructuring declarations are allowed without an annotationconst { a, b } = obj and array destructuring are skipped by the inference check; only plain identifier declarations are required to be annotated.
  • Loop variables are allowed without an annotationfor...of, for, and for...in loop variables are skipped by the inference check.
  • Unannotated const/let inside .map/.filter/.reduce (etc.) callbacks is allowed — variables declared inside arrow callbacks passed to map, filter, find, findIndex, some, every, reduce, flatMap, forEach, sort, or from are skipped, since their types are driven by the iterated collection.
  • Code inside a <script> block is not checked — Extracted <script> virtual files (astro, md, markdown, html, htm, vue, svelte hosts) are skipped silently, because the extracted block is plain JavaScript and adding a type annotation there would be a parse error.