useExplicitType
Ce contenu n’est pas encore disponible dans votre langue.
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.
Examples
Section titled ExamplesInvalid
Section titled Invalidcode-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.
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.
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.
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.
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.
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:
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.
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.
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.
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:
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:
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:
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:
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:
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:
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.
Valid
Section titled ValidThe following patterns are considered correct code for a function immediately returning a value with 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:
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 following patterns are considered correct for type annotations on variables in function expressions: