Skip to content

Allow missing return for void any union #6512

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 4 commits into from
Jan 25, 2016
Merged
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
Binary file modified doc/TypeScript Language Specification.docx
Binary file not shown.
2 changes: 1 addition & 1 deletion doc/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -3885,7 +3885,7 @@ function g(x: number) {

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.

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.
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.
Copy link
Member

Choose a reason for hiding this comment

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

You actually need to update the Word document (which is the authoritative source) and then run the word2md script to update this file.

Alternatively, just skip this change and log a spec bug to handle the change.


The type of 'this' in a function implementation is the Any type.

Expand Down
5 changes: 3 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10201,7 +10201,8 @@ namespace ts {

/*
*TypeScript Specification 1.0 (6.3) - July 2014
* An explicitly typed function whose return type isn't the Void or the Any type
* 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.
* @param returnType - return type of the function, can be undefined if return type is not explicitly specified
Expand All @@ -10212,7 +10213,7 @@ namespace ts {
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(3,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(95,16): error TS2378: A 'get' accessor must return a value.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(118,5): error TS1003: Identifier expected.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(101,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(106,16): error TS2378: A 'get' accessor must return a value.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(129,5): error TS1003: Identifier expected.


==== tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts (3 errors) ====
==== tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts (4 errors) ====


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

function f19(): void | number {
// Okay; function return type is union containing void
}

function f20(): any | number {
// Okay; function return type is union containing any
}

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

class C {
public get m1() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ function f18() {
return "Okay, not type annotated.";
}

function f19(): void | number {
// Okay; function return type is union containing void
}

function f20(): any | number {
// Okay; function return type is union containing any
}

function f21(): number | string {
// Not okay; union does not contain void or any
}

class C {
public get m1() {
Expand Down Expand Up @@ -191,6 +202,15 @@ function f17() {
function f18() {
return "Okay, not type annotated.";
}
function f19() {
// Okay; function return type is union containing void
}
function f20() {
// Okay; function return type is union containing any
}
function f21() {
// Not okay; union does not contain void or any
}
var C = (function () {
function C() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ function f18() {
return "Okay, not type annotated.";
}

function f19(): void | number {
// Okay; function return type is union containing void
}

function f20(): any | number {
// Okay; function return type is union containing any
}

function f21(): number | string {
// Not okay; union does not contain void or any
}

class C {
public get m1() {
Expand Down