Skip to content

Support re-aliasing of type alias instantiations - #42284

Merged
ahejlsberg merged 5 commits into
masterfrom
newAliasesForInstantiatedTypes
Jan 11, 2021
Merged

Support re-aliasing of type alias instantiations#42284
ahejlsberg merged 5 commits into
masterfrom
newAliasesForInstantiatedTypes

Conversation

@ahejlsberg

Copy link
Copy Markdown
Member

This PR continues the work in #42149 by supporting re-aliasing of type alias instantiations. For example:

typeThing<T>={value: T};typeArrayThing<U>=Thing<U[]>;typeStringArrayThing=ArrayThing<string>;declarelett1: Thing<string[]>;// Thing<string[]>declarelett2: ArrayThing<string>;// ArrayThing<string> (previously Thing<string[]>)declarelett3: StringArrayThing;// StringArrayThing (previously Thing<string[]>)interfaceItem<T>{value: T}typeArrayItem<U>=Item<U[]>;typeNumberArrayItem=ArrayItem<number>;declarelett4: Item<number[]>;// Item<number[]>declarelett5: ArrayItem<number>;// ArrayItem<number>declarelett6: NumberArrayItem;// NumberArrayItem (previously ArrayItem<number>)functionfoo<T,KextendskeyofT>(obj: T,key: K){letx: Omit<T,K>;// Omit<T, K> (previously Pick<T, Exclude<keyof T, K>>)}

Previously, instantiations of aliased anonymous object, mapped, or conditional types with a new type alias would continue to display the original type alias in diagnostics, quick info, and declaration files. Now, re-aliasing of such types consistently adopts and displays the new type alias.

In addition, instantiations of distributive types (such as homomorphic mapped types and distributive conditional types) now properly preserve the original type alias when the instantiation results in a union type:

typeA={a: string};typeB={b: string};declareletp: Partial<A|B>;// Partial<A | B> (previously Partial<A> | Partial<B>)

@typescript-bottypescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Jan 11, 2021
@ahejlsberg

Copy link
Copy Markdown
MemberAuthor

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

@typescript-bot

typescript-bot commented Jan 11, 2021

Copy link
Copy Markdown
Contributor

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

Update: The results are in!

@typescript-bot

typescript-bot commented Jan 11, 2021

Copy link
Copy Markdown
Contributor

Heya @ahejlsberg, I've started to run the parallelized community code test suite on this PR at 8ac7030. You can monitor the build here.

@typescript-bot

typescript-bot commented Jan 11, 2021

Copy link
Copy Markdown
Contributor

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

@typescript-bot

typescript-bot commented Jan 11, 2021

Copy link
Copy Markdown
Contributor

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

@ahejlsberg

Copy link
Copy Markdown
MemberAuthor

@robpalme This is the continuation PR I mentioned in #42149. Give it a try and let me know how it goes.

@typescript-bot

Copy link
Copy Markdown
Contributor

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

Here they are:

Comparison Report - master..42284

Metricmaster42284DeltaBestWorst
Angular - node (v12.1.0, x64)
Memory used324,053k (± 0.02%)324,025k (± 0.02%)-28k (- 0.01%)323,871k324,182k
Parse Time1.98s (± 0.66%)1.99s (± 0.84%)+0.01s (+ 0.50%)1.95s2.01s
Bind Time0.81s (± 0.92%)0.82s (± 1.35%)+0.00s (+ 0.49%)0.81s0.86s
Check Time4.88s (± 0.33%)4.88s (± 0.58%)+0.01s (+ 0.21%)4.82s4.94s
Emit Time5.51s (± 0.77%)5.51s (± 0.61%)-0.00s (- 0.02%)5.46s5.59s
Total Time13.18s (± 0.43%)13.21s (± 0.50%)+0.03s (+ 0.21%)13.06s13.36s
Compiler-Unions - node (v12.1.0, x64)
Memory used200,247k (± 0.08%)200,294k (± 0.07%)+47k (+ 0.02%)199,982k200,507k
Parse Time0.78s (± 0.77%)0.77s (± 0.98%)-0.00s (- 0.00%)0.76s0.79s
Bind Time0.50s (± 0.66%)0.50s (± 1.04%)-0.00s (- 0.20%)0.49s0.51s
Check Time9.94s (± 1.06%)9.92s (± 0.79%)-0.02s (- 0.17%)9.75s10.15s
Emit Time2.37s (± 0.97%)2.36s (± 1.29%)-0.01s (- 0.42%)2.30s2.46s
Total Time13.58s (± 0.74%)13.55s (± 0.70%)-0.03s (- 0.23%)13.34s13.75s
Monaco - node (v12.1.0, x64)
Memory used337,446k (± 0.03%)337,473k (± 0.01%)+27k (+ 0.01%)337,335k337,541k
Parse Time1.59s (± 0.30%)1.58s (± 0.56%)-0.00s (- 0.31%)1.57s1.61s
Bind Time0.70s (± 1.08%)0.70s (± 0.64%)-0.01s (- 0.99%)0.69s0.71s
Check Time4.95s (± 0.46%)4.94s (± 0.43%)-0.01s (- 0.12%)4.88s4.98s
Emit Time2.88s (± 0.60%)2.89s (± 0.42%)+0.01s (+ 0.38%)2.86s2.91s
Total Time10.12s (± 0.24%)10.11s (± 0.36%)-0.01s (- 0.06%)9.99s10.16s
TFS - node (v12.1.0, x64)
Memory used292,356k (± 0.02%)292,396k (± 0.02%)+41k (+ 0.01%)292,282k292,533k
Parse Time1.25s (± 0.76%)1.25s (± 0.71%)-0.01s (- 0.64%)1.23s1.26s
Bind Time0.65s (± 0.73%)0.65s (± 0.86%)-0.01s (- 0.77%)0.64s0.66s
Check Time4.54s (± 0.70%)4.52s (± 0.54%)-0.02s (- 0.51%)4.47s4.58s
Emit Time2.95s (± 0.89%)2.94s (± 0.72%)-0.00s (- 0.14%)2.90s2.98s
Total Time9.40s (± 0.52%)9.36s (± 0.42%)-0.04s (- 0.37%)9.28s9.45s
material-ui - node (v12.1.0, x64)
Memory used478,950k (± 0.07%)473,781k (± 0.01%)-5,169k (- 1.08%)473,585k473,886k
Parse Time2.08s (± 0.73%)2.07s (± 0.54%)-0.02s (- 0.91%)2.04s2.09s
Bind Time0.65s (± 0.73%)0.64s (± 0.69%)-0.00s (- 0.77%)0.63s0.65s
Check Time12.49s (± 0.90%)12.51s (± 0.71%)+0.02s (+ 0.14%)12.28s12.72s
Emit Time0.00s (± 0.00%)0.00s (± 0.00%)0.00s ( NaN%)0.00s0.00s
Total Time15.23s (± 0.65%)15.22s (± 0.62%)-0.00s (- 0.03%)14.98s15.44s
System
Machine Namets-ci-ubuntu
Platformlinux 4.4.0-197-generic
Architecturex64
Available Memory16 GB
Available Memory4 GB
CPUs4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Hosts
  • node (v12.1.0, x64)
