@typescript-eslint/explicit-module-boundary-types

Require parameter and return types on exported functions and the methods of exported classes

Also called typescript-eslint/explicit-module-boundary-types, typescript/explicit-module-boundary-types

If TypeScript infers an exported function's return type from its body, a change inside the function changes the type that every importing file sees. Nobody notices while making the edit, and the errors show up in those other files.

This rule checks exported functions and the non-private methods of exported classes. They need a return type and a type for every parameter, and that type can't be any. A parameter with a default value is fine without a type. Functions that aren't exported aren't checked.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"@typescript-eslint/explicit-module-boundary-types" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"@typescript-eslint/explicit-module-boundary-types" = ["error", {
  allowArgumentsExplicitlyTypedAsAny = false,
  allowTypedFunctionExpressions = true,
  allowHigherOrderFunctions = true,
  allowDirectConstAssertionInArrowFunctions = true,
  allowOverloadFunctions = false,
  allowedNames = [], # list of strings
}]

Examples#

example.ts3 problems
export function parsePrice(text) {
  return Number(text.replace("$", ""));
}

export function log(value: any): void {
  console.log(value);
}

An export further down the file counts too:

example.ts1 problem
const isEmpty = (items: string[]) => items.length === 0;

export { isEmpty };
example.tsNo problems
export function parsePrice(text: string): number {
  return Number(strip(text));
}

export function log(value: unknown, level = "info"): void {
  console.log(level, value);
}

function strip(text: string) {
  return text.replace("$", "");
}

export class Cart {
  private count() {
    return 0;
  }
}

Options#

OptionDefaultWhat it does
allowArgumentsExplicitlyTypedAsAnyfalseAllow a parameter that's written as any.
allowTypedFunctionExpressionstrueAllow a function expression that gets its type from a typed variable or property, or from an as.
allowHigherOrderFunctionstrueAllow a function that returns another function right away to go without a return type. The inner function is still checked.
allowDirectConstAssertionInArrowFunctionstrueAllow () => value as const to go without a return type.
allowOverloadFunctionsfalseSkip the implementation of a function that has overload signatures.
allowedNames[]Names of functions and methods that are never reported.

With allowTypedFunctionExpressions, the type on the variable is enough:

example.tsNo problems
type Check = (items: string[]) => boolean;

export const isEmpty: Check = items => items.length === 0;
example.tswith { "allowTypedFunctionExpressions": false }2 problems
type Check = (items: string[]) => boolean;

export const isEmpty: Check = items => items.length === 0;

With allowHigherOrderFunctions, only the inner function needs a return type:

example.tsNo problems
export const multiplyBy = (factor: number) => (value: number): number => value * factor;

With allowOverloadFunctions, callers only see the overload signatures:

example.tswith { "allowOverloadFunctions": true }No problems
export function double(value: number): number;
export function double(value: string): string;
export function double(value: number | string) {
  return typeof value === "number" ? value * 2 : value + value;
}
example.ts1 problem
export function double(value: number): number;
export function double(value: string): string;
export function double(value: number | string) {
  return typeof value === "number" ? value * 2 : value + value;
}