Skip to content

Fixed const reverse mapped types themselves to be treated as const #55794

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
merged 3 commits into from
Nov 27, 2023

Conversation

Andarist
Copy link
Contributor

No description provided.

// @strict: true
// @noEmit: true

declare function test1<const T>(obj: {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The first 3 test cases are testing objects and how readonly, -readonly, and lack of them impact the T and the fresh object that is created by mapping over T.

Those objects today would be constified for the most part but their properties wouldn't always be marked as readonly when they should be. When readonly modifier is used on the mapped type then that result (typeof obj) has readonly properties but the const type parameter itself doesn't have them. This is consistent with #12589 . You might also want to recheck when/how readonly is preserved/stripped on members of the reverse mapped type here

[K in keyof T]: T[K];
}): T;

const result4 = test4(["1", 2]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is an especially important part of the fix. Currently T is inferred as (2 | "1")[] but it really should be readonly ["1", 2]

@Andarist Andarist force-pushed the fix/const-reverse-mapped-types branch from 1dbd5f3 to d6d1604 Compare September 20, 2023 07:04
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are also some other test cases that I'd like to add here but I'm not entirely sure what should be the expected results for them

  1. const T, P extends keyof T - this might iterate over P which might be the subset of keyof T. Should we still assign constness to T in such a case? See the playground TS playground. Note that it's not really possible to gather P across from different mapped types and somehow different mapped type to build up a "combined" reverse mapped type: TS playground
  2. K in keyof T | "extra" - what should happen with a non-homomorphic case like this? TS playground

Copy link
Member

Choose a reason for hiding this comment

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

  1. Probably? I'm not a huge fan of overuse of const type variables, but if they're in-use, there a strong signal that const-ness is desired somewhere related to that type parameter. The workflow is usually "does this do what I want? No? Add a const.", so the const normally only gets added by people if it's meant to be meaningful to inference.
  2. Eh, I'd leave that as whatever it falls out as from being not homomorphic. It'll get unified if and when we ever adjust the logic to consider something like that as homomorphic-enough to get the homomorphic type variable treatment.

@weswigham weswigham added this to the TypeScript 5.4.0 milestone Oct 4, 2023
@weswigham
Copy link
Member

@typescript-bot test this
@typescript-bot run dt
@typescript-bot test top100
@typescript-bot perf test this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Oct 4, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Oct 4, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Oct 4, 2023

Heya @weswigham, I've started to run the regular perf test suite on this PR at d6d1604. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

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

Here they are:

Compiler

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
Angular - node (v18.15.0, x64)
Memory used 295,072k (± 0.01%) 295,055k (± 0.01%) ~ 295,015k 295,102k p=0.575 n=6
Parse Time 2.63s (± 0.81%) 2.64s (± 0.28%) ~ 2.63s 2.65s p=0.368 n=6
Bind Time 0.84s (± 0.90%) 0.84s (± 1.23%) ~ 0.83s 0.85s p=0.664 n=6
Check Time 8.05s (± 0.26%) 8.06s (± 0.17%) ~ 8.05s 8.09s p=0.279 n=6
Emit Time 7.04s (± 0.29%) 7.03s (± 0.34%) ~ 7.00s 7.07s p=0.224 n=6
Total Time 18.57s (± 0.20%) 18.57s (± 0.15%) ~ 18.55s 18.61s p=0.870 n=6
Compiler-Unions - node (v18.15.0, x64)
Memory used 192,611k (± 1.56%) 191,624k (± 1.22%) ~ 190,627k 196,408k p=0.378 n=6
Parse Time 1.34s (± 1.29%) 1.34s (± 0.41%) ~ 1.34s 1.35s p=0.154 n=6
Bind Time 0.73s (± 0.00%) 0.73s (± 0.00%) ~ 0.73s 0.73s p=1.000 n=6
Check Time 9.19s (± 0.68%) 9.21s (± 0.66%) ~ 9.17s 9.33s p=0.617 n=6
Emit Time 2.64s (± 0.50%) 2.63s (± 0.67%) ~ 2.60s 2.64s p=0.266 n=6
Total Time 13.90s (± 0.36%) 13.91s (± 0.47%) ~ 13.86s 14.04s p=0.872 n=6
Monaco - node (v18.15.0, x64)
Memory used 347,287k (± 0.01%) 347,303k (± 0.00%) ~ 347,288k 347,315k p=0.108 n=6
Parse Time 2.46s (± 0.31%) 2.46s (± 0.31%) ~ 2.45s 2.47s p=1.000 n=6
Bind Time 0.94s (± 0.00%) 0.94s (± 0.00%) ~ 0.94s 0.94s p=1.000 n=6
Check Time 6.88s (± 0.44%) 6.89s (± 0.61%) ~ 6.83s 6.95s p=1.000 n=6
Emit Time 4.02s (± 0.49%) 4.03s (± 0.37%) ~ 4.01s 4.05s p=0.373 n=6
Total Time 14.30s (± 0.27%) 14.31s (± 0.36%) ~ 14.25s 14.40s p=0.808 n=6
TFS - node (v18.15.0, x64)
Memory used 302,537k (± 0.01%) 302,552k (± 0.00%) ~ 302,548k 302,556k p=0.172 n=6
Parse Time 2.00s (± 0.80%) 2.00s (± 0.66%) ~ 1.98s 2.02s p=0.801 n=6
Bind Time 1.00s (± 0.75%) 1.00s (± 0.81%) ~ 1.00s 1.02s p=1.000 n=6
Check Time 6.24s (± 0.33%) 6.26s (± 0.22%) ~ 6.24s 6.28s p=0.142 n=6
Emit Time 3.57s (± 0.49%) 3.58s (± 0.42%) ~ 3.56s 3.59s p=0.933 n=6
Total Time 12.82s (± 0.18%) 12.84s (± 0.32%) ~ 12.80s 12.90s p=0.629 n=6
material-ui - node (v18.15.0, x64)
Memory used 470,506k (± 0.00%) 470,520k (± 0.00%) ~ 470,481k 470,552k p=0.173 n=6
Parse Time 2.57s (± 0.52%) 2.57s (± 0.53%) ~ 2.55s 2.59s p=0.452 n=6
Bind Time 0.99s (± 0.76%) 1.00s (± 1.21%) +0.01s (+ 1.52%) 0.99s 1.02s p=0.045 n=6
Check Time 16.62s (± 0.26%) 16.66s (± 0.36%) ~ 16.60s 16.76s p=0.227 n=6
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=6
Total Time 20.18s (± 0.26%) 20.24s (± 0.35%) ~ 20.16s 20.36s p=0.148 n=6
xstate - node (v18.15.0, x64)
Memory used 512,588k (± 0.02%) 512,567k (± 0.01%) ~ 512,502k 512,669k p=0.873 n=6
Parse Time 3.27s (± 0.17%) 3.27s (± 0.47%) ~ 3.24s 3.28s p=0.859 n=6
Bind Time 1.55s (± 0.48%) 1.55s (± 0.41%) ~ 1.54s 1.56s p=0.718 n=6
Check Time 2.87s (± 0.68%) 2.89s (± 0.68%) ~ 2.85s 2.91s p=0.193 n=6
Emit Time 0.08s (± 5.21%) 0.08s (± 4.99%) ~ 0.08s 0.09s p=0.218 n=6
Total Time 7.77s (± 0.26%) 7.78s (± 0.27%) ~ 7.75s 7.81s p=0.518 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • Angular - node (v18.15.0, x64)
  • Compiler-Unions - node (v18.15.0, x64)
  • Monaco - node (v18.15.0, x64)
  • TFS - node (v18.15.0, x64)
  • material-ui - node (v18.15.0, x64)
  • xstate - node (v18.15.0, x64)
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

tsserver

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
Compiler-UnionsTSServer - node (v18.15.0, x64)
Req 1 - updateOpen 2,396ms (± 1.25%) 2,384ms (± 1.57%) ~ 2,331ms 2,427ms p=0.422 n=6
Req 2 - geterr 5,330ms (± 1.38%) 5,324ms (± 1.42%) ~ 5,276ms 5,476ms p=0.688 n=6
Req 3 - references 329ms (± 0.68%) 331ms (± 1.60%) ~ 326ms 338ms p=0.685 n=6
Req 4 - navto 278ms (± 1.31%) 278ms (± 0.80%) ~ 274ms 280ms p=0.607 n=6
Req 5 - completionInfo count 1,356 (± 0.00%) 1,356 (± 0.00%) ~ 1,356 1,356 p=1.000 n=6
Req 5 - completionInfo 77ms (± 8.20%) 78ms (± 7.79%) ~ 75ms 90ms p=0.356 n=6
CompilerTSServer - node (v18.15.0, x64)
Req 1 - updateOpen 2,472ms (± 1.17%) 2,485ms (± 1.20%) ~ 2,427ms 2,507ms p=0.298 n=6
Req 2 - geterr 4,132ms (± 1.87%) 4,087ms (± 1.45%) ~ 4,058ms 4,207ms p=0.470 n=6
Req 3 - references 338ms (± 1.62%) 340ms (± 1.31%) ~ 333ms 343ms p=1.000 n=6
Req 4 - navto 284ms (± 0.43%) 285ms (± 0.29%) ~ 284ms 286ms p=0.276 n=6
Req 5 - completionInfo count 1,518 (± 0.00%) 1,518 (± 0.00%) ~ 1,518 1,518 p=1.000 n=6
Req 5 - completionInfo 81ms (± 7.72%) 83ms (± 7.14%) ~ 75ms 87ms p=0.438 n=6
xstateTSServer - node (v18.15.0, x64)
Req 1 - updateOpen 2,596ms (± 0.44%) 2,574ms (± 0.61%) -22ms (- 0.83%) 2,561ms 2,605ms p=0.037 n=6
Req 2 - geterr 1,707ms (± 2.50%) 1,692ms (± 2.93%) ~ 1,638ms 1,741ms p=0.470 n=6
Req 3 - references 106ms (± 1.10%) 114ms (± 8.60%) ~ 106ms 127ms p=0.054 n=6
Req 4 - navto 359ms (± 0.29%) 358ms (± 0.65%) ~ 353ms 359ms p=0.121 n=6
Req 5 - completionInfo count 2,071 (± 0.00%) 2,071 (± 0.00%) ~ 2,071 2,071 p=1.000 n=6
Req 5 - completionInfo 308ms (± 1.27%) 304ms (± 3.13%) ~ 295ms 315ms p=0.629 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • CompilerTSServer - node (v18.15.0, x64)
  • Compiler-UnionsTSServer - node (v18.15.0, x64)
  • xstateTSServer - node (v18.15.0, x64)
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

Startup

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
tsc-startup - node (v18.15.0, x64)
Execution time 152.28ms (± 0.18%) 152.41ms (± 0.20%) +0.13ms (+ 0.09%) 151.16ms 155.20ms p=0.000 n=600
tsserver-startup - node (v18.15.0, x64)
Execution time 227.78ms (± 0.18%) 227.71ms (± 0.16%) -0.08ms (- 0.03%) 226.43ms 233.07ms p=0.018 n=600
tsserverlibrary-startup - node (v18.15.0, x64)
Execution time 228.56ms (± 0.19%) 228.61ms (± 0.18%) ~ 226.93ms 235.82ms p=0.329 n=600
typescript-startup - node (v18.15.0, x64)
Execution time 228.80ms (± 0.16%) 228.89ms (± 0.19%) ~ 227.15ms 237.69ms p=0.100 n=600
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • tsc-startup - node (v18.15.0, x64)
  • tsserver-startup - node (v18.15.0, x64)
  • tsserverlibrary-startup - node (v18.15.0, x64)
  • typescript-startup - node (v18.15.0, x64)
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

Developer Information:

Download Benchmarks

@typescript-bot
Copy link
Collaborator

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

Everything looks good!

@typescript-bot
Copy link
Collaborator

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

@sandersn sandersn added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants