-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Don't error when function has an implicit return but its return type is assignable to undefined
#53490
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
MariaSolOs
merged 10 commits into
microsoft:main
from
MariaSolOs:undef-return-exceptions
Mar 27, 2023
Merged
Don't error when function has an implicit return but its return type is assignable to undefined
#53490
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
535e098
Re-add isTypeAssignableTo check
7e97af8
Add tests
1c2c3fa
Combine tests in a single file
250b65c
Remove old baselines
df2ab56
Add type alias case
f497490
Daniel was right
2f36455
Keeping the other check just to be safe
769251f
Update comment
b10847a
Revert "Keeping the other check just to be safe"
6956159
Update baseline
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...reference/functionsWithImplicitReturnTypeAssignableToUndefined(strictnullchecks=false).js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
//// [functionsWithImplicitReturnTypeAssignableToUndefined.ts] | ||
function f1(): unknown { | ||
if (Math.random() < 0.5) return true; | ||
|
||
// Implicit return, but undefined is always assignable to unknown. | ||
} | ||
|
||
type MyUnknown = unknown; | ||
function f2(): unknown { | ||
if (Math.random() < 0.5) return true; | ||
|
||
// Implicit return, but undefined is always assignable to unknown. | ||
} | ||
|
||
function f3(): any { | ||
// Implicit return, but undefined is always assignable to any. | ||
} | ||
|
||
function f4(): void { | ||
// Implicit return, but undefined is always assignable to void. | ||
} | ||
|
||
function f5(): {} { | ||
if (Math.random() < 0.5) return {}; | ||
|
||
// Implicit return, but undefined is assignable to object when strictNullChecks is off. | ||
} | ||
|
||
function f6(): Record<string, any> { | ||
if (Math.random() < 0.5) return { "foo": true }; | ||
|
||
// Implicit return, but undefined is assignable to records (which are just fancy objects) | ||
// when strictNullChecks is off. | ||
} | ||
|
||
function f7(): null { | ||
if (Math.random() < 0.5) return null; | ||
|
||
// Implicit return, but undefined is assignable to null when strictNullChecks is off. | ||
} | ||
|
||
function f8(): string | null { | ||
if (Math.random() < 0.5) return "foo"; | ||
|
||
// Implicit return, but undefined is assignable to null when strictNullChecks is off. | ||
} | ||
|
||
//// [functionsWithImplicitReturnTypeAssignableToUndefined.js] | ||
function f1() { | ||
if (Math.random() < 0.5) | ||
return true; | ||
// Implicit return, but undefined is always assignable to unknown. | ||
} | ||
function f2() { | ||
if (Math.random() < 0.5) | ||
return true; | ||
// Implicit return, but undefined is always assignable to unknown. | ||
} | ||
function f3() { | ||
// Implicit return, but undefined is always assignable to any. | ||
} | ||
function f4() { | ||
// Implicit return, but undefined is always assignable to void. | ||
} | ||
function f5() { | ||
if (Math.random() < 0.5) | ||
return {}; | ||
// Implicit return, but undefined is assignable to object when strictNullChecks is off. | ||
} | ||
function f6() { | ||
if (Math.random() < 0.5) | ||
return { "foo": true }; | ||
// Implicit return, but undefined is assignable to records (which are just fancy objects) | ||
// when strictNullChecks is off. | ||
} | ||
function f7() { | ||
if (Math.random() < 0.5) | ||
return null; | ||
// Implicit return, but undefined is assignable to null when strictNullChecks is off. | ||
} | ||
function f8() { | ||
if (Math.random() < 0.5) | ||
return "foo"; | ||
// Implicit return, but undefined is assignable to null when strictNullChecks is off. | ||
} |
84 changes: 84 additions & 0 deletions
84
...ence/functionsWithImplicitReturnTypeAssignableToUndefined(strictnullchecks=false).symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
=== tests/cases/compiler/functionsWithImplicitReturnTypeAssignableToUndefined.ts === | ||
function f1(): unknown { | ||
>f1 : Symbol(f1, Decl(functionsWithImplicitReturnTypeAssignableToUndefined.ts, 0, 0)) | ||
|
||
if (Math.random() < 0.5) return true; | ||
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) | ||
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) | ||
|
||
// Implicit return, but undefined is always assignable to unknown. | ||
} | ||
|
||
type MyUnknown = unknown; | ||
>MyUnknown : Symbol(MyUnknown, Decl(functionsWithImplicitReturnTypeAssignableToUndefined.ts, 4, 1)) | ||
|
||
function f2(): unknown { | ||
>f2 : Symbol(f2, Decl(functionsWithImplicitReturnTypeAssignableToUndefined.ts, 6, 25)) | ||
|
||
if (Math.random() < 0.5) return true; | ||
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) | ||
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) | ||
|
||
// Implicit return, but undefined is always assignable to unknown. | ||
} | ||
|
||
function f3(): any { | ||
>f3 : Symbol(f3, Decl(functionsWithImplicitReturnTypeAssignableToUndefined.ts, 11, 1)) | ||
|
||
// Implicit return, but undefined is always assignable to any. | ||
} | ||
|
||
function f4(): void { | ||
>f4 : Symbol(f4, Decl(functionsWithImplicitReturnTypeAssignableToUndefined.ts, 15, 1)) | ||
|
||
// Implicit return, but undefined is always assignable to void. | ||
} | ||
|
||
function f5(): {} { | ||
>f5 : Symbol(f5, Decl(functionsWithImplicitReturnTypeAssignableToUndefined.ts, 19, 1)) | ||
|
||
if (Math.random() < 0.5) return {}; | ||
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) | ||
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) | ||
|
||
// Implicit return, but undefined is assignable to object when strictNullChecks is off. | ||
} | ||
|
||
function f6(): Record<string, any> { | ||
>f6 : Symbol(f6, Decl(functionsWithImplicitReturnTypeAssignableToUndefined.ts, 25, 1)) | ||
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
|
||
if (Math.random() < 0.5) return { "foo": true }; | ||
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) | ||
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) | ||
>"foo" : Symbol("foo", Decl(functionsWithImplicitReturnTypeAssignableToUndefined.ts, 28, 37)) | ||
|
||
// Implicit return, but undefined is assignable to records (which are just fancy objects) | ||
// when strictNullChecks is off. | ||
} | ||
|
||
function f7(): null { | ||
>f7 : Symbol(f7, Decl(functionsWithImplicitReturnTypeAssignableToUndefined.ts, 32, 1)) | ||
|
||
if (Math.random() < 0.5) return null; | ||
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) | ||
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) | ||
|
||
// Implicit return, but undefined is assignable to null when strictNullChecks is off. | ||
} | ||
|
||
function f8(): string | null { | ||
>f8 : Symbol(f8, Decl(functionsWithImplicitReturnTypeAssignableToUndefined.ts, 38, 1)) | ||
|
||
if (Math.random() < 0.5) return "foo"; | ||
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) | ||
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) | ||
|
||
// Implicit return, but undefined is assignable to null when strictNullChecks is off. | ||
} |
107 changes: 107 additions & 0 deletions
107
...erence/functionsWithImplicitReturnTypeAssignableToUndefined(strictnullchecks=false).types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
=== tests/cases/compiler/functionsWithImplicitReturnTypeAssignableToUndefined.ts === | ||
function f1(): unknown { | ||
>f1 : () => unknown | ||
|
||
if (Math.random() < 0.5) return true; | ||
>Math.random() < 0.5 : boolean | ||
>Math.random() : number | ||
>Math.random : () => number | ||
>Math : Math | ||
>random : () => number | ||
>0.5 : 0.5 | ||
>true : true | ||
|
||
// Implicit return, but undefined is always assignable to unknown. | ||
} | ||
|
||
type MyUnknown = unknown; | ||
>MyUnknown : unknown | ||
|
||
function f2(): unknown { | ||
>f2 : () => unknown | ||
|
||
if (Math.random() < 0.5) return true; | ||
>Math.random() < 0.5 : boolean | ||
>Math.random() : number | ||
>Math.random : () => number | ||
>Math : Math | ||
>random : () => number | ||
>0.5 : 0.5 | ||
>true : true | ||
|
||
// Implicit return, but undefined is always assignable to unknown. | ||
} | ||
|
||
function f3(): any { | ||
>f3 : () => any | ||
|
||
// Implicit return, but undefined is always assignable to any. | ||
} | ||
|
||
function f4(): void { | ||
>f4 : () => void | ||
|
||
// Implicit return, but undefined is always assignable to void. | ||
} | ||
|
||
function f5(): {} { | ||
>f5 : () => {} | ||
|
||
if (Math.random() < 0.5) return {}; | ||
>Math.random() < 0.5 : boolean | ||
>Math.random() : number | ||
>Math.random : () => number | ||
>Math : Math | ||
>random : () => number | ||
>0.5 : 0.5 | ||
>{} : {} | ||
|
||
// Implicit return, but undefined is assignable to object when strictNullChecks is off. | ||
} | ||
|
||
function f6(): Record<string, any> { | ||
>f6 : () => Record<string, any> | ||
|
||
if (Math.random() < 0.5) return { "foo": true }; | ||
>Math.random() < 0.5 : boolean | ||
>Math.random() : number | ||
>Math.random : () => number | ||
>Math : Math | ||
>random : () => number | ||
>0.5 : 0.5 | ||
>{ "foo": true } : { foo: boolean; } | ||
>"foo" : boolean | ||
>true : true | ||
|
||
// Implicit return, but undefined is assignable to records (which are just fancy objects) | ||
// when strictNullChecks is off. | ||
} | ||
|
||
function f7(): null { | ||
>f7 : () => null | ||
|
||
if (Math.random() < 0.5) return null; | ||
>Math.random() < 0.5 : boolean | ||
>Math.random() : number | ||
>Math.random : () => number | ||
>Math : Math | ||
>random : () => number | ||
>0.5 : 0.5 | ||
|
||
// Implicit return, but undefined is assignable to null when strictNullChecks is off. | ||
} | ||
|
||
function f8(): string | null { | ||
>f8 : () => string | null | ||
|
||
if (Math.random() < 0.5) return "foo"; | ||
>Math.random() < 0.5 : boolean | ||
>Math.random() : number | ||
>Math.random : () => number | ||
>Math : Math | ||
>random : () => number | ||
>0.5 : 0.5 | ||
>"foo" : "foo" | ||
|
||
// Implicit return, but undefined is assignable to null when strictNullChecks is off. | ||
} |
60 changes: 60 additions & 0 deletions
60
...ce/functionsWithImplicitReturnTypeAssignableToUndefined(strictnullchecks=true).errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
tests/cases/compiler/functionsWithImplicitReturnTypeAssignableToUndefined.ts(22,16): error TS2366: Function lacks ending return statement and return type does not include 'undefined'. | ||
tests/cases/compiler/functionsWithImplicitReturnTypeAssignableToUndefined.ts(28,16): error TS2366: Function lacks ending return statement and return type does not include 'undefined'. | ||
tests/cases/compiler/functionsWithImplicitReturnTypeAssignableToUndefined.ts(35,16): error TS2366: Function lacks ending return statement and return type does not include 'undefined'. | ||
tests/cases/compiler/functionsWithImplicitReturnTypeAssignableToUndefined.ts(41,16): error TS2366: Function lacks ending return statement and return type does not include 'undefined'. | ||
|
||
|
||
==== tests/cases/compiler/functionsWithImplicitReturnTypeAssignableToUndefined.ts (4 errors) ==== | ||
function f1(): unknown { | ||
if (Math.random() < 0.5) return true; | ||
|
||
// Implicit return, but undefined is always assignable to unknown. | ||
} | ||
|
||
type MyUnknown = unknown; | ||
function f2(): unknown { | ||
if (Math.random() < 0.5) return true; | ||
|
||
// Implicit return, but undefined is always assignable to unknown. | ||
} | ||
|
||
function f3(): any { | ||
// Implicit return, but undefined is always assignable to any. | ||
} | ||
|
||
function f4(): void { | ||
// Implicit return, but undefined is always assignable to void. | ||
} | ||
|
||
function f5(): {} { | ||
~~ | ||
!!! error TS2366: Function lacks ending return statement and return type does not include 'undefined'. | ||
if (Math.random() < 0.5) return {}; | ||
|
||
// Implicit return, but undefined is assignable to object when strictNullChecks is off. | ||
} | ||
|
||
function f6(): Record<string, any> { | ||
~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2366: Function lacks ending return statement and return type does not include 'undefined'. | ||
if (Math.random() < 0.5) return { "foo": true }; | ||
|
||
// Implicit return, but undefined is assignable to records (which are just fancy objects) | ||
// when strictNullChecks is off. | ||
} | ||
|
||
function f7(): null { | ||
~~~~ | ||
!!! error TS2366: Function lacks ending return statement and return type does not include 'undefined'. | ||
if (Math.random() < 0.5) return null; | ||
|
||
// Implicit return, but undefined is assignable to null when strictNullChecks is off. | ||
} | ||
|
||
function f8(): string | null { | ||
~~~~~~~~~~~~~ | ||
!!! error TS2366: Function lacks ending return statement and return type does not include 'undefined'. | ||
if (Math.random() < 0.5) return "foo"; | ||
|
||
// Implicit return, but undefined is assignable to null when strictNullChecks is off. | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.