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

Reverse mapped types with intersection constraint #55811

Merged

Conversation

jfet97
Copy link
Contributor

@jfet97 jfet97 commented Sep 21, 2023

TLDR

This PR enhances reverse mapped types capabilities providing users with the ability to enable EPC (Excess Property Checking) on type parameters inference sites.

Background

This PR has come up trying to find a way to solve some problems discussed in #55645 and #55644, which are a simplification of what's going on in this PR from XState. In those cases the usual type parameters inference behaviour is not enough, mainly because it disables EPC (Excess Property Checking).

FIxes #55927

Idea

Reverse mapped types are a powerful tool. Currently TypeScript is able to reverse three kind of mapped types:

  1. a homomorphic mapped type { [P in keyof T]: X }
  2. a mapped type { [P in K]: X }, where K is a type parameter
  3. a mapped type with an union constraint, as long as the union contains one of the constraints of the previous cases

This PR adds a fourth case: a mapped type with an intersection constraint, as long as the intersection contains one of the constraints of the previous cases.

While the union constraint could be seen as a way to force the presence of some properties, the intersection one could be seen as a way to prevent the presence of extra properties.

Examples

Example N. 1

Here the type parameter T is now inferred, by reversing the mapped type, as { bar: 1; baz: "a"; record: { a: number; b: number } }, instead of falling back to its constraint Foo. The properties of the parameter s are constrained to be exactly the ones already declared in Foo, so EPC comes into play if the argument contains extra properties:

interface Foo {
  bar: number;
  baz: string;
  record: Record<any, any>;
}

// the intersection constraint on `K` is `keyof T & keyof Foo`
declare function useFoo<T extends Foo>(s: { [K in keyof T & keyof Foo]: T[K] } ): T

useFoo({ 
  bar: 1,
  baz: 'a',
  record: { a: 1, b: 2 },
  extra: 123, // <-- EPC error
})

In this way you get both inference and EPC check!
Note that the extra property is stripped away from T because it wouldn't survive the action of the mapped type anyway.

Example N. 2

In the following example, inspired from #12936, is the type parameter U the one that gets inferred by reversing the mapped type. It is inferred as { x: 1; y: "y"; record: { a: number; b: number } } instead of falling back to its constraint. As before, no extra properties are allowed so we get an EPC error on z that will always be stripped away from the inferred type:

// the intersection constraint on `K` is `keyof U & keyof T`
const checkType = <T>() => <U extends T>(value: { [K in keyof U & keyof T]: U[K] }) => value;

const checked = checkType<{x: number, y: string, record: Record<any, any> }>()({
  x: 1,
  y: "y",
  record: { a: 1, b: 2 },
  z: "z", // <-- EPC error
});

Tests

Two tests files have been added:

  1. reverseMappedTypeIntersectionConstraint, in which the main capabilities brought by this PR have been tested, although I must admit I don't think there are enough test cases
  2. reverseMappedTypeLimitedConstraint, in which the stripping behaviour on the inferred type parameters has been written down, but it is somehow subsumed by the previous test and for this reason it might be deleted altogether

A note on EPC

What's good about this PR is the fact that it lets the user enable EPC on type parameters without fundamentally change how TS treats type parameters inference. It's just a nice side effect of enhancing reverse mapped types' capabilities.

Remarks

This PR may help with some cases presented in #12936 as well.

#55794 could help improving what happens when a const type parameter is used, because there are some inconsistencies with readonly modifiers.

Acknowledgements

Huge thanks to @Andarist for always having my back 😄.

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Sep 21, 2023
@typescript-bot
Copy link
Collaborator

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

@jfet97
Copy link
Contributor Author

jfet97 commented Sep 21, 2023

@microsoft-github-policy-service agree

@sandersn sandersn added this to Not started in PR Backlog Sep 29, 2023
@sandersn
Copy link
Member

@jfet97 can you open a new bug with your proposal where we can discuss it? For complex issues like this the PR itself is usually pretty short, but if it sits around for long the code gets stale.

@sandersn sandersn moved this from Not started to Waiting on author in PR Backlog Sep 29, 2023
@sandersn sandersn self-assigned this Sep 29, 2023
@jfet97
Copy link
Contributor Author

jfet97 commented Sep 30, 2023

Done 😄, it's #55927 @sandersn

@jfet97 jfet97 changed the title Reverse mapped type with intersection constraint Reverse mapped types with intersection constraint Sep 30, 2023
@weswigham weswigham added this to the TypeScript 5.4.0 milestone Oct 3, 2023
@weswigham
Copy link
Member

This looks pretty good, but since it's definitely pretty high on the break risk scale (since it's intentionally adding new errors), let me run the extended tests and say that we'll probably want to merge this after we cut the 5.3 rc, so it can be in the nightly for awhile.

@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 3, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Oct 3, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Oct 3, 2023

