跳转到内容

useExplicitType

此内容尚不支持你的语言。

Diagnostic Category: lint/nursery/useExplicitType

Since: v1.9.3

Sources:

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. Explicit return types also reduce the chance of bugs by asserting the return type, and it avoids surprising “action at a distance,” where changing the body of one function may cause failures inside another function.

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

// Should indicate that no value is returned (void)
function test() {
return;
}
code-block.ts:1:1 lint/nursery/useExplicitType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

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-documenting and can speed up TypeScript type checking.

Add a return type annotation.

// Should indicate that a number is returned
var fn = function () {
return 1;
};
code-block.ts:2:10 lint/nursery/useExplicitType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

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-documenting and can speed up TypeScript type checking.

Add a return type annotation.

// Should indicate that a string is returned
var arrowFn = () => 'test';
code-block.ts:2:15 lint/nursery/useExplicitType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

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-documenting and can speed up TypeScript type checking.

Add a return type annotation.

class Test {
// Should indicate that no value is returned (void)
method() {
return;
}
}
code-block.ts:3:3 lint/nursery/useExplicitType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

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-documenting and can speed up TypeScript type checking.

Add a return type annotation.

// Should indicate that no value is returned (void)
function test(a: number) {
a += 1;
}
code-block.ts:1:1 lint/nursery/useExplicitType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Missing return type on function.

> 1 │ // Should indicate that no value is returned (void)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 2 │ function test(a: number) {
^^^^^^^^^^^^^
3 │ a += 1;
4 │ }

Declaring the return type makes the code self-documenting and can speed up TypeScript type checking.

Add a return type annotation.

// Should use const assertions
const func = (value: number) => ({ type: 'X', value }) as any;
code-block.ts:2:14 lint/nursery/useExplicitType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Missing return type on function.

1 │ // Should use const assertions
> 2 │ const func = (value: number) => ({ type: ‘X’, value }) as any;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │

Declaring the return type makes the code self-documenting and can speed up TypeScript type checking.

Add a return type annotation.

The following pattern is considered incorrect code for a higher-order function, as the returned function does not specify a return type:

const arrowFn = () => () => {};
code-block.ts:1:23 lint/nursery/useExplicitType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Missing return type on function.

> 1 │ const arrowFn = () => () => {};
^^^^^^^^
2 │

Declaring the return type makes the code self-documenting and can speed up TypeScript type checking.

Add a return type annotation.

const arrowFn = () => {
return () => { };
}
code-block.ts:2:10 lint/nursery/useExplicitType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Missing return type on function.

1 │ const arrowFn = () => {
> 2 │ return () => { };
^^^^^^^^^
3 │ }
4 │

Declaring the return type makes the code self-documenting and can speed up TypeScript type checking.

Add a return type annotation.

The following pattern is considered incorrect code for a higher-order function because the function body contains multiple statements. We only check whether the first statement is a function return.

// A function has multiple statements in the body
function f() {
if (x) {
return 0;
}
return (): void => {}
}
code-block.ts:1:1 lint/nursery/useExplicitType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Missing return type on function.

