@typescript-eslint/typedef

Require type annotations in the places you choose

deprecated

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:

example.tswith { "parameter": true, "arrowParameter": true }2 problems
function greet(name) {
  return `Hello ${name}`;
}

const double = value => value * 2;
example.tswith { "parameter": true, "arrowParameter": true }No problems
function greet(name: string) {
  return `Hello ${name}`;
}

const double = (value: number) => value * 2;

Options#

OptionDefaultWhat it does
parameterfalseRequire a type on function and method parameters.
arrowParameterfalseRequire a type on arrow function parameters.
variableDeclarationfalseRequire a type on const, let, and var. Destructuring has its own options.
variableDeclarationIgnoreFunctionfalseDon't require a type on a variable or a class property that's set to a function.
arrayDestructuringfalseRequire a type on const [a, b] = ....
objectDestructuringfalseRequire a type on const { a, b } = ....
memberVariableDeclarationfalseRequire a type on class properties.
propertyDeclarationfalseRequire a type on interface and type literal properties.

Both are reported with variableDeclaration:

example.tswith { "variableDeclaration": true }2 problems
const retries = 3;
const log = (message: string) => console.log(message);

Add variableDeclarationIgnoreFunction to leave functions alone. The variable in a for...of or for...in loop never needs a type:

example.tswith { "variableDeclaration": true, "variableDeclarationIgnoreFunction": true }No problems
const 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:

example.tswith { "arrayDestructuring": true, "objectDestructuring": true }2 problems
const [first] = [1, 2];
const { name } = { name: "Ada" };

The type goes after the pattern:

example.tswith { "arrayDestructuring": true, "objectDestructuring": true }No problems
const [first]: number[] = [1, 2];
const { name }: { name: string } = { name: "Ada" };

Reported with memberVariableDeclaration and propertyDeclaration:

example.tswith { "memberVariableDeclaration": true, "propertyDeclaration": true }2 problems
class Counter {
  count = 0;
}

interface Options {
  verbose;
}

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.