Skip to content

Don't mention all the possible return types that permit no return statements #53560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35664,7 +35664,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const functionFlags = getFunctionFlags(func);
const type = returnType && unwrapReturnType(returnType, functionFlags);

// Functions with an explicitly specified 'undefined, 'void', 'any' or 'unknown' return type don't need any return expressions.
// Functions with an explicitly specified 'undefined, 'void', 'any', or 'unknown' return type don't need any return expressions.
if (type && maybeTypeOfKind(type, TypeFlags.Undefined | TypeFlags.Void | TypeFlags.Any | TypeFlags.Unknown)) {
return;
}
Expand All @@ -35684,12 +35684,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
else if (type && !hasExplicitReturn) {
// minimal check: function has syntactic return type annotation and no explicit return statements in the body
// this function does not conform to the specification.
if (strictNullChecks) {
error(errorNode, Diagnostics.A_function_whose_declared_type_is_neither_undefined_void_nor_any_must_return_a_value);
}
else {
error(errorNode, Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value);
}
error(errorNode, Diagnostics.A_function_with_this_return_type_needs_to_explicitly_return_a_value);
}
else if (type && strictNullChecks) {
error(errorNode, Diagnostics.Function_lacks_ending_return_statement_and_return_type_does_not_include_undefined);
Expand Down
6 changes: 1 addition & 5 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1880,10 +1880,6 @@
"category": "Error",
"code": 2354
},
"A function whose declared type is neither 'void' nor 'any' must return a value.": {
"category": "Error",
"code": 2355
},
"An arithmetic operand must be of type 'any', 'number', 'bigint' or an enum type.": {
"category": "Error",
"code": 2356
Expand Down Expand Up @@ -3619,7 +3615,7 @@
"category": "Error",
"code": 2846
},
"A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.": {
"A function with this return type needs to explicitly return a value.": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"must explicitly"? (Not sure what form we use most often)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's pretend we're starting from scratch. What would you prefer to see as a user?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this diagnostic, but I might be a bit confused when the return type is inferred. That's unrelated to the message per se though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do something like A function with return type 'X' needs to explicitly return a value.? I think this would help with Maria's point about when the return type is inferred.

"category": "Error",
"code": 2847
},
Expand Down
6 changes: 2 additions & 4 deletions src/services/codefixes/returnValueCorrect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ const fixIdAddReturnStatement = "fixAddReturnStatement";
const fixRemoveBracesFromArrowFunctionBody = "fixRemoveBracesFromArrowFunctionBody";
const fixIdWrapTheBlockWithParen = "fixWrapTheBlockWithParen";
const errorCodes = [
Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value.code,
Diagnostics.A_function_whose_declared_type_is_neither_undefined_void_nor_any_must_return_a_value.code,
Diagnostics.A_function_with_this_return_type_needs_to_explicitly_return_a_value.code,
Diagnostics.Type_0_is_not_assignable_to_type_1.code,
Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1.code
];
Expand Down Expand Up @@ -214,8 +213,7 @@ function getInfo(checker: TypeChecker, sourceFile: SourceFile, position: number,

const declaration = findAncestor(node.parent, isFunctionLikeDeclaration);
switch (errorCode) {
case Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value.code:
case Diagnostics.A_function_whose_declared_type_is_neither_undefined_void_nor_any_must_return_a_value.code:
case Diagnostics.A_function_with_this_return_type_needs_to_explicitly_return_a_value.code:
if (!declaration || !declaration.body || !declaration.type || !rangeContainsRange(declaration.type, node)) return undefined;
return getFixInfo(checker, declaration, checker.getTypeFromTypeNode(declaration.type), /*isFunctionType*/ false);
case Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1.code:
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/ParameterList5.errors.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
tests/cases/compiler/ParameterList5.ts(1,15): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/compiler/ParameterList5.ts(1,15): error TS2847: A function with this return type needs to explicitly return a value.
tests/cases/compiler/ParameterList5.ts(1,16): error TS2369: A parameter property is only allowed in a constructor implementation.
tests/cases/compiler/ParameterList5.ts(1,29): error TS2304: Cannot find name 'C'.


==== tests/cases/compiler/ParameterList5.ts (3 errors) ====
function A(): (public B) => C {
~~~~~~~~~~~~~~~
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
!!! error TS2847: A function with this return type needs to explicitly return a value.
~~~~~~~~
!!! error TS2369: A parameter property is only allowed in a constructor implementation.
~
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(6,23): error TS1055: Type '{}' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(6,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(6,23): error TS2847: A function with this return type needs to explicitly return a value.
tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(7,23): error TS1055: Type 'any' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(8,23): error TS1055: Type 'number' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(8,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(8,23): error TS2847: A function with this return type needs to explicitly return a value.
tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(9,23): error TS1055: Type 'PromiseLike' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(10,23): error TS1055: Type 'typeof Thenable' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
Construct signature return types 'Thenable' and 'PromiseLike<T>' are incompatible.
Expand All @@ -22,15 +22,15 @@ tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration1
~~~
!!! error TS1055: Type '{}' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
~~~
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
!!! error TS2847: A function with this return type needs to explicitly return a value.
async function fn3(): any { } // error
~~~
!!! error TS1055: Type 'any' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
async function fn4(): number { } // error
~~~~~~
!!! error TS1055: Type 'number' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
~~~~~~
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
!!! error TS2847: A function with this return type needs to explicitly return a value.
async function fn5(): PromiseLike<void> { } // error
~~~~~~~~~~~~~~~~~
!!! error TS1055: Type 'PromiseLike' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(6,23): error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<{}>'?
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(6,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(6,23): error TS2847: A function with this return type needs to explicitly return a value.
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(7,23): error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<any>'?
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(8,23): error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<number>'?
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(8,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(8,23): error TS2847: A function with this return type needs to explicitly return a value.
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(9,23): error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<void>'?
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(10,23): error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<void>'?
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(17,16): error TS1058: The return type of an async function must either be a valid promise or must not contain a callable 'then' member.
Expand All @@ -19,15 +19,15 @@ tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration1
~~~
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<{}>'?
~~~
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
!!! error TS2847: A function with this return type needs to explicitly return a value.
async function fn3(): any { } // error
~~~
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<any>'?
async function fn4(): number { } // error
~~~~~~
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<number>'?
~~~~~~
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
!!! error TS2847: A function with this return type needs to explicitly return a value.
async function fn5(): PromiseLike<void> { } // error
~~~~~~~~~~~~~~~~~
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<void>'?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
tests/cases/compiler/conflictingTypeAnnotatedVar.ts(1,5): error TS2300: Duplicate identifier 'foo'.
tests/cases/compiler/conflictingTypeAnnotatedVar.ts(2,10): error TS2300: Duplicate identifier 'foo'.
tests/cases/compiler/conflictingTypeAnnotatedVar.ts(2,10): error TS2393: Duplicate function implementation.
tests/cases/compiler/conflictingTypeAnnotatedVar.ts(2,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/compiler/conflictingTypeAnnotatedVar.ts(2,17): error TS2847: A function with this return type needs to explicitly return a value.
tests/cases/compiler/conflictingTypeAnnotatedVar.ts(3,10): error TS2300: Duplicate identifier 'foo'.
tests/cases/compiler/conflictingTypeAnnotatedVar.ts(3,10): error TS2393: Duplicate function implementation.
tests/cases/compiler/conflictingTypeAnnotatedVar.ts(3,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/compiler/conflictingTypeAnnotatedVar.ts(3,17): error TS2847: A function with this return type needs to explicitly return a value.


==== tests/cases/compiler/conflictingTypeAnnotatedVar.ts (7 errors) ====
Expand All @@ -17,11 +17,11 @@ tests/cases/compiler/conflictingTypeAnnotatedVar.ts(3,17): error TS2355: A funct
~~~
!!! error TS2393: Duplicate function implementation.
~~~~~~
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
!!! error TS2847: A function with this return type needs to explicitly return a value.
function foo(): number { }
~~~
!!! error TS2300: Duplicate identifier 'foo'.
~~~
!!! error TS2393: Duplicate function implementation.
~~~~~~
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
!!! error TS2847: A function with this return type needs to explicitly return a value.
4 changes: 2 additions & 2 deletions tests/baselines/reference/controlFlowGenericTypes.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(49,15): error TS2
tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(55,15): error TS2345: Argument of type 'undefined' is not assignable to parameter of type 'Box<unknown>'.
tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(81,11): error TS2339: Property 'foo' does not exist on type 'MyUnion'.
Property 'foo' does not exist on type 'AA'.
tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(90,44): error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(90,44): error TS2847: A function with this return type needs to explicitly return a value.
tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(91,11): error TS2339: Property 'foo' does not exist on type 'MyUnion'.
Property 'foo' does not exist on type 'AA'.
tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(156,16): error TS18048: 'obj' is possibly 'undefined'.
Expand Down Expand Up @@ -109,7 +109,7 @@ tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(168,9): error TS1

const fn2 = <T extends MyUnion>(value: T): MyUnion => {
~~~~~~~
!!! error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
!!! error TS2847: A function with this return type needs to explicitly return a value.
value.foo; // Error
~~~
!!! error TS2339: Property 'foo' does not exist on type 'MyUnion'.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tests/cases/compiler/errorOnContextuallyTypedReturnType.ts(1,5): error TS2322: Type '() => void' is not assignable to type '() => boolean'.
Type 'void' is not assignable to type 'boolean'.
tests/cases/compiler/errorOnContextuallyTypedReturnType.ts(2,37): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/compiler/errorOnContextuallyTypedReturnType.ts(2,37): error TS2847: A function with this return type needs to explicitly return a value.


==== tests/cases/compiler/errorOnContextuallyTypedReturnType.ts (2 errors) ====
Expand All @@ -10,5 +10,5 @@ tests/cases/compiler/errorOnContextuallyTypedReturnType.ts(2,37): error TS2355:
!!! error TS2322: Type 'void' is not assignable to type 'boolean'.
var n2: () => boolean = function ():boolean { }; // expect an error here
~~~~~~~
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
!!! error TS2847: A function with this return type needs to explicitly return a value.

12 changes: 6 additions & 6 deletions tests/baselines/reference/errorOnFunctionReturnType.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tests/cases/conformance/jsdoc/foo.js(7,10): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/conformance/jsdoc/foo.js(7,10): error TS2847: A function with this return type needs to explicitly return a value.
tests/cases/conformance/jsdoc/foo.js(13,5): error TS2322: Type 'string' is not assignable to type 'number'.
tests/cases/conformance/jsdoc/foo.js(16,60): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/conformance/jsdoc/foo.js(21,20): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/conformance/jsdoc/foo.js(16,60): error TS2847: A function with this return type needs to explicitly return a value.
tests/cases/conformance/jsdoc/foo.js(21,20): error TS2847: A function with this return type needs to explicitly return a value.
tests/cases/conformance/jsdoc/foo.js(31,10): error TS2534: A function returning 'never' cannot have a reachable end point.
tests/cases/conformance/jsdoc/foo.js(37,5): error TS2322: Type 'string' is not assignable to type 'never'.
tests/cases/conformance/jsdoc/foo.js(40,56): error TS2534: A function returning 'never' cannot have a reachable end point.
Expand All @@ -17,7 +17,7 @@ tests/cases/conformance/jsdoc/foo.js(45,18): error TS2534: A function returning
/** @type {FunctionReturningPromise} */
function testPromise1() {
~~~~~~~~~~~~
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
!!! error TS2847: A function with this return type needs to explicitly return a value.
console.log("Nope");
}

Expand All @@ -30,14 +30,14 @@ tests/cases/conformance/jsdoc/foo.js(45,18): error TS2534: A function returning

var testPromise3 = /** @type {FunctionReturningPromise} */ function() {
~~~~~~~~
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
!!! error TS2847: A function with this return type needs to explicitly return a value.
console.log("test")
}

/** @type {FunctionReturningPromise} */
var testPromise4 = function() {
~~~~~~~~
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
!!! error TS2847: A function with this return type needs to explicitly return a value.
console.log("test")
}

Expand Down
Loading