Skip to content
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

Explicit undefined return type checked similar to explicit void return type #53607

Merged
merged 9 commits into from
Apr 1, 2023

Conversation

ahejlsberg
Copy link
Member

This PR (hopefully) completes the work started in #53092 and subsequently modified in #53490. With this and the other PRs, checking of functions changes as follows compared to 5.0:

  • In functions with an explicit or contextual return type undefined, expression-less return statements and the implicit return and the end of the function are considered to have type undefined (as opposed to void). Note that this does not apply to functions with union return types that include undefined.
  • Functions with an explicitly specified return type undefined aren't required to have return statements (similar to functions with an explicitly specified return type of void or any). Note that this does not apply to functions with union return types that include undefined.
  • The --noImplicitReturns option doesn't apply to functions with return types that include undefined (similar to functions with return types that include void, any, or unknown).

Fixes #36288.
Fixes #53473.

@ahejlsberg
Copy link
Member Author

@typescript-bot test this
@typescript-bot user test this inline
@typescript-bot run dt
@typescript-bot perf test faster
@typescript-bot test top100

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 31, 2023

Heya @ahejlsberg, I've started to run the extended test suite on this PR at 39ed3b8. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 31, 2023

Heya @ahejlsberg, I've started to run the diff-based user code test suite on this PR at 39ed3b8. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 31, 2023

Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at 39ed3b8. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 31, 2023

Heya @ahejlsberg, I've started to run the abridged perf test suite on this PR at 39ed3b8. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 31, 2023

Heya @ahejlsberg, I've started to run the diff-based top-repos suite on this PR at 39ed3b8. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

@ahejlsberg Here are the results of running the user test suite comparing main and refs/pull/53607/merge:

Unfortunately, something went wrong, but it probably wasn't caused by your change.

@typescript-bot
Copy link
Collaborator

@ahejlsberg
The results of the perf run you requested are in!

Here they are:

Comparison Report - main..53607

Metric main 53607 Delta Best Worst p-value
Angular - node (v16.17.1, x64)
Memory used 361,673k (± 0.00%) 361,744k (± 0.01%) +71k (+ 0.02%) 361,699k 361,785k p=0.005 n=6
Parse Time 3.51s (± 0.43%) 3.53s (± 0.75%) ~ 3.50s 3.56s p=0.331 n=6
Bind Time 1.17s (± 0.76%) 1.18s (± 0.64%) ~ 1.17s 1.19s p=0.053 n=6
Check Time 9.49s (± 0.27%) 9.52s (± 0.20%) +0.04s (+ 0.39%) 9.49s 9.54s p=0.019 n=6
Emit Time 7.95s (± 0.75%) 8.03s (± 1.06%) ~ 7.94s 8.17s p=0.128 n=6
Total Time 22.12s (± 0.34%) 22.27s (± 0.41%) +0.14s (+ 0.64%) 22.15s 22.40s p=0.013 n=6
Compiler-Unions - node (v16.17.1, x64)
Memory used 192,523k (± 0.03%) 192,581k (± 0.03%) ~ 192,485k 192,635k p=0.199 n=6
Parse Time 1.59s (± 1.17%) 1.60s (± 0.39%) ~ 1.59s 1.61s p=1.000 n=6
Bind Time 0.83s (± 0.66%) 0.83s (± 1.27%) ~ 0.81s 0.84s p=1.000 n=6
Check Time 10.35s (± 0.67%) 10.36s (± 0.56%) ~ 10.29s 10.42s p=0.746 n=6
Emit Time 2.99s (± 0.70%) 3.00s (± 1.10%) ~ 2.97s 3.06s p=0.871 n=6
Total Time 15.75s (± 0.67%) 15.79s (± 0.50%) ~ 15.70s 15.90s p=0.872 n=6
Monaco - node (v16.17.1, x64)
Memory used 345,757k (± 0.00%) 345,677k (± 0.00%) -81k (- 0.02%) 345,649k 345,691k p=0.005 n=6
Parse Time 2.72s (± 0.54%) 2.74s (± 0.48%) ~ 2.72s 2.75s p=0.050 n=6
Bind Time 1.08s (± 0.50%) 1.08s (± 0.77%) ~ 1.07s 1.09s p=0.855 n=6
Check Time 7.77s (± 0.30%) 7.77s (± 0.56%) ~ 7.73s 7.85s p=0.567 n=6
Emit Time 4.46s (± 0.67%) 4.50s (± 0.70%) ~ 4.45s 4.54s p=0.127 n=6
Total Time 16.03s (± 0.24%) 16.09s (± 0.43%) ~ 16.04s 16.22s p=0.090 n=6
TFS - node (v16.17.1, x64)
Memory used 300,032k (± 0.01%) 300,061k (± 0.01%) ~ 300,032k 300,114k p=0.128 n=6
Parse Time 2.16s (± 0.63%) 2.17s (± 0.86%) ~ 2.16s 2.21s p=0.149 n=6
Bind Time 1.23s (± 0.80%) 1.24s (± 0.94%) ~ 1.23s 1.26s p=0.209 n=6
Check Time 7.18s (± 0.41%) 7.24s (± 0.68%) +0.06s (+ 0.84%) 7.21s 7.34s p=0.018 n=6
Emit Time 4.35s (± 0.86%) 4.34s (± 0.84%) ~ 4.30s 4.39s p=0.747 n=6
Total Time 14.92s (± 0.43%) 15.00s (± 0.63%) ~ 14.92s 15.19s p=0.199 n=6
material-ui - node (v16.17.1, x64)
Memory used 476,461k (± 0.01%) 476,455k (± 0.01%) ~ 476,418k 476,488k p=0.810 n=6
Parse Time 3.22s (± 0.38%) 3.24s (± 0.55%) +0.02s (+ 0.73%) 3.21s 3.26s p=0.042 n=6
Bind Time 0.95s (± 0.00%) 0.96s (± 0.43%) +0.01s (+ 0.88%) 0.95s 0.96s p=0.007 n=6
Check Time 18.13s (± 0.56%) 18.24s (± 0.43%) ~ 18.14s 18.38s p=0.149 n=6
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=6
Total Time 22.30s (± 0.46%) 22.44s (± 0.42%) ~ 22.30s 22.59s p=0.066 n=6
xstate - node (v16.17.1, x64)
Memory used 550,589k (± 0.02%) 550,621k (± 0.02%) ~ 550,485k 550,758k p=0.689 n=6
Parse Time 3.93s (± 0.21%) 3.94s (± 0.25%) ~ 3.93s 3.95s p=0.140 n=6
Bind Time 1.75s (± 0.86%) 1.76s (± 0.51%) ~ 1.75s 1.77s p=0.557 n=6
Check Time 3.06s (± 0.45%) 3.09s (± 0.96%) ~ 3.06s 3.13s p=0.084 n=6
Emit Time 0.09s (± 0.00%) 0.09s (± 0.00%) ~ 0.09s 0.09s p=1.000 n=6
Total Time 8.83s (± 0.23%) 8.88s (± 0.38%) +0.04s (+ 0.47%) 8.84s 8.93s p=0.029 n=6
System
Machine Namets-ci-ubuntu
Platformlinux 5.4.0-135-generic
Architecturex64
Available Memory16 GB
Available Memory15 GB
CPUs4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Hosts
  • node (v16.17.1, x64)
