Skip to content

Fixed inferred const rest types in signatures - #55034

Closed
Mateusz Burzyński (Andarist) wants to merge 2 commits into
microsoft:mainfrom
Andarist:fix/const-type-variables-in-rest-positions
Closed

Fixed inferred const rest types in signatures#55034
Mateusz Burzyński (Andarist) wants to merge 2 commits into
microsoft:mainfrom
Andarist:fix/const-type-variables-in-rest-positions

Conversation

@Andarist

Copy link
Copy Markdown
Contributor

fixes#55033

@typescript-botTypeScript Bot (typescript-bot) added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Jul 15, 2023
@ssalbdivad

David Blass (ssalbdivad) commented Jul 16, 2023

Copy link
Copy Markdown

Pretty hyped for this one. I've run into so many issues trying to constrain narrowed array types without breaking inference. Couldn't really tell from the linked issue if this will fix a lot of them, but I have faith 🙏

@jakebailey

Copy link
Copy Markdown
Member

After messing with this same code for #53255, #53258 (note a similar getRestTypeAtPosition change), #53398, I'm like 50% afraid of this 😄

TypeScript Bot (@typescript-bot) test this
TypeScript Bot (@typescript-bot) test top100
TypeScript Bot (@typescript-bot) user test this
TypeScript Bot (@typescript-bot) run dt
TypeScript Bot (@typescript-bot) perf test this faster
TypeScript Bot (@typescript-bot) pack this

@typescript-bot

TypeScript Bot (typescript-bot) commented Jul 18, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the diff-based top-repos suite on this PR at fb32b83. You can monitor the build here.

Update: The results are in!

@typescript-bot

TypeScript Bot (typescript-bot) commented Jul 18, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the diff-based user code test suite on this PR at fb32b83. You can monitor the build here.

Update: The results are in!

@typescript-bot

TypeScript Bot (typescript-bot) commented Jul 18, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the abridged perf test suite on this PR at fb32b83. You can monitor the build here.

Update: The results are in!

@typescript-bot

TypeScript Bot (typescript-bot) commented Jul 18, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the tarball bundle task on this PR at fb32b83. You can monitor the build here.

@typescript-bot

TypeScript Bot (typescript-bot) commented Jul 18, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the extended test suite on this PR at fb32b83. You can monitor the build here.

@typescript-bot

TypeScript Bot (typescript-bot) commented Jul 18, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the parallelized Definitely Typed test suite on this PR at fb32b83. You can monitor the build here.

Update: The results are in!

@typescript-bot

TypeScript Bot (typescript-bot) commented Jul 18, 2023

Copy link
Copy Markdown
Contributor

Hey Jake Bailey (@jakebailey), 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/155902/artifacts?artifactName=tgz&fileId=FAE8E38594F6A90F7E599497909CC324A2C4C0E435E4CFA4BD738E7304C0DB8F02&fileName=/typescript-5.2.0-insiders.20230718.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.2.0-pr-55034-8".;

@Andarist

Copy link
Copy Markdown
ContributorAuthor

After messing with this same code for #53255, #53258 (note a similar getRestTypeAtPosition change), #53398, I'm like 50% afraid of this 😄

When learning about this issue I immediately opened your PR to check out if it's related anyhow to this issue. I think that at the end of the day, it isn't and that this PR shouldn't actually impact anybody negatively. This only makes those rests readonly when the type variable is const - given that const is pretty new I don't expect any accidental breakage here.

@jakebailey

Copy link
Copy Markdown
Member

After messing with this same code for #53255, #53258 (note a similar getRestTypeAtPosition change), #53398, I'm like 50% afraid of this 😄

When learning about this issue I immediately opened your PR to check out if it's related anyhow to this issue. I think that at the end of the day, it isn't and that this PR shouldn't actually impact anybody negatively. This only makes those rests readonly when the type variable is const - given that const is pretty new I don't expect any accidental breakage here.

