@typescript-eslint/typedef
Require type annotations in the places you choose
Also called typescript-eslint/typedef, typescript/typedef
TypeScript infers most types. Some teams still want them written out in certain places, for example on every parameter, so a reader doesn't need an editor to see them.
Every option is off by default, so this rule reports nothing until you turn on at least one.
typescript-eslint has deprecated this rule and no longer recommends it.
Examples#
Reported with parameter and arrowParameter on:
{ "parameter": true, "arrowParameter": true }2 problemsfunction greet(nameExpected name to have a type annotation.) {
return `Hello ${name}`;
}
const double = valueExpected value to have a type annotation. => value * 2;{ "parameter": true, "arrowParameter": true }No problemsfunction greet(name: string) {
return `Hello ${name}`;
}
const double = (value: number) => value * 2;Options#
| Option | Default | What it does |
|---|---|---|
parameter | false | Require a type on function and method parameters. |
arrowParameter | false | Require a type on arrow function parameters. |
variableDeclaration | false | Require a type on const, let, and var. Destructuring has its own options. |
variableDeclarationIgnoreFunction | false | Don't require a type on a variable or a class property that's set to a function. |
arrayDestructuring | false | Require a type on const [a, b] = .... |
objectDestructuring | false | Require a type on const { a, b } = .... |
memberVariableDeclaration | false | Require a type on class properties. |
propertyDeclaration | false | Require a type on interface and type literal properties. |
Both are reported with variableDeclaration:
{ "variableDeclaration": true }2 problemsconst retries = 3Expected retries to have a type annotation.;
const log = (message: string) => console.log(message)Expected log to have a type annotation.;Add variableDeclarationIgnoreFunction to leave functions alone. The variable in a for...of or for...in loop never needs a type:
{ "variableDeclaration": true, "variableDeclarationIgnoreFunction": true }No problemsconst retries: number = 3;
const log = (message: string) => console.log(message);
for (const attempt of [1, 2, 3]) {
log(`attempt ${attempt}`);
}Reported with arrayDestructuring and objectDestructuring:
{ "arrayDestructuring": true, "objectDestructuring": true }2 problemsconst [first]Expected a type annotation. = [1, 2];
const { name }Expected a type annotation. = { name: "Ada" };The type goes after the pattern:
{ "arrayDestructuring": true, "objectDestructuring": true }No problemsconst [first]: number[] = [1, 2];
const { name }: { name: string } = { name: "Ada" };Reported with memberVariableDeclaration and propertyDeclaration:
{ "memberVariableDeclaration": true, "propertyDeclaration": true }2 problemsclass Counter {
count = 0;Expected count to have a type annotation.
}
interface Options {
verbose;Expected verbose to have a type annotation.
}When not to use it#
Annotations that repeat what TypeScript infers add noise, and const mode: string = "dark" loses the more precise type "dark". To catch values that silently become any, turn on noImplicitAny in your tsconfig.json instead.