Heya @weswigham, I've started to run the diff-based top-repos suite on this PR at d3d7a45. 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,085k (± 0.02%) 295,069k (± 0.01%) ~ 295,021k 295,133k p=0.630 n=6
Parse Time 2.64s (± 0.39%) 2.64s (± 0.46%) ~ 2.62s 2.65s p=1.000 n=6
Bind Time 0.84s (± 0.99%) 0.84s (± 1.43%) ~ 0.83s 0.86s p=0.863 n=6
Check Time 8.05s (± 0.35%) 8.06s (± 0.33%) ~ 8.02s 8.09s p=0.809 n=6
Emit Time 7.05s (± 0.29%) 7.04s (± 0.24%) ~ 7.02s 7.07s p=0.220 n=6
Total Time 18.59s (± 0.15%) 18.58s (± 0.21%) ~ 18.51s 18.63s p=0.808 n=6
Compiler-Unions - node (v18.15.0, x64)
Memory used 194,557k (± 1.53%) 192,636k (± 1.58%) ~ 190,642k 196,618k p=0.336 n=6
Parse Time 1.35s (± 1.15%) 1.35s (± 1.28%) ~ 1.33s 1.38s p=0.865 n=6
Bind Time 0.73s (± 0.00%) 0.73s (± 0.56%) ~ 0.73s 0.74s p=0.405 n=6
Check Time 9.18s (± 1.00%) 9.18s (± 0.80%) ~ 9.12s 9.28s p=0.744 n=6
Emit Time 2.63s (± 0.66%) 2.65s (± 0.62%) ~ 2.63s 2.67s p=0.324 n=6
Total Time 13.89s (± 0.62%) 13.90s (± 0.44%) ~ 13.83s 13.98s p=0.568 n=6
Monaco - node (v18.15.0, x64)
Memory used 347,294k (± 0.00%) 347,302k (± 0.00%) ~ 347,279k 347,316k p=0.296 n=6
Parse Time 2.46s (± 0.33%) 2.46s (± 0.61%) ~ 2.45s 2.49s p=0.738 n=6
Bind Time 0.94s (± 0.43%) 0.94s (± 0.00%) ~ 0.94s 0.94s p=0.405 n=6
Check Time 6.87s (± 0.18%) 6.91s (± 0.72%) ~ 6.87s 6.99s p=0.073 n=6
Emit Time 4.01s (± 0.37%) 4.02s (± 0.37%) ~ 4.00s 4.04s p=0.243 n=6
Total Time 14.29s (± 0.25%) 14.34s (± 0.30%) ~ 14.29s 14.40s p=0.092 n=6
TFS - node (v18.15.0, x64)
Memory used 302,536k (± 0.00%) 302,540k (± 0.01%) ~ 302,521k 302,574k p=0.422 n=6
Parse Time 2.01s (± 0.73%) 2.00s (± 0.98%) ~ 1.97s 2.02s p=0.871 n=6
Bind Time 1.00s (± 1.36%) 1.00s (± 0.98%) ~ 0.99s 1.02s p=1.000 n=6
Check Time 6.27s (± 0.57%) 6.24s (± 0.27%) ~ 6.22s 6.26s p=0.168 n=6
Emit Time 3.56s (± 0.63%) 3.57s (± 0.33%) ~ 3.56s 3.59s p=0.289 n=6
Total Time 12.84s (± 0.23%) 12.81s (± 0.22%) ~ 12.79s 12.85s p=0.370 n=6
material-ui - node (v18.15.0, x64)
Memory used 470,506k (± 0.00%) 470,507k (± 0.00%) ~ 470,480k 470,532k p=1.000 n=6
Parse Time 2.57s (± 0.47%) 2.57s (± 0.49%) ~ 2.55s 2.59s p=0.738 n=6
Bind Time 1.00s (± 1.09%) 0.99s (± 0.52%) ~ 0.99s 1.00s p=0.247 n=6
Check Time 16.65s (± 0.55%) 16.66s (± 0.27%) ~ 16.58s 16.70s p=1.000 n=6
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=6
Total Time 20.22s (± 0.43%) 20.22s (± 0.17%) ~ 20.16s 20.25s p=0.936 n=6
xstate - node (v18.15.0, x64)
Memory used 512,618k (± 0.01%) 512,597k (± 0.01%) ~ 512,543k 512,714k p=0.575 n=6
Parse Time 3.27s (± 0.23%) 3.27s (± 0.23%) ~ 3.26s 3.28s p=1.000 n=6
Bind Time 1.55s (± 0.00%) 1.55s (± 0.26%) ~ 1.54s 1.55s p=0.405 n=6
Check Time 2.87s (± 0.26%) 2.88s (± 1.18%) ~ 2.84s 2.93s p=0.871 n=6
Emit Time 0.08s (± 0.00%) 0.08s (± 0.00%) ~ 0.08s 0.08s p=1.000 n=6
Total Time 7.76s (± 0.15%) 7.76s (± 0.38%) ~ 7.73s 7.81s p=0.871 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,377ms (± 1.53%) 2,359ms (± 1.20%) ~ 2,327ms 2,396ms p=0.335 n=6
Req 2 - geterr 5,325ms (± 1.26%) 5,368ms (± 1.64%) ~ 5,257ms 5,469ms p=0.471 n=6
Req 3 - references 333ms (± 1.39%) 330ms (± 1.58%) ~ 325ms 338ms p=0.293 n=6
Req 4 - navto 278ms (± 0.58%) 277ms (± 0.94%) ~ 273ms 280ms p=0.677 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 (± 5.98%) 83ms (±10.41%) ~ 74ms 91ms p=0.303 n=6
CompilerTSServer - node (v18.15.0, x64)
Req 1 - updateOpen 2,457ms (± 1.27%) 2,473ms (± 1.43%) ~ 2,426ms 2,515ms p=0.471 n=6
Req 2 - geterr 4,127ms (± 1.89%) 4,137ms (± 2.03%) ~ 4,050ms 4,218ms p=0.471 n=6
Req 3 - references 337ms (± 1.55%) 336ms (± 1.14%) ~ 332ms 343ms p=1.000 n=6
Req 4 - navto 284ms (± 0.35%) 284ms (± 0.18%) ~ 283ms 284ms p=0.931 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 79ms (± 7.50%) 78ms (± 6.27%) ~ 76ms 88ms p=0.654 n=6
xstateTSServer - node (v18.15.0, x64)
Req 1 - updateOpen 2,594ms (± 0.15%) 2,600ms (± 0.42%) ~ 2,582ms 2,614ms p=0.170 n=6
Req 2 - geterr 1,689ms (± 2.28%) 1,683ms (± 1.74%) ~ 1,651ms 1,719ms p=0.689 n=6
Req 3 - references 108ms (± 4.69%) 120ms (± 9.45%) ~ 104ms 128ms p=0.170 n=6
Req 4 - navto 360ms (± 0.43%) 360ms (± 0.94%) ~ 354ms 364ms p=0.803 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 306ms (± 1.74%) 307ms (± 1.14%) ~ 303ms 312ms p=0.688 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.25ms (± 0.18%) 152.30ms (± 0.17%) ~ 151.30ms 154.36ms p=0.094 n=600
tsserver-startup - node (v18.15.0, x64)
Execution time 227.75ms (± 0.16%) 227.72ms (± 0.17%) ~ 226.45ms 234.14ms p=0.053 n=600
tsserverlibrary-startup - node (v18.15.0, x64)
Execution time 228.45ms (± 0.21%) 228.54ms (± 0.21%) +0.09ms (+ 0.04%) 226.99ms 243.49ms p=0.012 n=600
typescript-startup - node (v18.15.0, x64)
Execution time 228.82ms (± 0.17%) 228.82ms (± 0.16%) ~ 227.09ms 231.45ms p=0.905 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/55811/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.