Yeah, I'm pretty sure that I was a little overzealous in propagating readonly to the last use (this PR), where my issue was definitely intended to change the other use.

@typescript-bot

Copy link
Copy Markdown
Contributor

Jake Bailey (@jakebailey)
The results of the perf run you requested are in!

Here they are:

Comparison Report - main..55034

Metricmain55034DeltaBestWorstp-value
Angular - node (v16.17.1, x64)
Memory used366,223k (± 0.01%)366,214k (± 0.00%)~366,200k366,225kp=0.423 n=6
Parse Time3.56s (± 0.65%)3.58s (± 0.63%)~3.56s3.62sp=0.168 n=6
Bind Time1.19s (± 0.63%)1.20s (± 0.46%)~1.19s1.20sp=0.137 n=6
Check Time9.71s (± 0.46%)9.67s (± 0.42%)~9.62s9.73sp=0.146 n=6
Emit Time8.09s (± 0.46%)8.02s (± 0.77%)~7.96s8.12sp=0.065 n=6
Total Time22.54s (± 0.28%)22.46s (± 0.43%)~22.37s22.63sp=0.126 n=6
Compiler-Unions - node (v16.17.1, x64)
Memory used193,442k (± 0.68%)193,464k (± 0.68%)~192,849k196,148kp=0.471 n=6
Parse Time1.59s (± 0.51%)1.59s (± 0.62%)~1.57s1.60sp=0.383 n=6
Bind Time0.83s (± 0.66%)0.82s (± 0.92%)~0.81s0.83sp=0.137 n=6
Check Time10.15s (± 0.76%)10.18s (± 0.56%)~10.11s10.28sp=0.378 n=6
Emit Time3.00s (± 0.53%)3.01s (± 1.35%)~2.98s3.09sp=1.000 n=6
Total Time15.56s (± 0.54%)15.60s (± 0.54%)~15.51s15.76sp=0.378 n=6
Monaco - node (v16.17.1, x64)
Memory used346,183k (± 0.01%)346,189k (± 0.00%)~346,176k346,200kp=0.689 n=6
Parse Time2.77s (± 0.49%)2.78s (± 0.77%)~2.76s2.82sp=0.742 n=6
Bind Time1.07s (± 0.38%)1.08s (± 0.59%)+0.01s (+ 1.09%)1.07s1.09sp=0.009 n=6
Check Time8.01s (± 0.67%)8.03s (± 0.45%)~7.98s8.07sp=0.687 n=6
Emit Time4.49s (± 0.60%)4.47s (± 0.86%)~4.44s4.54sp=0.332 n=6
Total Time16.34s (± 0.50%)16.35s (± 0.41%)~16.27s16.45sp=0.872 n=6
TFS - node (v16.17.1, x64)
Memory used300,246k (± 0.00%)300,229k (± 0.01%)~300,203k300,252kp=0.128 n=6
Parse Time2.21s (± 0.62%)2.21s (± 0.72%)~2.19s2.23sp=0.869 n=6
Bind Time1.20s (± 1.33%)1.21s (± 0.81%)~1.19s1.22sp=0.510 n=6
Check Time7.33s (± 0.45%)7.34s (± 0.34%)~7.30s7.37sp=0.618 n=6
Emit Time4.35s (± 0.52%)4.33s (± 1.25%)~4.26s4.42sp=0.145 n=6
Total Time15.09s (± 0.33%)15.08s (± 0.37%)~15.01s15.14sp=0.574 n=6
material-ui - node (v16.17.1, x64)
Memory used481,657k (± 0.01%)481,630k (± 0.01%)~481,584k481,695kp=0.575 n=6
Parse Time3.25s (± 0.41%)3.27s (± 0.57%)+0.02s (+ 0.46%)3.23s3.28sp=0.048 n=6
Bind Time0.96s (± 0.78%)0.96s (± 1.26%)~0.95s0.98sp=0.235 n=6
Check Time18.45s (± 0.60%)18.36s (± 0.58%)~18.28s18.57sp=0.199 n=6
Emit Time0.00s (± 0.00%)0.00s (± 0.00%)~0.00s0.00sp=1.000 n=6
Total Time22.67s (± 0.46%)22.58s (± 0.52%)~22.47s22.81sp=0.199 n=6
xstate - node (v16.17.1, x64)
Memory used561,367k (± 0.03%)561,174k (± 0.02%)-193k (- 0.03%)561,060k561,346kp=0.031 n=6
Parse Time4.00s (± 0.34%)4.03s (± 0.43%)+0.02s (+ 0.54%)4.01s4.06sp=0.042 n=6
Bind Time1.78s (± 1.05%)1.79s (± 1.22%)~1.77s1.83sp=0.505 n=6
Check Time3.07s (± 0.39%)3.06s (± 0.68%)~3.04s3.09sp=0.514 n=6
Emit Time0.10s (± 5.76%)0.10s (± 5.76%)~0.09s0.10sp=1.000 n=6
Total Time8.94s (± 0.39%)8.97s (± 0.44%)~8.92s9.03sp=0.228 n=6
System
Machine Namets-ci-ubuntu
Platformlinux 5.4.0-148-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)
BenchmarkNameIterations
Current550346
Baselinemain6