Scenarios
  • Angular - node (v16.17.1, x64)
  • Compiler-Unions - node (v16.17.1, x64)
  • Monaco - node (v16.17.1, x64)
  • TFS - node (v16.17.1, x64)
  • material-ui - node (v16.17.1, x64)
  • xstate - node (v16.17.1, x64)
Benchmark Name Iterations
Current 53607 6
Baseline main 6

Developer Information:

Download Benchmark

@typescript-bot
Copy link
Collaborator

@ahejlsberg Here are the results of running the top-repos suite comparing main and refs/pull/53607/merge:

Everything looks good!

@typescript-bot
Copy link
Collaborator

Hey @ahejlsberg, the results of running the DT tests are ready.
Everything looks the same!
You can check the log here.

@gabritto
Copy link
Member

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 31, 2023

Heya @gabritto, I've started to run the tarball bundle task on this PR at 39ed3b8. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 31, 2023

Hey @gabritto, I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/151418/artifacts?artifactName=tgz&fileId=B89711228EC2A48ADEDB42C169AA808EADFEBAC64380B790A7B2A3C7A4798BEE02&fileName=/typescript-5.1.0-insiders.20230331.tgz"
    }
}

and then running npm install.


There is also a playground for this build and an npm module you can use via "typescript": "npm:@typescript-deploys/pr-build@5.1.0-pr-53607-12".;

@ahejlsberg
Copy link
Member Author

@typescript-bot perf test faster

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 31, 2023

Heya @ahejlsberg, I've started to run the abridged perf test suite on this PR at 39ed3b8. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

@ahejlsberg
The results of the perf run you requested are in!

Here they are:

Comparison Report - main..53607

