Skip to main content

explicit-function-return-type

Require explicit return types on functions and class methods.

Functions in TypeScript often don't need to be given an explicit return type annotation. Leaving off the return type is less code to read or write and allows the compiler to infer it from the contents of the function.

However, explicit return types do make it visually more clear what type is returned by a function. They can also speed up TypeScript type checking performance in large codebases with many large functions.

This rule enforces that functions do have an explicit return type annotation.

eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/explicit-function-return-type": "error"
}
});

Try this rule in the playground ↗

Examples

// Should indicate that no value is returned (void)
function test() {
return;
}

// Should indicate that a number is returned
var fn = function () {
return 1;
};

// Should indicate that a string is returned
var arrowFn = () => 'test';

class Test {
// Should indicate that no value is returned (void)
method() {
return;
}
}
Open in Playground

Options

This rule accepts the following options:

type Options = [
{
/** Whether to allow arrow functions that start with the `void` keyword. */
allowConciseArrowFunctionExpressionsStartingWithVoid?: boolean;
/** Whether to ignore arrow functions immediately returning a `as const` value. */
allowDirectConstAssertionInArrowFunctions?: boolean;
/** Whether to ignore function expressions (functions which are not part of a declaration). */
allowExpressions?: boolean;
/** Whether to ignore functions that don't have generic type parameters. */
allowFunctionsWithoutTypeParameters?: boolean;
/** Whether to ignore functions immediately returning another function expression. */
allowHigherOrderFunctions?: boolean;
/** Whether to ignore immediately invoked function expressions (IIFEs). */
allowIIFEs?: boolean;
/** Whether to ignore type annotations on the variable of function expressions. */
allowTypedFunctionExpressions?: boolean;
/** An array of function/method names that will not have their arguments or return values checked. */
allowedNames?: string[];
},
];

const defaultOptions: Options = [
{
allowConciseArrowFunctionExpressionsStartingWithVoid: false,
allowDirectConstAssertionInArrowFunctions: true,
allowedNames: [],
allowExpressions: false,
allowFunctionsWithoutTypeParameters: false,
allowHigherOrderFunctions: true,
allowIIFEs: false,
allowTypedFunctionExpressions: true,
},
];

Configuring in a mixed JS/TS codebase

If you are working on a codebase within which you lint non-TypeScript code (i.e. .js/.mjs/.cjs/.jsx), you should ensure that you should use ESLint overrides to only enable the rule on .ts/.mts/.cts/.tsx files. If you don't, then you will get unfixable lint errors reported within .js/.mjs/.cjs/.jsx files.

{
"rules": {
// disable the rule for all files
"@typescript-eslint/explicit-function-return-type": "off",
},
"overrides": [
{
// enable the rule specifically for TypeScript files
"files": ["*.ts", "*.mts", "*.cts", "*.tsx"],
"rules": {
"@typescript-eslint/explicit-function-return-type": "error",
},
},
],
}

allowExpressions

Whether to ignore function expressions (functions which are not part of a declaration). Default: false.

Examples of code for this rule with { allowExpressions: true }:

function test() {}

const fn = () => {};

export default () => {};
Open in Playground

allowTypedFunctionExpressions

Whether to ignore type annotations on the variable of function expressions. Default: true.

Examples of code for this rule with { allowTypedFunctionExpressions: true }:

let arrowFn = () => 'test';

let funcExpr = function () {
return 'test';
};

let objectProp = {
foo: () => 1,
};
Open in Playground

allowHigherOrderFunctions

Whether to ignore functions immediately returning another function expression. Default: true.

Examples of code for this rule with { allowHigherOrderFunctions: true }:

var arrowFn = () => () => {};

function fn() {
return function () {};
}
Open in Playground

allowDirectConstAssertionInArrowFunctions

Whether to ignore arrow functions immediately returning a as const value. Default: true.

Examples of code for this rule with { allowDirectConstAssertionInArrowFunctions: true }:

const func = (value: number) => ({ type: 'X', value }) as any;
const func = (value: number) => ({ type: 'X', value }) as Action;
Open in Playground

allowConciseArrowFunctionExpressionsStartingWithVoid

Whether to allow arrow functions that start with the void keyword. Default: false.

Examples of code for this rule with { allowConciseArrowFunctionExpressionsStartingWithVoid: true }:

var join = (a: string, b: string) => `${a}${b}`;

const log = (message: string) => {
console.log(message);
};
Open in Playground

allowFunctionsWithoutTypeParameters

Whether to ignore functions that don't have generic type parameters. Default: false.

Examples of code for this rule with { allowFunctionsWithoutTypeParameters: true }:

function foo<T>(t: T) {
return t;
}

const bar = <T>(t: T) => t;
Open in Playground

allowedNames

An array of function/method names that will not have their arguments or return values checked. Default: [].

You may pass function/method names you would like this rule to ignore, like so:

{
"@typescript-eslint/explicit-function-return-type": [
"error",
{
"allowedNames": ["ignoredFunctionName", "ignoredMethodName"]
}
]
}

allowIIFEs

Whether to ignore immediately invoked function expressions (IIFEs). Default: false.

Examples of code for this rule with { allowIIFEs: true }:

var func = () => 'foo';
Open in Playground

When Not To Use It

If you don't find the added cost of explicitly writing function return types to be worth the visual clarity, or your project is not large enough for it to be a factor in type checking performance, then you will not need this rule.

Further Reading

Resources