Developer Information:

Download Benchmark

@typescript-bot

Copy link
Copy Markdown
Contributor

Jake Bailey (@jakebailey) Here are the results of running the user test suite comparing main and refs/pull/55034/merge:

There were infrastructure failures potentially unrelated to your change:

  • 1 instance of "Unknown failure"
  • 1 instance of "Package install failed"

Otherwise...

Something interesting changed - please have a look.

Details

rxjs-src

/mnt/ts_downloads/rxjs-src/build.sh

  • [NEW] error TS2428: All declarations of 'WeakMap' must have identical type parameters.
    • /home/vsts/work/1/s/typescript-55034/lib/lib.es2015.collection.d.ts(63,11)
    • /home/vsts/work/1/s/typescript-55034/lib/lib.es2015.iterable.d.ts(162,11)
    • /home/vsts/work/1/s/typescript-55034/lib/lib.es2015.symbol.wellknown.d.ts(140,11)
    • /home/vsts/work/1/s/typescript-55034/lib/lib.es2015.collection.d.ts(63,11)
    • /home/vsts/work/1/s/typescript-55034/lib/lib.es2015.iterable.d.ts(162,11)
    • /home/vsts/work/1/s/typescript-55034/lib/lib.es2015.symbol.wellknown.d.ts(140,11)
    • /home/vsts/work/1/s/typescript-55034/lib/lib.es2015.collection.d.ts(63,11)
    • /home/vsts/work/1/s/typescript-55034/lib/lib.es2015.iterable.d.ts(162,11)
    • /home/vsts/work/1/s/typescript-55034/lib/lib.es2015.symbol.wellknown.d.ts(140,11)
    • /home/vsts/work/1/s/typescript-55034/lib/lib.es2015.collection.d.ts(63,11)
    • /home/vsts/work/1/s/typescript-55034/lib/lib.es2015.iterable.d.ts(162,11)
    • /home/vsts/work/1/s/typescript-55034/lib/lib.es2015.symbol.wellknown.d.ts(140,11)
    • /home/vsts/work/1/s/typescript-55034/lib/lib.es2015.collection.d.ts(63,11)
    • /home/vsts/work/1/s/typescript-55034/lib/lib.es2015.iterable.d.ts(162,11)
    • /home/vsts/work/1/s/typescript-55034/lib/lib.es2015.symbol.wellknown.d.ts(140,11)
  • [MISSING] error TS2428: All declarations of 'WeakMap' must have identical type parameters.
    • /home/vsts/work/1/s/typescript-main/lib/lib.es2015.collection.d.ts(63,11)
    • /home/vsts/work/1/s/typescript-main/lib/lib.es2015.iterable.d.ts(162,11)
    • /home/vsts/work/1/s/typescript-main/lib/lib.es2015.symbol.wellknown.d.ts(140,11)
    • /home/vsts/work/1/s/typescript-main/lib/lib.es2015.collection.d.ts(63,11)
    • /home/vsts/work/1/s/typescript-main/lib/lib.es2015.iterable.d.ts(162,11)
    • /home/vsts/work/1/s/typescript-main/lib/lib.es2015.symbol.wellknown.d.ts(140,11)
    • /home/vsts/work/1/s/typescript-main/lib/lib.es2015.collection.d.ts(63,11)
    • /home/vsts/work/1/s/typescript-main/lib/lib.es2015.iterable.d.ts(162,11)
    • /home/vsts/work/1/s/typescript-main/lib/lib.es2015.symbol.wellknown.d.ts(140,11)
    • /home/vsts/work/1/s/typescript-main/lib/lib.es2015.collection.d.ts(63,11)
    • /home/vsts/work/1/s/typescript-main/lib/lib.es2015.iterable.d.ts(162,11)
    • /home/vsts/work/1/s/typescript-main/lib/lib.es2015.symbol.wellknown.d.ts(140,11)
    • /home/vsts/work/1/s/typescript-main/lib/lib.es2015.collection.d.ts(63,11)
    • /home/vsts/work/1/s/typescript-main/lib/lib.es2015.iterable.d.ts(162,11)
    • /home/vsts/work/1/s/typescript-main/lib/lib.es2015.symbol.wellknown.d.ts(140,11)