Metric main 53607 Delta Best Worst p-value
Angular - node (v16.17.1, x64)
Memory used 361,701k (± 0.01%) 361,738k (± 0.01%) +37k (+ 0.01%) 361,697k 361,754k p=0.045 n=6
Parse Time 3.51s (± 1.00%) 3.51s (± 0.70%) ~ 3.47s 3.53s p=0.935 n=6
Bind Time 1.18s (± 0.54%) 1.18s (± 1.13%) ~ 1.16s 1.20s p=0.720 n=6
Check Time 9.52s (± 0.15%) 9.53s (± 0.52%) ~ 9.48s 9.60s p=0.936 n=6
Emit Time 7.98s (± 0.54%) 7.96s (± 0.67%) ~ 7.88s 8.04s p=0.517 n=6
Total Time 22.19s (± 0.27%) 22.18s (± 0.43%) ~ 22.10s 22.33s p=0.470 n=6
Compiler-Unions - node (v16.17.1, x64)
Memory used 193,105k (± 0.72%) 194,643k (± 0.85%) ~ 192,497k 195,759k p=0.298 n=6
Parse Time 1.59s (± 1.66%) 1.59s (± 0.47%) ~ 1.58s 1.60s p=0.805 n=6
Bind Time 0.83s (± 0.91%) 0.82s (± 0.50%) -0.01s (- 1.21%) 0.81s 0.82s p=0.024 n=6
Check Time 10.34s (± 0.74%) 10.33s (± 0.42%) ~ 10.25s 10.37s p=0.470 n=6
Emit Time 3.00s (± 0.46%) 2.99s (± 0.95%) ~ 2.96s 3.04s p=0.459 n=6
Total Time 15.75s (± 0.45%) 15.73s (± 0.41%) ~ 15.62s 15.82s p=0.373 n=6
Monaco - node (v16.17.1, x64)
Memory used 345,738k (± 0.01%) 345,680k (± 0.00%) -57k (- 0.02%) 345,669k 345,695k p=0.005 n=6
Parse Time 2.72s (± 0.46%) 2.71s (± 0.43%) ~ 2.69s 2.72s p=0.210 n=6
Bind Time 1.08s (± 0.50%) 1.08s (± 1.08%) ~ 1.07s 1.10s p=0.498 n=6
Check Time 7.81s (± 0.45%) 7.76s (± 0.44%) -0.05s (- 0.66%) 7.70s 7.80s p=0.036 n=6
Emit Time 4.46s (± 0.52%) 4.43s (± 0.78%) ~ 4.39s 4.47s p=0.220 n=6
Total Time 16.08s (± 0.33%) 15.99s (± 0.40%) ~ 15.92s 16.07s p=0.077 n=6
TFS - node (v16.17.1, x64)
Memory used 300,030k (± 0.00%) 300,051k (± 0.00%) +21k (+ 0.01%) 300,034k 300,074k p=0.045 n=6
Parse Time 2.17s (± 0.50%) 2.16s (± 1.03%) ~ 2.14s 2.20s p=0.251 n=6
Bind Time 1.23s (± 0.80%) 1.23s (± 0.42%) ~ 1.22s 1.23s p=0.348 n=6
Check Time 7.20s (± 0.20%) 7.22s (± 0.58%) ~ 7.17s 7.29s p=0.329 n=6
Emit Time 4.34s (± 0.38%) 4.33s (± 0.91%) ~ 4.29s 4.38s p=0.573 n=6
Total Time 14.96s (± 0.25%) 14.94s (± 0.53%) ~ 14.88s 15.05s p=0.571 n=6
material-ui - node (v16.17.1, x64)
Memory used 476,456k (± 0.00%) 476,461k (± 0.01%) ~ 476,426k 476,521k p=0.936 n=6
Parse Time 3.22s (± 0.46%) 3.21s (± 0.23%) ~ 3.20s 3.22s p=0.241 n=6
Bind Time 0.95s (± 0.43%) 0.95s (± 0.79%) ~ 0.94s 0.96s p=1.000 n=6
Check Time 18.15s (± 0.73%) 18.21s (± 0.38%) ~ 18.12s 18.30s p=0.297 n=6
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=6
Total Time 22.32s (± 0.66%) 22.37s (± 0.35%) ~ 22.27s 22.48s p=0.378 n=6
xstate - node (v16.17.1, x64)
Memory used 550,624k (± 0.03%) 550,615k (± 0.02%) ~ 550,483k 550,782k p=0.810 n=6
Parse Time 3.95s (± 0.49%) 3.92s (± 0.46%) -0.03s (- 0.80%) 3.90s 3.95s p=0.029 n=6
Bind Time 1.76s (± 0.23%) 1.76s (± 0.56%) ~ 1.75s 1.77s p=0.930 n=6
Check Time 3.08s (± 0.61%) 3.06s (± 0.45%) ~ 3.04s 3.08s p=0.089 n=6
Emit Time 0.09s (± 0.00%) 0.09s (± 0.00%) ~ 0.09s 0.09s p=1.000 n=6
Total Time 8.88s (± 0.40%) 8.83s (± 0.30%) -0.05s (- 0.56%) 8.79s 8.86s p=0.043 n=6
System
Machine Namets-ci-ubuntu
Platformlinux 5.4.0-135-generic
Architecturex64
Available Memory16 GB
Available Memory15 GB
CPUs4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Hosts
  • node (v16.17.1, x64)
