Skip to content

Commit e568892

Browse files
committed
Allow missing return for unions containing any or void
1 parent 0dc485a commit e568892

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

doc/spec.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ function f() {
265265

266266
To benefit from this inference, a programmer can use the TypeScript language service. For example, a code editor can incorporate the TypeScript language service and use the service to find the members of a string object as in the following screen shot.
267267

268-
  ![](images/image1.png)
268+
  ![](images/image1.png)
269269

270270
In this example, the programmer benefits from type inference without providing type annotations. Some beneficial tools, however, do require the programmer to provide type annotations. In TypeScript, we can express a parameter requirement as in the following code fragment.
271271

@@ -413,7 +413,7 @@ This signature denotes that a function may be passed as the parameter of the '$'
413413
414414
A typical client would not need to add any additional typing but could just use a community-supplied typing to discover (through statement completion with documentation tips) and verify (through static checking) correct use of the library, as in the following screen shot.
415415
416-
  ![](images/image2.png)
416+
  ![](images/image2.png)
417417
418418
Section [3.3](#3.3) provides additional information about object types.
419419
@@ -630,7 +630,7 @@ An important goal of TypeScript is to provide accurate and straightforward types
630630

631631
JavaScript programming interfaces often include functions whose behavior is discriminated by a string constant passed to the function. The Document Object Model makes heavy use of this pattern. For example, the following screen shot shows that the 'createElement' method of the 'document' object has multiple signatures, some of which identify the types returned when specific strings are passed into the method.
632632

633-
  ![](images/image3.png)
633+
  ![](images/image3.png)
634634

635635
The following code fragment uses this feature. Because the 'span' variable is inferred to have the type 'HTMLSpanElement', the code can reference without static error the 'isMultiline' property of 'span'.
636636

@@ -641,7 +641,7 @@ span.isMultiLine = false; // OK: HTMLSpanElement has isMultiline property
641641

642642
In the following screen shot, a programming tool combines information from overloading on string parameters with contextual typing to infer that the type of the variable 'e' is 'MouseEvent' and that therefore 'e' has a 'clientX' property.
643643

644-
  ![](images/image4.png)
644+
  ![](images/image4.png)
645645

646646
Section [3.9.2.4](#3.9.2.4) provides details on how to use string literals in function signatures.
647647

@@ -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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2466,6 +2466,12 @@ namespace ts {
24662466
return type && (type.flags & TypeFlags.Any) !== 0;
24672467
}
24682468

2469+
function isUnionContaining(type: Type, kinds: TypeFlags) {
2470+
return type
2471+
&& (type.flags & TypeFlags.Union)
2472+
&& someConstituentTypeHasKind(type, kinds);
2473+
}
2474+
24692475
// Return the type of a binding element parent. We check SymbolLinks first to see if a type has been
24702476
// assigned by contextual typing.
24712477
function getTypeForBindingElementParent(node: VariableLikeDeclaration) {
@@ -10201,7 +10207,8 @@ namespace ts {
1020110207

1020210208
/*
1020310209
*TypeScript Specification 1.0 (6.3) - July 2014
10204-
* An explicitly typed function whose return type isn't the Void or the Any type
10210+
* An explicitly typed function whose return type isn't the Void type,
10211+
* the Any type, or a union type containing the Void or Any type as a constituent
1020510212
* must have at least one return statement somewhere in its body.
1020610213
* An exception to this rule is if the function implementation consists of a single 'throw' statement.
1020710214
* @param returnType - return type of the function, can be undefined if return type is not explicitly specified
@@ -10212,7 +10219,7 @@ namespace ts {
1021210219
}
1021310220

1021410221
// Functions with with an explicitly specified 'void' or 'any' return type don't need any return expressions.
10215-
if (returnType === voidType || isTypeAny(returnType)) {
10222+
if (returnType === voidType || isTypeAny(returnType) || isUnionContaining(returnType, TypeFlags.Any) || isUnionContaining(returnType, TypeFlags.Void)) {
1021610223
return;
1021710224
}
1021810225

tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +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(93,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
32
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(101,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
43
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(106,16): error TS2378: A 'get' accessor must return a value.
54
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(129,5): error TS1003: Identifier expected.
65

76

8-
==== tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts (5 errors) ====
7+
==== tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts (4 errors) ====
98

109

1110
function f1(): string {
@@ -101,8 +100,6 @@ tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(129,5): e
101100
}
102101

103102
function f19(): void | number {
104-
~~~~~~~~~~~~~
105-
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
106103
// Okay; function return type is union containing void
107104
}
108105

0 commit comments

Comments
 (0)