@jfet97
Copy link
Contributor Author

jfet97 commented Oct 4, 2023

@weswigham thanks for your review, I'm glad you liked it!

To be honest I didn't expect any particular problems because:

  1. reverse mapped types are sort of a niche/advanced feature
  2. if you try to reverse a mapped type with an intersection constraint now the result is pretty useless, in the sense that it doesn't infer the type parameter, so I find it hard to believe that it ever occurred to someone to try 😂

I believe the tests not finding any errors supports my opinion, but there is no rush to add this feature of course. I'm just sharing my thoughts.

@sandersn sandersn moved this from Waiting on author to Needs merge in PR Backlog Oct 4, 2023
@@ -13523,14 +13523,34 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return instantiateType(instantiable, createTypeMapper([type.indexType, type.objectType], [getNumberLiteralType(0), createTupleType([replacement])]));
}

function getLimitedConstraint(type: ReverseMappedType) {
Copy link
Member

Choose a reason for hiding this comment

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

I'd really like a comment explaining a "limited constraint" is, maybe with an example or two. Otherwise LGTM

Copy link
Contributor Author

@jfet97 jfet97 Oct 5, 2023

Choose a reason for hiding this comment

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

Comments have been added. I hope it's clearer now.

@typescript-bot typescript-bot added For Backlog Bug PRs that fix a backlog bug and removed For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Oct 5, 2023
@Andarist
Copy link
Contributor

Andarist commented Oct 23, 2023

@jakebailey would you be so kind to create a TS playground for this one? :)

@jakebailey
Copy link
Member

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Oct 23, 2023

Heya @jakebailey, I've started to run the tarball bundle task on this PR at 21fae9e. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Oct 23, 2023

Hey @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/158307/artifacts?artifactName=tgz&fileId=A516E71E34A0566287DB0253279348F1DBC0344BC8D24046ACDBDC7EE31BF6CD02&fileName=/typescript-5.3.0-insiders.20231023.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.3.0-pr-55811-15".;

@sandersn sandersn merged commit eb2046d into microsoft:main Nov 30, 2023
19 checks passed
PR Backlog automation moved this from Needs merge to Done Nov 30, 2023
@jfet97
Copy link
Contributor Author

jfet97 commented Dec 1, 2023

Another example can be found on this playground by @Andarist , to be compared with the 5.3.2 one. The latter fails to infer completely in the first scenario.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
PR Backlog
  
Done
Development

Successfully merging this pull request may close these issues.

Support for reverse mapped types with intersection constraint
7 participants