Scenarios
  • Angular - node (v16.17.1, x64)
  • Compiler-Unions - node (v16.17.1, x64)
  • Monaco - node (v16.17.1, x64)
  • TFS - node (v16.17.1, x64)
  • material-ui - node (v16.17.1, x64)
  • xstate - node (v16.17.1, x64)
Benchmark Name Iterations
Current 53607 6
Baseline main 6

Developer Information:

Download Benchmark

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Mar 31, 2023

I reviewed, but let's just answer outstanding comments from @gabritto and others first.

@ahejlsberg
Copy link
Member Author

@typescript-bot user test this inline

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 31, 2023

Heya @ahejlsberg, I've started to run the diff-based user code test suite on this PR at 39ed3b8. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

@ahejlsberg Here are the results of running the user test suite comparing main and refs/pull/53607/merge:

Unfortunately, something went wrong, but it probably wasn't caused by your change.

@jakebailey
Copy link
Member

I'm looking into what's going on with those tests. Not sure what's going wrong; it looks green but then decides to say that something failed.

@jakebailey
Copy link
Member

It looks like one of the user tests no longer npm installs correctly; not totally sure why we fail the whole thing due to that, though. Otherwise, reading the logs, this PR shows no change in any of those user tests.

@ahejlsberg
Copy link
Member Author

@gabritto Yeah, it would be more consistent for all of them to return the error. I will change the logic to have noImplicitReturns only exclude functions that have exactly undefined as their return type.

}

const f21: () => undefined | number = () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

For these new cases, I would appreciate keeping the convention from the other tests of indicating if this will error :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, I can fix the comments.

@ahejlsberg
Copy link
Member Author

In addition to addressing @gabritto's feedback, latest commits align the rules for --noImplicitReturns to remove inconsistencies between functions with explicit unknown return types and functions with inferred unknown return types. Previously, only functions with explicit unknown return types would issue errors for implicit returns in --noImplicitReturns mode. Now both do.

@ahejlsberg
Copy link
Member Author

@typescript-bot test this
@typescript-bot user test this inline
@typescript-bot run dt
@typescript-bot test top100

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 1, 2023

Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at 40f9512. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 1, 2023

Heya @ahejlsberg, I've started to run the diff-based user code test suite on this PR at 40f9512. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 1, 2023

Heya @ahejlsberg, I've started to run the extended test suite on this PR at 40f9512. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 1, 2023

Heya @ahejlsberg, I've started to run the diff-based top-repos suite on this PR at 40f9512. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

@ahejlsberg Here are the results of running the user test suite comparing main and refs/pull/53607/merge:

Unfortunately, something went wrong, but it probably wasn't caused by your change.

@typescript-bot
Copy link
Collaborator

Hey @ahejlsberg, the results of running the DT tests are ready.
Everything looks the same!
You can check the log here.

@typescript-bot
Copy link
Collaborator

@ahejlsberg Here are the results of running the top-repos suite comparing main and refs/pull/53607/merge:

Something interesting changed - please have a look.

Details

microsoft/vscode

4 of 53 projects failed to build with the old tsc and were ignored

extensions/references-view/tsconfig.json

src/tsconfig.tsec.json

vuetifyjs/vuetify

4 of 7 projects failed to build with the old tsc and were ignored

packages/vuetify/tsconfig.checks.json

packages/vuetify/tsconfig.dist.json

packages/vuetify/tsconfig.json

@ahejlsberg
Copy link
Member Author

A few new errors in vscode and vuetify related to the change I mention here. I think these new errors are acceptable--the projects have noImplicitReturns enabled, the functions in question have inferred return types unknown (or Promise<unknown>), and the errors would have been reported if the functions had explicit return type annotations. It looks to me like all of the examples actually intended for the functions to return void but inadvertently returned values in some conditional code paths.

@ahejlsberg
Copy link
Member Author

ahejlsberg commented Apr 1, 2023

Otherwise things look good, so I'm going to merge this.

@ahejlsberg ahejlsberg merged commit b40385b into main Apr 1, 2023
@ahejlsberg ahejlsberg deleted the fix36288 branch April 1, 2023 21:44
@stevenwdv
Copy link

Sorry for replying to an old thread, but can I ask a question about this?

Why is this not allowed for unions with undefined? E.g. for event listeners:

function addListener(listener: (ev: Event) => undefined | boolean) {/*...*/}
addListener(ev => {/*... (no return)*/});
// Argument of type '(ev: Event) => void' is not assignable to parameter of type '(ev: Event) => boolean | undefined'.
//   Type 'void' is not assignable to type 'boolean | undefined'.

I think this could be a nice use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
7 participants