Skip to content

Commit 85d09e4

Browse files
committed
Merge pull request #6512 from masaeedu/allowMissingReturnForVoidAnyUnion
Allow missing return for void any union
2 parents 6ff1bbe + 2884086 commit 85d09e4

File tree

6 files changed

+52
-6
lines changed

6 files changed

+52
-6
lines changed
564 Bytes
Binary file not shown.

doc/spec.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3885,7 +3885,7 @@ function g(x: number) {
38853885
38863886
the inferred return type for 'f' and 'g' is Any because the functions reference themselves through a cycle with no return type annotations. Adding an explicit return type 'number' to either breaks the cycle and causes the return type 'number' to be inferred for the other.
38873887
3888-
An explicitly typed function whose return type isn't the Void or the Any type must have at least one return statement somewhere in its body. An exception to this rule is if the function implementation consists of a single 'throw' statement.
3888+
An explicitly typed function whose return type isn't the Void type, the Any type, or a union type containing the Void or Any type as a constituent must have at least one return statement somewhere in its body. An exception to this rule is if the function implementation consists of a single 'throw' statement.
38893889
38903890
The type of 'this' in a function implementation is the Any type.
38913891

src/compiler/checker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10404,7 +10404,8 @@ namespace ts {
1040410404

1040510405
/*
1040610406
*TypeScript Specification 1.0 (6.3) - July 2014
10407-
* An explicitly typed function whose return type isn't the Void or the Any type
10407+
* An explicitly typed function whose return type isn't the Void type,
10408+
* the Any type, or a union type containing the Void or Any type as a constituent
1040810409
* must have at least one return statement somewhere in its body.
1040910410
* An exception to this rule is if the function implementation consists of a single 'throw' statement.
1041010411
* @param returnType - return type of the function, can be undefined if return type is not explicitly specified
@@ -10415,7 +10416,7 @@ namespace ts {
1041510416
}
1041610417

1041710418
// Functions with with an explicitly specified 'void' or 'any' return type don't need any return expressions.
10418-
if (returnType === voidType || isTypeAny(returnType)) {
10419+
if (returnType === voidType || isTypeAny(returnType) || (returnType && (returnType.flags & TypeFlags.Union) && someConstituentTypeHasKind(returnType, TypeFlags.Any | TypeFlags.Void))) {
1041910420
return;
1042010421
}
1042110422

tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.errors.txt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(3,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
2-
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(95,16): error TS2378: A 'get' accessor must return a value.
3-
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(118,5): error TS1003: Identifier expected.
2+
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(101,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
3+
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(106,16): error TS2378: A 'get' accessor must return a value.
4+
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(129,5): error TS1003: Identifier expected.
45

56

6-
==== tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts (3 errors) ====
7+
==== tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts (4 errors) ====
78

89

910
function f1(): string {
@@ -98,6 +99,19 @@ tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(118,5): e
9899
return "Okay, not type annotated.";
99100
}
100101

102+
function f19(): void | number {
103+
// Okay; function return type is union containing void
104+
}
105+
106+
function f20(): any | number {
107+
// Okay; function return type is union containing any
108+
}
109+
110+
function f21(): number | string {
111+
~~~~~~~~~~~~~~~
112+
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
113+
// Not okay; union does not contain void or any
114+
}
101115

102116
class C {
103117
public get m1() {

tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ function f18() {
9191
return "Okay, not type annotated.";
9292
}
9393

94+
function f19(): void | number {
95+
// Okay; function return type is union containing void
96+
}
97+
98+
function f20(): any | number {
99+
// Okay; function return type is union containing any
100+
}
101+
102+
function f21(): number | string {
103+
// Not okay; union does not contain void or any
104+
}
94105

95106
class C {
96107
public get m1() {
@@ -191,6 +202,15 @@ function f17() {
191202
function f18() {
192203
return "Okay, not type annotated.";
193204
}
205+
function f19() {
206+
// Okay; function return type is union containing void
207+
}
208+
function f20() {
209+
// Okay; function return type is union containing any
210+
}
211+
function f21() {
212+
// Not okay; union does not contain void or any
213+
}
194214
var C = (function () {
195215
function C() {
196216
}

tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ function f18() {
9292
return "Okay, not type annotated.";
9393
}
9494

95+
function f19(): void | number {
96+
// Okay; function return type is union containing void
97+
}
98+
99+
function f20(): any | number {
100+
// Okay; function return type is union containing any
101+
}
102+
103+
function f21(): number | string {
104+
// Not okay; union does not contain void or any
105+
}
95106

96107
class C {
97108
public get m1() {

0 commit comments

Comments
 (0)