useExplicitReturnType
Summary
Section titled “Summary”- Rule available since:
v2.4.11 - Diagnostic Category:
lint/nursery/useExplicitReturnType - This rule doesn’t have a fix.
- The default severity of this rule is warning.
- Sources:
- Inspired from
@typescript-eslint/explicit-function-return-type
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useExplicitReturnType": "error" } } }}Description
Section titled “Description”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 are required when using TypeScript’s
isolatedModules and similar
single-file transpilation tools, because the compiler cannot perform
cross-file type inference. Without explicit return types, exported functions may produce
incomplete or incorrect .d.ts declarations.
This rule enforces that functions do have an explicit return type annotation.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”// Should indicate that no value is returned (void)function test() { return;}code-block.ts:2:1 lint/nursery/useExplicitReturnType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Missing return type on function.
1 │ // Should indicate that no value is returned (void)
> 2 │ function test() {
│ ^^^^^^^^^^^^^
3 │ return;
4 │ }
ℹ Declaring the return type makes the code self-documented and can speed up TypeScript type checking.
ℹ Add a return type to the function.
ℹ This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/2017 for more information or to report possible bugs.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
// Should indicate that a number is returnedvar fn = function () { return 1;};code-block.ts:2:10 lint/nursery/useExplicitReturnType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Missing return type on function.
1 │ // Should indicate that a number is returned
> 2 │ var fn = function () {
│ ^^^^^^^^^^^^^
> 3 │ return 1;
> 4 │ };
│ ^
5 │
ℹ Declaring the return type makes the code self-documented and can speed up TypeScript type checking.
ℹ Add a return type to the function.
ℹ This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/2017 for more information or to report possible bugs.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
// Should indicate that a string is returnedvar arrowFn = () => 'test';code-block.ts:2:15 lint/nursery/useExplicitReturnType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Missing return type on function.
1 │ // Should indicate that a string is returned
> 2 │ var arrowFn = () => ‘test’;
│ ^^^^^^^^^^^^
3 │
ℹ Declaring the return type makes the code self-documented and can speed up TypeScript type checking.
ℹ Add a return type to the function.
ℹ This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/2017 for more information or to report possible bugs.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
class Test { // Should indicate that no value is returned (void) method() { return; }}code-block.ts:3:3 lint/nursery/useExplicitReturnType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Missing return type on function.
1 │ class Test {
2 │ // Should indicate that no value is returned (void)
> 3 │ method() {
│ ^^^^^^^^^^
> 4 │ return;
> 5 │ }
│ ^
6 │ }
7 │
ℹ Declaring the return type makes the code self-documented and can speed up TypeScript type checking.
ℹ Add a return type to the function.
ℹ This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/2017 for more information or to report possible bugs.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
The following example is considered incorrect for a higher-order function, as the returned function does not specify a return type:
var arrowFn = () => () => {};code-block.ts:1:21 lint/nursery/useExplicitReturnType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Missing return type on function.
> 1 │ var arrowFn = () => () => {};
│ ^^^^^^^^
2 │
ℹ Declaring the return type makes the code self-documented and can speed up TypeScript type checking.
ℹ Add a return type to the function.
ℹ This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/2017 for more information or to report possible bugs.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
var arrowFn = () => { return () => { };}code-block.ts:2:10 lint/nursery/useExplicitReturnType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Missing return type on function.
1 │ var arrowFn = () => {
> 2 │ return () => { };
│ ^^^^^^^^^
3 │ }
4 │
ℹ Declaring the return type makes the code self-documented and can speed up TypeScript type checking.
ℹ Add a return type to the function.
ℹ This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/2017 for more information or to report possible bugs.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
The following example is considered incorrect because not all return statements return a function. A higher-order function is only valid when every return statement returns a function expression:
function f() { if (x) { return 0; } return (): void => {}}code-block.ts:1:1 lint/nursery/useExplicitReturnType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Missing return type on function.
> 1 │ function f() {
│ ^^^^^^^^^^
2 │ if (x) {
3 │ return 0;
ℹ Declaring the return type makes the code self-documented and can speed up TypeScript type checking.
ℹ Add a return type to the function.
ℹ This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/2017 for more information or to report possible bugs.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
// No return value should be expected (void)function test(): void { return;}// A return value of type numbervar fn = function (): number { return 1;}// A return value of type stringvar arrowFn = (): string => 'test';class Test { // No return value should be expected (void) method(): void { return; }}The following example is considered correct code for a function immediately returning a value with as const:
var func = (value: number) => ({ foo: 'bar', value }) as const;The following examples are considered correct code for a function passed as an argument:
// Callbacks without return typessetTimeout(function() { console.log("Hello!"); }, 1000);The following example is considered correct code for a higher-order function, where the returned function explicitly specifies a return type and the function body contains only one statement:
// the outer function returns an inner function that has a `void` return typevar arrowFn = () => (): void => {};// the outer function returns an inner function that has a `void` return typevar arrowFn = () => { return (): void => { };}The following examples are considered correct for type annotations on variables in function expressions:
// A function with a type assertion using `as`var asTyped = (() => '') as () => string;// A function with a type assertion using `<>`var castTyped = <() => string>(() => '');// A variable declarator with a type annotation.type FuncType = () => string;var arrowFn: FuncType = () => 'test';// A function is a default parameter with a type annotationtype CallBack = () => void;var f = (gotcha: CallBack = () => { }): void => { };// A class property with a type annotationtype MethodType = () => void;class App { private method: MethodType = () => { };}Options
Section titled “Options”allowExpressions
Section titled “allowExpressions”When set to true, only function declarations and class methods are checked.
Function expressions (assigned to variables, passed as arguments, etc.) are allowed
without return types.
Default: false
{ "linter": { "rules": { "nursery": { "useExplicitReturnType": { "options": { "allowExpressions": true } } } } }}// Callbacks and standalone expressions are allowedsetTimeout(function() { console.log("Hello!"); }, 1000);foo(() => 1);allowIifes
Section titled “allowIifes”When set to true, IIFEs (Immediately Invoked Function Expressions) are allowed
without explicit return types.
Default: false
{ "linter": { "rules": { "nursery": { "useExplicitReturnType": { "options": { "allowIifes": true } } } } }}// IIFEs are allowed(function () { return 1;})();(() => { return 1;})();allowedNames
Section titled “allowedNames”An array of function/method names that are allowed to not have explicit return types.
Default: []
{ "linter": { "rules": { "nursery": { "useExplicitReturnType": { "options": { "allowedNames": [ "myFunction" ] } } } } }}// Functions with allowed names don't need return typesfunction myFunction() { return 42;}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.