-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Changes from all commits
3bee8eb
17b43e5
b267f96
e38cd2d
03c6cb5
ec64119
1eed970
168fa04
5632662
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
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(107,17): error TS2355: A function whose declared type is neither '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. | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
@@ -112,6 +113,16 @@ 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 | ||
} | ||
|
||
function f23(): undefined | number { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 'void' nor 'any' must return a value. | ||
// Error; because `undefined | number` becomes `number` without strictNullChecks. | ||
} | ||
|
||
class C { | ||
public get m1() { | ||
~~ | ||
|
@@ -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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts(5,16): error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. | ||
tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts(13,22): error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. | ||
|
||
|
||
==== tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts (2 errors) ==== | ||
function f1(): undefined | number { | ||
// Okay; return type allows implicit return of undefined | ||
} | ||
|
||
function f2(): number { | ||
~~~~~~ | ||
!!! error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. | ||
// Error; return type does not include undefined | ||
} | ||
|
||
async function f3(): Promise<undefined | number> { | ||
// Okay; return type allows implicit return of undefined | ||
} | ||
|
||
async function f4(): Promise<number> { | ||
~~~~~~~~~~~~~~~ | ||
!!! error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. | ||
// Error; return type does not include undefined | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//// [functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts] | ||
function f1(): undefined | number { | ||
// Okay; return type allows implicit return of undefined | ||
} | ||
|
||
function f2(): number { | ||
// Error; return type does not include undefined | ||
} | ||
|
||
async function f3(): Promise<undefined | number> { | ||
// Okay; return type allows implicit return of undefined | ||
} | ||
|
||
async function f4(): Promise<number> { | ||
// Error; return type does not include undefined | ||
} | ||
|
||
|
||
//// [functionsMissingReturnStatementsAndExpressionsStrictNullChecks.js] | ||
function f1() { | ||
// Okay; return type allows implicit return of undefined | ||
} | ||
function f2() { | ||
// Error; return type does not include undefined | ||
} | ||
async function f3() { | ||
// Okay; return type allows implicit return of undefined | ||
} | ||
async function f4() { | ||
// Error; return type does not include undefined | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jakebailey @DanielRosenwasser The removal of this assignability check has caused a regression. This example now errors when it shouldn't:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed the regression here: #53515 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Funny. We were just talking about adding this check back again in #53490
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll look into why it's not working as we expected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, no, that PR was exactly to fix the issue you mentioned. So it should be fixed in main now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checked, and it is fixed, yep.