@typescript-bot

Copy link
Copy Markdown
Contributor

Jake Bailey (@jakebailey) Here are the results of running the top-repos suite comparing main and refs/pull/55034/merge:

Everything looks good!

@typescript-bot

Copy link
Copy Markdown
Contributor

Hey Jake Bailey (@jakebailey), the results of running the DT tests are ready.
Everything looks the same!
You can check the log here.

@sandersn

Copy link
Copy Markdown
Member

The bug is marked Working As Intended now, so should we close this PR?

@Andarist

Copy link
Copy Markdown
ContributorAuthor

The reported issue had some issues in it (it didnt use a readonly constraint). So im not sure if „working as intended” was applied to only what was oroginally reported or if it was applied to everything that was discussed there. I think that this particular thing that I fixed here still has some merit so i’d appreciate if this would get reviewed

@ahejlsberg

Copy link
Copy Markdown
Member

I like where this is going, but I think we also need the changes in #55229. For example, the following is broken by this PR without the additional changes:

declarefunctioninvoke<constTextendsunknown[]>(f: (...args: T)=>void, ...args: T): T;invoke((a: string,b: number)=>{},"hello",123);

I know this isn't a particularly likely break, but the inference failure because of non-readonly constraints is just confounding to most users and hard to justify (even if I've tried).

@Andarist

Mateusz Burzyński (Andarist) commented Aug 1, 2023

Copy link
Copy Markdown
ContributorAuthor

I know this isn't a particularly likely break, but the inference failure because of non-readonly constraints is just confounding to most users and hard to justify (even if I've tried).

ye, this tends to be pretty confusing - would it be worth adding some diagnostics that would prompt users to add the reaodnly modifier to their constraints? Might not be feasible when constraints are defined using aliases but catching common cases when they are declared directly in the signature could still help a lot

@ahejlsberg

Copy link
Copy Markdown
Member

would it be worth adding some diagnostics that would prompt users to add the reaodnly modifier to their constraints?

I'm hoping there's no need for that with #55229 since inference now adjusts itself based on constraints.

@Andarist

Copy link
Copy Markdown
ContributorAuthor

Closing as those commits are now included in #55229

@microsoftMicrosoft (microsoft) locked as resolved and limited conversation to collaborators Oct 22, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

For Uncommitted BugPR for untriaged, rejected, closed or missing bug

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Type hole: A function with const generic does not infer the type if it is a spread parameter (tuple)

6 participants

@Andarist@ssalbdivad@jakebailey@typescript-bot@sandersn@ahejlsberg