> 1 │ // A function has multiple statements in the body
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 2 │ function f() {
^^^^^^^^^^
3 │ if (x) {
4 │ return 0;

Declaring the return type makes the code self-documenting and can speed up TypeScript type checking.

Add a return type annotation.

// A function has multiple statements in the body
function f() {
let str = "test";
return (): string => {
str;
}
}
code-block.ts:1:1 lint/nursery/useExplicitType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Missing return type on function.

> 1 │ // A function has multiple statements in the body
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 2 │ function f() {
^^^^^^^^^^
3 │ let str = “test”;
4 │ return (): string => {

Declaring the return type makes the code self-documenting and can speed up TypeScript type checking.

Add a return type annotation.

The following pattern is considered incorrect code for an interface method without a return type:

interface Array<Type> {
method();
}
code-block.ts:2:3 lint/nursery/useExplicitType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Missing return type on function.

1 │ interface Array<Type> {
> 2 │ method();
^^^^^^^^^
3 │ }
4 │

Declaring the return type makes the code self-documenting and can speed up TypeScript type checking.

Add a return type annotation.

The following pattern is considered incorrect code for a type declaration of a function without a return type:

type MyObject = {
(input: string);
propertyName: string;
};
code-block.ts:2:3 lint/nursery/useExplicitType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Missing return type on function.

1 │ type MyObject = {
> 2 │ (input: string);
^^^^^^^^^^^^^^^^
3 │ propertyName: string;
4 │ };

Declaring the return type makes the code self-documenting and can speed up TypeScript type checking.

Add a return type annotation.

The following pattern is considered incorrect code for an abstract class method without a return type:

abstract class MyClass {
public abstract method();
}
code-block.ts:2:3 lint/nursery/useExplicitType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Missing return type on function.

1 │ abstract class MyClass {
> 2 │ public abstract method();
^^^^^^^^^^^^^^^^^^^^^^^^^
3 │ }
4 │

Declaring the return type makes the code self-documenting and can speed up TypeScript type checking.

Add a return type annotation.

The following pattern is considered incorrect code for an abstract class getter without a return type:

abstract class P<T> {
abstract get poke();
}
code-block.ts:2:3 lint/nursery/useExplicitType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Missing return type on function.

1 │ abstract class P<T> {
> 2 │ abstract get poke();
^^^^^^^^^^^^^^^^^^^^
3 │ }
4 │

Declaring the return type makes the code self-documenting and can speed up TypeScript type checking.

Add a return type annotation.

The following pattern is considered incorrect code for a function declaration in a namespace without a return type:

declare namespace myLib {
function makeGreeting(s: string);
}
code-block.ts:2:3 lint/nursery/useExplicitType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Missing return type on function.

1 │ declare namespace myLib {
> 2 │ function makeGreeting(s: string);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │ }
4 │

Declaring the return type makes the code self-documenting and can speed up TypeScript type checking.

Add a return type annotation.

The following pattern is considered incorrect code for a module function export without a return type:

declare module "foo" {
export default function bar();
}
code-block.ts:2:18 lint/nursery/useExplicitType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Missing return type on function.

1 │ declare module “foo” {
> 2 │ export default function bar();
^^^^^^^^^^^^^^^
3 │ }
4 │

Declaring the return type makes the code self-documenting and can speed up TypeScript type checking.

Add a return type annotation.

// No return value should be expected (void)
function test(): void {
return;
}
// A return value of type number
var fn = function (): number {
return 1;
}
// A return value of type string
var arrowFn = (): string => 'test';
class Test {
// No return value should be expected (void)
method(): void {
return;
}
}

The following patterns are considered correct code for a function immediately returning a value with as const:

const func = (value: number) => ({ foo: 'bar', value }) as const;

The following patterns are considered correct code for a function allowed within specific expression contexts, such as an IIFE, a function passed as an argument, or a function inside an array:

// Callbacks without return types
setTimeout(function() { console.log("Hello!"); }, 1000);
// IIFE
(() => {})();
// a function inside an array
[function () {}, () => {}];

The following pattern 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 type
const arrowFn = () => (): void => {};
// the outer function returns an inner function that has a `void` return type
const arrowFn = () => {
return (): void => { };
}

The following patterns are considered correct for type annotations on variables in function expressions:

// A function with a type assertion using `as`
const asTyped = (() => '') as () => string;
// A function with a type assertion using `<>`
const castTyped = <() => string>(() => '');
// A variable declarator with a type annotation.
type FuncType = () => string;
const arrowFn: FuncType = () => 'test';
// A function is a default parameter with a type annotation
type CallBack = () => void;
const f = (gotcha: CallBack = () => { }): void => { };
// A class property with a type annotation
type MethodType = () => void;
class App {
private method: MethodType = () => { };
}