Skip to content

Allow implicit return with explicit undefined return type #53092

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

Merged
merged 9 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 4 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35469,8 +35469,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const functionFlags = getFunctionFlags(func);
const type = returnType && unwrapReturnType(returnType, functionFlags);

// Functions with with an explicitly specified 'void' or 'any' return type don't need any return expressions.
if (type && maybeTypeOfKind(type, TypeFlags.Any | TypeFlags.Void)) {
// Functions with an explicitly specified 'undefined, 'void' or 'any' return type don't need any return expressions.
if (type && maybeTypeOfKind(type, TypeFlags.Undefined | TypeFlags.Void | TypeFlags.Any)) {
return;
}

Expand All @@ -35489,9 +35489,9 @@ 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.
error(errorNode, Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value);
error(errorNode, Diagnostics.A_function_whose_declared_type_is_neither_undefined_void_nor_any_must_return_a_value);
}
else if (type && strictNullChecks && !isTypeAssignableTo(undefinedType, type)) {
else if (type && strictNullChecks) {
error(errorNode, Diagnostics.Function_lacks_ending_return_statement_and_return_type_does_not_include_undefined);
}
else if (compilerOptions.noImplicitReturns) {
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1868,7 +1868,7 @@
"category": "Error",
"code": 2354
},
"A function whose declared type is neither 'void' nor 'any' must return a value.": {
"A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.": {
"category": "Error",
"code": 2355
},
Expand Down Expand Up @@ -6706,7 +6706,7 @@
"category": "Suggestion",
"code": 80010
},

"Add missing 'super()' call": {
"category": "Message",
"code": 90001
Expand Down
4 changes: 2 additions & 2 deletions src/services/codefixes/returnValueCorrect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +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.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 @@ -213,7 +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:
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
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(1,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(99,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(104,16): error TS2378: A 'get' accessor must return a value.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(126,15): error TS18050: The value 'undefined' cannot be used here.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(127,5): error TS1003: Identifier expected.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(1,16): error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(99,17): error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(107,17): error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(112,16): error TS2378: A 'get' accessor must return a value.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(134,15): error TS18050: The value 'undefined' cannot be used here.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(135,5): error TS1003: Identifier expected.


==== tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts (5 errors) ====
==== tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts (6 errors) ====
function f1(): string {
~~~~~~
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
!!! error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
// errors because there are no return statements
}

Expand Down Expand Up @@ -40,12 +41,12 @@ tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(127,5): e
return null;
}

function f8(): void {
function f8(): any {
// Fine since are typed any.
return;
}

function f9(): void {
function f9(): any {
// Fine since we are typed any and return undefined
Copy link
Contributor Author

Choose a reason for hiding this comment

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

(To match the comments, as otherwise these two are exactly same with f5 and f6.)

return undefined;
}
Expand Down Expand Up @@ -108,10 +109,20 @@ tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(127,5): e

function f21(): number | string {
~~~~~~~~~~~~~~~
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
!!! error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
// Not okay; union does not contain void or any
}

function f22(): undefined {
// Okay; return type allows implicit return of undefined
}

function f23(): undefined | number {
Copy link
Member

Choose a reason for hiding this comment

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

I think your last commit only included this baseline, as f23 doesn't appear to be anywhere else.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just tapped the commit button from GitHub, will add it with the another change for the feedback.

~~~~~~~~~~~~~~~~~~
!!! error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
// Error; because `undefined | number` becomes `number` without strictNullChecks.
}

class C {
public get m1() {
~~
Expand Down Expand Up @@ -143,4 +154,5 @@ tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(127,5): e
}
~
!!! error TS1003: Identifier expected.
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ function f7(): void {
return null;
}

function f8(): void {
function f8(): any {
// Fine since are typed any.
return;
}

function f9(): void {
function f9(): any {
// Fine since we are typed any and return undefined
return undefined;
}
Expand Down Expand Up @@ -101,6 +101,14 @@ function f21(): number | string {
// Not okay; union does not contain void or any
}

function f22(): undefined {
// Okay; return type allows implicit return of undefined
}

function f23(): undefined | number {
// Error; because `undefined | number` becomes `number` without strictNullChecks.
}

class C {
public get m1() {
// Errors; get accessors must return a value.
Expand All @@ -126,7 +134,8 @@ class C {
throw null;
throw undefined.
}
}
}


//// [functionsMissingReturnStatementsAndExpressions.js]
function f1() {
Expand Down Expand Up @@ -209,6 +218,12 @@ function f20() {
function f21() {
// Not okay; union does not contain void or any
}
function f22() {
// Okay; return type allows implicit return of undefined
}
function f23() {
// Error; because `undefined | number` becomes `number` without strictNullChecks.
}
var C = /** @class */ (function () {
function C() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ function f7(): void {
return null;
}

function f8(): void {
function f8(): any {
>f8 : Symbol(f8, Decl(functionsMissingReturnStatementsAndExpressions.ts, 30, 1))

// Fine since are typed any.
return;
}

function f9(): void {
function f9(): any {
>f9 : Symbol(f9, Decl(functionsMissingReturnStatementsAndExpressions.ts, 35, 1))

// Fine since we are typed any and return undefined
Expand Down Expand Up @@ -152,37 +152,49 @@ function f21(): number | string {
// Not okay; union does not contain void or any
}

function f22(): undefined {
>f22 : Symbol(f22, Decl(functionsMissingReturnStatementsAndExpressions.ts, 100, 1))

// Okay; return type allows implicit return of undefined
}

function f23(): undefined | number {
>f23 : Symbol(f23, Decl(functionsMissingReturnStatementsAndExpressions.ts, 104, 1))

// Error; because `undefined | number` becomes `number` without strictNullChecks.
}

class C {
>C : Symbol(C, Decl(functionsMissingReturnStatementsAndExpressions.ts, 100, 1))
>C : Symbol(C, Decl(functionsMissingReturnStatementsAndExpressions.ts, 108, 1))

public get m1() {
>m1 : Symbol(C.m1, Decl(functionsMissingReturnStatementsAndExpressions.ts, 102, 9))
>m1 : Symbol(C.m1, Decl(functionsMissingReturnStatementsAndExpressions.ts, 110, 9))

// Errors; get accessors must return a value.
}

public get m2() {
>m2 : Symbol(C.m2, Decl(functionsMissingReturnStatementsAndExpressions.ts, 105, 5))
>m2 : Symbol(C.m2, Decl(functionsMissingReturnStatementsAndExpressions.ts, 113, 5))

// Permissible; returns undefined.
return;
}

public get m3() {
>m3 : Symbol(C.m3, Decl(functionsMissingReturnStatementsAndExpressions.ts, 110, 5))
>m3 : Symbol(C.m3, Decl(functionsMissingReturnStatementsAndExpressions.ts, 118, 5))

return "Okay, because this is a return expression.";
}

public get m4() {
>m4 : Symbol(C.m4, Decl(functionsMissingReturnStatementsAndExpressions.ts, 114, 5))
>m4 : Symbol(C.m4, Decl(functionsMissingReturnStatementsAndExpressions.ts, 122, 5))

// Fine since this consists of a single throw statement.
throw null;
}

public get m5() {
>m5 : Symbol(C.m5, Decl(functionsMissingReturnStatementsAndExpressions.ts, 119, 5))
>m5 : Symbol(C.m5, Decl(functionsMissingReturnStatementsAndExpressions.ts, 127, 5))

// Not fine, since we can *only* consist of a single throw statement
// if no return statements are present but we are a get accessor.
Expand All @@ -191,3 +203,4 @@ class C {
>undefined : Symbol(undefined)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ function f7(): void {
>null : null
}

function f8(): void {
>f8 : () => void
function f8(): any {
>f8 : () => any

// Fine since are typed any.
return;
}

function f9(): void {
>f9 : () => void
function f9(): any {
>f9 : () => any

// Fine since we are typed any and return undefined
return undefined;
Expand Down Expand Up @@ -159,6 +159,18 @@ function f21(): number | string {
// Not okay; union does not contain void or any
}

function f22(): undefined {
>f22 : () => undefined

// Okay; return type allows implicit return of undefined
}

function f23(): undefined | number {
>f23 : () => undefined | number

// Error; because `undefined | number` becomes `number` without strictNullChecks.
}

class C {
>C : C

Expand Down Expand Up @@ -204,3 +216,4 @@ class C {
}
> : any
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//// [functionsMissingReturnStatementsAndExpressions2.ts]
function f1(): undefined | number {
// Okay; return type allows implicit return of undefined
}


//// [functionsMissingReturnStatementsAndExpressions2.js]
function f1() {
// Okay; return type allows implicit return of undefined
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
=== tests/cases/compiler/functionsMissingReturnStatementsAndExpressions2.ts ===
function f1(): undefined | number {
>f1 : Symbol(f1, Decl(functionsMissingReturnStatementsAndExpressions2.ts, 0, 0))

// Okay; return type allows implicit return of undefined
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
=== tests/cases/compiler/functionsMissingReturnStatementsAndExpressions2.ts ===
function f1(): undefined | number {
>f1 : () => undefined | number

// Okay; return type allows implicit return of undefined
}

Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ function f7(): void {
return null;
}

function f8(): void {
function f8(): any {
// Fine since are typed any.
return;
}

function f9(): void {
function f9(): any {
// Fine since we are typed any and return undefined
return undefined;
}
Expand Down Expand Up @@ -104,6 +104,14 @@ function f21(): number | string {
// Not okay; union does not contain void or any
}

function f22(): undefined {
// Okay; return type allows implicit return of undefined
}

function f23(): undefined | number {
// Error; because `undefined | number` becomes `number` without strictNullChecks.
}

class C {
public get m1() {
// Errors; get accessors must return a value.
Expand All @@ -129,4 +137,4 @@ class C {
throw null;
throw undefined.
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @allowUnreachableCode: true
// @strictNullChecks: true

// @target: es5

function f1(): undefined | number {
// Okay; return type allows implicit return of undefined
}
4 changes: 1 addition & 3 deletions tests/cases/fourslash/codeFixCorrectReturnValue6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@
//// undefined
//// }

verify.codeFixAvailable([
{ description: 'Add a return statement' },
]);
verify.not.codeFixAvailable();