typescript/explicit-function-return-type Restriction
What it does
This rule enforces that functions have an explicit return type annotation.
Why is this bad?
Explicit return types make it clearer what type is returned by a function. Making the type returned by a function obvious allows the reader to infer what the function does and how it can be used from a quick glance.
Another benefit of explicit return types is the potential for a speed up of type checking in large codebases with many large functions.
Examples
Examples of incorrect code for this rule:
ts
// 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;
}
}
Examples of correct code for this rule:
ts
// 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;
}
}
How to use
To enable this rule in the CLI or using the config file, you can use:
bash
oxlint --deny typescript/explicit-function-return-type
json
{
"rules": {
"typescript/explicit-function-return-type": "error"
}
}