GeezerVison is showing github.com — Allow self referential type aliases by weswigham · Pull Request #33018 · microsoft/TypeScript
Conversation
Fixes #32967
I talked about this in the linked issue, but I think we already have pretty close to what we need in place to handle recursive type aliases - so this PR adds the parts we were missing.
With this you can now write:
type HypertextNode = string | [string, { [key: string]: any }, ...HypertextNode[]];
and it will check like one may expect:
const hypertextNode: HypertextNode =
["div", { id: "parent" },
["div", { id: "first-child" }, "I'm the first child"],
["div", { id: "second-child" }, "I'm the second child"]
];
How it works
When we're creating a type reference, if we're looking to get a reference to a type alias type, we peek the circularity stack - if we see that we're already resolving that type, we return a (cached) "deferred substitution type". These substitution types can be handled exactly like any other substitution, the difference is in their implementation - their substitute field is not calculated until it is first accessed. (If this cuteness is a little over the top, a new type kind that functions like this could be added, but then we'd lose the free handling that already exists and would need to replumb a lot). So long as that substitute isn't immediately unwrapped, it can serve as a (reinstantiable!) placeholder for the type alias, functioning as a self-reference position.
I'm opening this mostly to advance the discussion from #32967. Much of what we saw as blockers on recursive type aliases has, at this point, been handled in some way already (since conditionals and indexed accesses have inadvertently allowed some of the same mechanics, over time). Provided we want to commit to it, I think it's certainly reasonable for us to handle circular type aliases without substantial architectural change.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
apieceofbart, dragomirtitian, markusjohnsson, Konstantin6487, AwesomeObserver, eilvelia, and millsp reacted with thumbs up emoji
AviVahl, alexrock, fatcerberus, dragomirtitian, hasparus, aleclarson, Konstantin6487, jwunderl, dead-claudia, and millsp reacted with hooray emoji
AnyhowStep, dragomirtitian, dubzzz, aleclarson, fatcerberus, pelotom, jwunderl, and yyx990803 reacted with heart emoji
All reactions
-
👍
7 reactions
-
🎉
10 reactions
-
❤️
8 reactions
|
cc Anders Hejlsberg (@ahejlsberg) so you can get an idea of what I've been going on about it not being that bad of a change if we don't want it to be.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Heya Wesley Wigham (@weswigham), I've started to run the parallelized Definitely Typed test suite on this PR at 6c9e85c. You can monitor the build here. It should now contribute to this PR's status checks.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Hey Wesley Wigham (@weswigham), 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/41288/artifacts?artifactName=tgz&fileId=282DB72299055B907AB20A7ED54753A4D316F6BCAA024E532660281DC96C42BF02&fileName=/typescript-3.7.0-insiders.20190821.tgz"
}
}
and then running npm install.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
saulshanabrook, weswigham, fatcerberus, AnyhowStep, dragomirtitian, dubzzz, Igorbek, and eilvelia reacted with laugh emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
DT looks like master's DT, as expected, this has no change in any extant non-erroring code~
|
AnyhowStep reacted with heart emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
How does this deal with conditional types? #26980
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Well, I mean, I already have a test in the PR that uses one inside of a tuple in an alias.... So...maybe? Haven't tried yet - so long as we're not immediately unwrapping the substitute before we manufacture the type (which we may do sometimes for conditionals to facilitate simplification), it might work.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Recursive conditional types would be great and make it possible to model, e.g. Array.prototype.flat with infinite depth (well, “infinite” up to the instantiation depth limit anyway):
// note: might not be 100% correct
type Flattened<T> = T extends (infer U)[][] ? Flattened<U[]> : T;
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Bruce Pascoe (@fatcerberus) If you're trying to model actually performing it at the type level, that'd be how you would do it (assuming this even handles that). But it does beg the question, if combined with #29317, given the following type Flatten<T>:
type Flatten<T extends any[]> =
{[P in keyof T]: T[P] extends Array<infer U> ? U : T[P]};
Would either or both of these be valid? (object is a supertype of any[])
// 1. covariant
const flat1: Array<object & not Array<any>> = get() as Flatten<object[]>;
// 2. contravariant
const flat1: Flatten<object[]> = get() as Array<object & not Array<any>>;
// Helper
declare function get(): any
Basically, does it follow math here or does it just break?
Edit: premature submission
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@isiahmeadows Yeah, I was trying to model the return type of [...].flat(Infinity). It's currently not possible to do so because of the ban on recursive type aliases.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Wesley Wigham (@weswigham)
The results of the perf run you requested are in!
Here they are:
Comparison Report - master..33018
| Metric |
master |
33018 |
Delta |
Best |
Worst |
| Angular - node (v12.1.0, x64) |
| Memory used |
325,554k (± 0.02%) |
325,879k (± 0.03%) |
+325k (+ 0.10%) |
325,668k |
326,068k |
| Parse Time |
1.48s (± 0.64%) |
1.48s (± 0.64%) |
-0.00s (- 0.00%) |
1.47s |
1.51s |
| Bind Time |
0.76s (± 0.81%) |
0.76s (± 0.81%) |
+0.00s (+ 0.26%) |
0.75s |
0.78s |
| Check Time |
4.19s (± 0.58%) |
4.22s (± 0.57%) |
+0.02s (+ 0.60%) |
4.17s |
4.27s |
| Emit Time |
5.25s (± 0.88%) |
5.29s (± 0.83%) |
+0.04s (+ 0.80%) |
5.14s |
5.36s |
| Total Time |
11.69s (± 0.54%) |
11.75s (± 0.55%) |
+0.07s (+ 0.58%) |
11.56s |
11.85s |
| Monaco - node (v12.1.0, x64) |
| Memory used |
345,878k (± 0.02%) |
345,940k (± 0.02%) |
+62k (+ 0.02%) |
345,781k |
346,063k |
| Parse Time |
1.22s (± 0.78%) |
1.22s (± 0.77%) |
-0.00s (- 0.33%) |
1.20s |
1.24s |
| Bind Time |
0.68s (± 0.50%) |
0.68s (± 0.76%) |
+0.00s (+ 0.59%) |
0.67s |
0.69s |
| Check Time |
4.27s (± 0.40%) |
4.25s (± 0.34%) |
-0.01s (- 0.33%) |
4.23s |
4.29s |
| Emit Time |
2.87s (± 0.77%) |
2.86s (± 0.37%) |
-0.01s (- 0.35%) |
2.83s |
2.87s |
| Total Time |
9.03s (± 0.34%) |
9.01s (± 0.22%) |
-0.02s (- 0.27%) |
8.96s |
9.05s |
| TFS - node (v12.1.0, x64) |
| Memory used |
301,359k (± 0.02%) |
301,393k (± 0.02%) |
+34k (+ 0.01%) |
301,210k |
301,505k |
| Parse Time |
0.95s (± 0.52%) |
0.95s (± 0.71%) |
-0.01s (- 0.73%) |
0.94s |
0.96s |
| Bind Time |
0.63s (± 1.53%) |
0.62s (± 0.76%) |
-0.01s (- 0.95%) |
0.62s |
0.64s |
| Check Time |
3.86s (± 0.58%) |
3.85s (± 0.58%) |
-0.00s (- 0.10%) |
3.81s |
3.90s |
| Emit Time |
2.96s (± 0.58%) |
2.96s (± 0.82%) |
-0.00s (- 0.00%) |
2.92s |
3.04s |
| Total Time |
8.40s (± 0.41%) |
8.39s (± 0.48%) |
-0.01s (- 0.15%) |
8.34s |
8.52s |
| Angular - node (v8.9.0, x64) |
| Memory used |
344,222k (± 0.01%) |
344,569k (± 0.02%) |
+347k (+ 0.10%) |
344,483k |
344,730k |
| Parse Time |
2.00s (± 1.18%) |
1.99s (± 0.57%) |
-0.01s (- 0.40%) |
1.96s |
2.02s |
| Bind Time |
0.82s (± 0.71%) |
0.82s (± 0.68%) |
+0.00s (+ 0.12%) |
0.81s |
0.83s |
| Check Time |
5.01s (± 0.49%) |
5.04s (± 0.79%) |
+0.03s (+ 0.64%) |
4.98s |
5.17s |
| Emit Time |
6.13s (± 0.38%) |
6.13s (± 0.62%) |
0.00s ( 0.00%) |
6.03s |
6.23s |
| Total Time |
13.96s (± 0.31%) |
13.98s (± 0.41%) |
+0.03s (+ 0.19%) |
13.88s |
14.12s |
| Monaco - node (v8.9.0, x64) |
| Memory used |
363,615k (± 0.01%) |
363,663k (± 0.01%) |
+48k (+ 0.01%) |
363,579k |
363,759k |
| Parse Time |
1.56s (± 0.38%) |
1.56s (± 0.48%) |
+0.00s (+ 0.06%) |
1.54s |
1.57s |
| Bind Time |
0.89s (± 0.50%) |
0.88s (± 0.51%) |
-0.01s (- 0.68%) |
0.87s |
0.89s |
| Check Time |
5.15s (± 2.16%) |
5.14s (± 1.50%) |
-0.02s (- 0.31%) |
4.99s |
5.27s |
| Emit Time |
3.13s (± 4.31%) |
3.13s (± 4.76%) |
+0.00s (+ 0.10%) |
2.90s |
3.34s |
| Total Time |
10.72s (± 0.66%) |
10.70s (± 0.75%) |
-0.02s (- 0.19%) |
10.52s |
10.84s |
| TFS - node (v8.9.0, x64) |
| Memory used |
317,606k (± 0.01%) |
317,600k (± 0.01%) |
-6k (- 0.00%) |
317,510k |
317,693k |
| Parse Time |
1.26s (± 0.54%) |
1.26s (± 0.51%) |
+0.00s (+ 0.16%) |
1.24s |
1.27s |
| Bind Time |
0.69s (± 4.68%) |
0.70s (± 5.45%) |
+0.01s (+ 1.02%) |
0.66s |
0.78s |
| Check Time |
4.46s (± 1.01%) |
4.43s (± 1.19%) |
-0.03s (- 0.63%) |
4.29s |
4.50s |
| Emit Time |
3.09s (± 0.52%) |
3.06s (± 0.67%) |
-0.03s (- 1.00%) |
2.99s |
3.10s |
| Total Time |
9.50s (± 0.38%) |
9.45s (± 0.30%) |
-0.05s (- 0.53%) |
9.36s |
9.50s |
| Angular - node (v8.9.0, x86) |
| Memory used |
194,996k (± 0.02%) |
195,212k (± 0.02%) |
+216k (+ 0.11%) |
195,130k |
195,331k |
| Parse Time |
1.93s (± 1.03%) |
1.93s (± 0.54%) |
-0.00s (- 0.10%) |
1.90s |
1.95s |
| Bind Time |
0.94s (± 0.61%) |
0.95s (± 0.77%) |
+0.01s (+ 0.85%) |
0.93s |
0.96s |
| Check Time |
4.58s (± 0.45%) |
4.60s (± 0.41%) |
+0.03s (+ 0.59%) |
4.57s |
4.64s |
| Emit Time |
5.84s (± 0.86%) |
5.80s (± 1.00%) |
-0.04s (- 0.62%) |
5.63s |
5.91s |
| Total Time |
13.29s (± 0.51%) |
13.29s (± 0.58%) |
-0.00s (- 0.02%) |
13.10s |
13.46s |
| Monaco - node (v8.9.0, x86) |
| Memory used |
203,173k (± 0.01%) |
203,238k (± 0.02%) |
+65k (+ 0.03%) |
203,192k |
203,335k |
| Parse Time |
1.61s (± 0.68%) |
1.62s (± 0.72%) |
+0.00s (+ 0.19%) |
1.60s |
1.65s |
| Bind Time |
0.72s (± 1.22%) |
0.72s (± 0.80%) |
-0.00s (- 0.41%) |
0.71s |
0.73s |
| Check Time |
4.88s (± 0.43%) |
4.86s (± 0.49%) |
-0.02s (- 0.45%) |
4.82s |
4.93s |
| Emit Time |
3.19s (± 0.53%) |
3.17s (± 0.70%) |
-0.01s (- 0.35%) |
3.11s |
3.22s |
| Total Time |
10.40s (± 0.28%) |
10.37s (± 0.21%) |
-0.04s (- 0.35%) |
10.32s |
10.43s |
| TFS - node (v8.9.0, x86) |
| Memory used |
178,481k (± 0.02%) |
178,522k (± 0.01%) |
+41k (+ 0.02%) |
178,483k |
178,564k |
| Parse Time |
1.31s (± 0.58%) |
1.32s (± 0.75%) |
+0.00s (+ 0.30%) |
1.29s |
1.34s |
| Bind Time |
0.64s (± 1.16%) |
0.64s (± 0.63%) |
-0.00s (- 0.16%) |
0.63s |
0.65s |
| Check Time |
4.29s (± 0.56%) |
4.31s (± 0.52%) |
+0.02s (+ 0.44%) |
4.27s |
4.37s |
| Emit Time |
2.86s (± 1.01%) |
2.84s (± 0.82%) |
-0.02s (- 0.66%) |
2.77s |
2.87s |
| Total Time |
9.10s (± 0.44%) |
9.11s (± 0.44%) |
+0.00s (+ 0.03%) |
9.01s |
9.20s |
| Angular - node (v9.0.0, x64) |
| Memory used |
343,776k (± 0.01%) |
344,149k (± 0.02%) |
+372k (+ 0.11%) |
344,011k |
344,347k |
| Parse Time |
1.72s (± 0.34%) |
1.71s (± 0.55%) |
-0.01s (- 0.58%) |
1.69s |
1.73s |
| Bind Time |
0.76s (± 1.16%) |
0.76s (± 0.78%) |
+0.00s (+ 0.13%) |
0.75s |
0.78s |
| Check Time |
4.74s (± 1.10%) |
4.77s (± 0.58%) |
+0.03s (+ 0.61%) |
4.74s |
4.86s |
| Emit Time |
5.71s (± 2.22%) |
5.71s (± 1.68%) |
-0.00s (- 0.05%) |
5.46s |
5.88s |
| Total Time |
12.93s (± 1.02%) |
12.95s (± 0.73%) |
+0.02s (+ 0.12%) |
12.77s |
13.14s |
| Monaco - node (v9.0.0, x64) |
| Memory used |
363,435k (± 0.01%) |
363,470k (± 0.02%) |
+34k (+ 0.01%) |
363,312k |
363,740k |
| Parse Time |
1.32s (± 0.72%) |
1.32s (± 0.51%) |
0.00s ( 0.00%) |
1.31s |
1.34s |
| Bind Time |
0.83s (± 1.13%) |
0.83s (± 1.39%) |
+0.01s (+ 0.60%) |
0.81s |
0.86s |
| Check Time |
5.06s (± 1.76%) |
5.01s (± 1.75%) |
-0.05s (- 1.03%) |
4.87s |
5.18s |
| Emit Time |
3.04s (± 5.42%) |
3.16s (± 5.47%) |
+0.12s (+ 4.09%) |
2.86s |
3.40s |
| Total Time |
10.24s (± 0.99%) |
10.32s (± 0.97%) |
+0.08s (+ 0.75%) |
10.10s |
10.48s |
| TFS - node (v9.0.0, x64) |
| Memory used |
317,428k (± 0.01%) |
317,445k (± 0.02%) |
+17k (+ 0.01%) |
317,362k |
317,601k |
| Parse Time |
1.04s (± 0.59%) |
1.04s (± 0.74%) |
0.00s ( 0.00%) |
1.03s |
1.06s |
| Bind Time |
0.62s (± 0.72%) |
0.62s (± 0.55%) |
-0.01s (- 0.80%) |
0.61s |
0.62s |
| Check Time |
4.38s (± 0.42%) |
4.37s (± 0.36%) |
-0.01s (- 0.25%) |
4.33s |
4.41s |
| Emit Time |
3.20s (± 0.59%) |
3.18s (± 0.52%) |
-0.01s (- 0.44%) |
3.13s |
3.21s |
| Total Time |
9.24s (± 0.26%) |
9.21s (± 0.26%) |
-0.03s (- 0.30%) |
9.16s |
9.26s |
| Angular - node (v9.0.0, x86) |
| Memory used |
195,045k (± 0.02%) |
195,225k (± 0.03%) |
+181k (+ 0.09%) |
195,119k |
195,372k |
| Parse Time |
1.64s (± 0.58%) |
1.63s (± 0.83%) |
-0.01s (- 0.43%) |
1.60s |
1.66s |
| Bind Time |
0.89s (± 1.00%) |
0.89s (± 0.41%) |
-0.01s (- 0.89%) |
0.88s |
0.89s |
| Check Time |
4.24s (± 0.37%) |
4.27s (± 0.68%) |
+0.02s (+ 0.57%) |
4.21s |
4.34s |
| Emit Time |
5.52s (± 0.85%) |
5.52s (± 0.80%) |
+0.00s (+ 0.02%) |
5.41s |
5.62s |
| Total Time |
12.29s (± 0.37%) |
12.30s (± 0.58%) |
+0.01s (+ 0.08%) |
12.12s |
12.47s |
| Monaco - node (v9.0.0, x86) |
| Memory used |
203,229k (± 0.01%) |
203,274k (± 0.01%) |
+45k (+ 0.02%) |
203,217k |
203,351k |
| Parse Time |
1.35s (± 0.63%) |
1.35s (± 0.89%) |
-0.00s (- 0.15%) |
1.33s |
1.39s |
| Bind Time |
0.64s (± 0.90%) |
0.65s (± 2.78%) |
+0.01s (+ 0.93%) |
0.63s |
0.72s |
| Check Time |
4.68s (± 0.38%) |
4.70s (± 0.45%) |
+0.02s (+ 0.34%) |
4.66s |
4.76s |
| Emit Time |
3.10s (± 0.74%) |
3.10s (± 0.70%) |
+0.00s (+ 0.16%) |
3.06s |
3.16s |
| Total Time |
9.77s (± 0.34%) |
9.79s (± 0.41%) |
+0.03s (+ 0.29%) |
9.74s |
9.89s |
| TFS - node (v9.0.0, x86) |
| Memory used |
178,567k (± 0.02%) |
178,597k (± 0.01%) |
+30k (+ 0.02%) |
178,546k |
178,642k |
| Parse Time |
1.07s (± 1.47%) |
1.06s (± 0.46%) |
-0.01s (- 0.65%) |
1.05s |
1.07s |
| Bind Time |
0.58s (± 0.81%) |
0.58s (± 1.21%) |
-0.00s (- 0.17%) |
0.57s |
0.60s |
| Check Time |
4.15s (± 0.27%) |
4.12s (± 0.44%) |
-0.02s (- 0.58%) |
4.09s |
4.17s |
| Emit Time |
2.78s (± 0.89%) |
2.79s (± 0.63%) |
+0.00s (+ 0.07%) |
2.73s |
2.81s |
| Total Time |
8.58s (± 0.51%) |
8.55s (± 0.34%) |
-0.03s (- 0.37%) |
8.49s |
8.63s |
System
| Machine Name | ts-ci-ubuntu |
| Platform | linux 4.4.0-142-generic |
| Architecture | x64 |
| Available Memory | 16 GB |
| Available Memory | 1 GB |
| CPUs | 4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz |
Hosts
- node (v12.1.0, x64)
- node (v8.9.0, x64)
- node (v8.9.0, x86)
- node (v9.0.0, x64)
- node (v9.0.0, x86)
Scenarios
- Angular - node (v12.1.0, x64)
- Angular - node (v8.9.0, x64)
- Angular - node (v8.9.0, x86)
- Angular - node (v9.0.0, x64)
- Angular - node (v9.0.0, x86)
- Monaco - node (v12.1.0, x64)
- Monaco - node (v8.9.0, x64)
- Monaco - node (v8.9.0, x86)
- Monaco - node (v9.0.0, x64)
- Monaco - node (v9.0.0, x86)
- TFS - node (v12.1.0, x64)
- TFS - node (v8.9.0, x64)
- TFS - node (v8.9.0, x86)
- TFS - node (v9.0.0, x64)
- TFS - node (v9.0.0, x86)
| Benchmark |
Name |
Iterations |
| Current |
33018 |
10 |
| Baseline |
master |
10 |
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
We'll be building this feature off of #33050 now which includes some parts of this~
|
millsp and karol-majewski reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Milestone
No milestone
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.
|