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?
- Centralizes all types in
.d.tsfiles so the type layer is clearly separated from code. - Makes refactoring safer because types have single definitions rather than scattered inline copies.
- 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.
| Option | Type | Default | Description |
|---|---|---|---|
ignoreFiles | string[] | [] | 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.tsfile — 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
: MyTypeare allowed — Only plainTSTypeReferencewithout type arguments passes. Generics like: Map<K, V>are still flagged because the type arguments are inline. as constis allowed — Theas constassertion is a plain type reference with no type arguments, so it passes.as SomeType<T>is allowed whenTis a type parameter — Generic type arguments that reference enclosing type parameters are considered dynamic dependencies and are not flagged.- Type predicates like
value is MyTypeare allowed —TSTypePredicateannotations (type-guard return types) are not flagged. - Destructuring declarations are allowed without an annotation —
const { a, b } = objand array destructuring are skipped by the inference check; only plain identifier declarations are required to be annotated. - Loop variables are allowed without an annotation —
for...of,for, andfor...inloop variables are skipped by the inference check. - Unannotated
const/letinside.map/.filter/.reduce(etc.) callbacks is allowed — variables declared inside arrow callbacks passed tomap,filter,find,findIndex,some,every,reduce,flatMap,forEach,sort, orfromare 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,sveltehosts) are skipped silently, because the extracted block is plain JavaScript and adding a type annotation there would be a parse error.