Scenarios
  • Angular - node (v12.1.0, x64)
  • Compiler-Unions - node (v12.1.0, x64)
  • Monaco - node (v12.1.0, x64)
  • TFS - node (v12.1.0, x64)
  • material-ui - node (v12.1.0, x64)
BenchmarkNameIterations
Current4228410
Baselinemaster10

@typescript-bot

Copy link
Copy Markdown
Contributor

The user suite test run you requested has finished and failed. I've opened a PR with the baseline diff from master.

@ahejlsberg

Copy link
Copy Markdown
MemberAuthor

Tests all look good, only changes are improved diagnostics. Performance is unaffected.

@DanielRosenwasser

Copy link
Copy Markdown
Member

@typescript-bot pack this

@typescript-bot

typescript-bot commented Jan 11, 2021

Copy link
Copy Markdown
Contributor

Heya @DanielRosenwasser, I've started to run the tarball bundle task on this PR at 8ac7030. You can monitor the build here.

@typescript-bot

typescript-bot commented Jan 11, 2021

Copy link
Copy Markdown
Contributor

Hey @DanielRosenwasser, 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/93139/artifacts?artifactName=tgz&fileId=4FE49FBE4115C3F38EC02DE524EF9087A6729DD9744C9B257007FE5D69F77D1202&fileName=/typescript-4.2.0-insiders.20210111.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@4.2.0-pr-42284-11".;

Comment on lines +209 to +214
!!! error TS2322: Type 'keyof T' is not assignable to type 'T[keyof T] extends Function ? never : keyof T'.
!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'T[keyof T] extends Function ? never : keyof T'.
!!! error TS2322: Type 'string' is not assignable to type 'T[keyof T] extends Function ? never : keyof T'.
!!! error TS2322: Type 'keyof T' is not assignable to type 'never'.
!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'never'.
!!! error TS2322: Type 'string' is not assignable to type 'never'.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why's this happening? What was avoiding the elaboration before?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sounds like we can't avoid re-elaboration based on type IDs

@ahejlsberg
ahejlsberg merged commit c456bbd into masterJan 11, 2021
@DanielRosenwasser
DanielRosenwasser deleted the newAliasesForInstantiatedTypes branch January 12, 2021 00:20
@KingwlKingwl mentioned this pull request Jan 12, 2021
10 tasks
@robpalme

robpalme commented Jan 12, 2021

Copy link
Copy Markdown

@ahejlsberg This is even better than the previous attempt 🎉

As before, there are no inlining/size regressions - I reviewed every changed line and this PR is a pure win. This PR improves upon 2c2d06d by also handling aliases to Record and Pick<obj, union>.

// Previous: 2c2d06
myRecordProp: Record<string,any>;
myObjectProp: Pick<{lotsOfProps,},"red"|"green"|"blue>;

...is replaced by...

// Master: Using PR#42284
myRecordProp: MyRecordType;
myObjectProp: MyColorsOnly<{lotsOfProps,}>;

On the large package I referred to in previousreports, this PR results in a further 10KB decrease vs the previous attempt, i.e the Record & Pick changes explains the 10KB win. Meaning the net effect of this PR is now:

  • 50 declaration files out of 394 were affected by this PR
  • Net size of all declaration files shrank by 41KB
    • 50 files shrank (total size of these files reduced by 41KB from 585KB to 544KB)
    • 0 files grew

Zzzen pushed a commit to Zzzen/TypeScript that referenced this pull request Jan 16, 2021
* New aliases for type alias instantiations
* New aliases for conditional, mapped, and anonymous object type instantiations
* Accept new baselines
* Fix issues with re-aliasing
* Accept new baselines
@DanielRosenwasserDanielRosenwasser added this to the TypeScript 4.2.0 milestone Jan 22, 2021
@microsoftmicrosoft locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: TeamFor Uncommitted BugPR for untriaged, rejected, closed or missing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@ahejlsberg@typescript-bot@DanielRosenwasser@robpalme