Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 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 Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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(108,16): error TS2378: A 'get' accessor must return a value.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(130,15): error TS18050: The value 'undefined' cannot be used here.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(131,5): error TS1003: Identifier expected.


==== tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts (5 errors) ====
Expand Down Expand Up @@ -40,12 +40,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
Copy Markdown
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 @@ -112,6 +112,14 @@ tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(127,5): e
// Not okay; union does not contain void or any
}

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

Comment thread
saschanaz marked this conversation as resolved.
function f23(): undefined | number {
Copy link
Copy Markdown
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
Copy Markdown
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.

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

class C {
public get m1() {
~~
Expand Down Expand Up @@ -143,4 +151,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,10 @@ function f21(): number | string {
// Not okay; union does not contain void or any
}

function f22(): undefined {
// Okay; implicitly returns undefined
}

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


//// [functionsMissingReturnStatementsAndExpressions.js]
function f1() {
Expand Down Expand Up @@ -209,6 +214,9 @@ function f20() {
function f21() {
// Not okay; union does not contain void or any
}
function f22() {
// Okay; implicitly returns undefined
}
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,43 @@ 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; implicitly returns undefined
}

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

public get m1() {
>m1 : Symbol(C.m1, Decl(functionsMissingReturnStatementsAndExpressions.ts, 102, 9))
>m1 : Symbol(C.m1, Decl(functionsMissingReturnStatementsAndExpressions.ts, 106, 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, 109, 5))

// Permissible; returns undefined.
return;
}

public get m3() {
>m3 : Symbol(C.m3, Decl(functionsMissingReturnStatementsAndExpressions.ts, 110, 5))
>m3 : Symbol(C.m3, Decl(functionsMissingReturnStatementsAndExpressions.ts, 114, 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, 118, 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, 123, 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 +197,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,12 @@ function f21(): number | string {
// Not okay; union does not contain void or any
}

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

// Okay; implicitly returns undefined
}

class C {
>C : C

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

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,10 @@ function f21(): number | string {
// Not okay; union does not contain void or any
}

function f22(): undefined {
// Okay; implicitly returns undefined
}

class C {
public get m1() {
// Errors; get accessors must return a value.
Expand All @@ -129,4 +133,4 @@ class C {
throw null;
throw 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();