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

const modifier on type parameters #51865

Merged
merged 15 commits into from
Dec 16, 2022
Merged

const modifier on type parameters #51865

merged 15 commits into from
Dec 16, 2022

Conversation

ahejlsberg
Copy link
Member

@ahejlsberg ahejlsberg commented Dec 12, 2022

With this PR we implement a new const modifier for type parameters. In a function, method, or constructor invocation, when a literal expression in an argument is contextually typed by a const type parameter, the literal expression is given the most precise type possible, similar to having applied an as const assertion (see #29510). The kinds of literal expressions affected by a const contextual type are string, numeric, and boolean literal values, array literals, and object literals.

The const modifier is permitted on type parameters of functions, methods, and classes. It is an error to apply the const modifier to type parameters of interfaces and type aliases.

Given the function declarations

declare function foo<const T>(x: T): T;
declare function bar<const T>(obj: [T, T]): T;

the invocations

const x1 = foo('a');
const x2 = foo(['a', ['b', 'c']]);
const x3 = foo({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] });
const x4 = bar([{ a: 1, b: 'x' }, { a: 2, b: 'y' }]);

infer the types

declare const x1: "a";
declare const x2: readonly ["a", readonly ["b", "c"]];
declare const x3: {
    readonly a: 1;
    readonly b: "c";
    readonly d: readonly ["e", 2, true, { readonly f: "g"; }];
};
declare const x4:
    { readonly a: 1; readonly b: "x"; } |
    { readonly a: 2; readonly b: "y"; };

A const type parameter has no effect on variables or other kinds of expressions used as function arguments. For example:

const a = [1, 2, 3];  // number[]
const b = foo(a);  // number[]

The example above still requires writing [1, 2, 3] as const to infer type readonly [1, 2, 3] for a.

When a const type parameter is constrained to an array type, that array type should include a readonly modifier; otherwise, inferences for the type parameter will fail to meet the constraint. For example:

declare function baz<const T extends readonly unknown[]>(...args: T): T;

const z = baz(1, 'b', { a: 1, b: 'x' });  // readonly [1, "b", { readonly a: 1; readonly b: "x"; }]

Without the readonly modifier in the constraint, inference defaults to unknown[] because an inferred readonly tuple type wouldn't be assignable to unknown[].

Fixes #30680.
Fixes #41114.

@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Dec 12, 2022
@ahejlsberg ahejlsberg added this to the TypeScript 5.0.0 milestone Dec 12, 2022
@ahejlsberg
Copy link
Member Author

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Dec 12, 2022

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Dec 12, 2022

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Dec 12, 2022

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Dec 12, 2022

Heya @ahejlsberg, I've started to run the diff-based user code test suite on this PR at ccf252f. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Dec 12, 2022

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

@typescript-bot
Copy link
Collaborator

@ahejlsberg Here are the results of running the user test suite comparing main and refs/pull/51865/merge:

Everything looks good!

@typescript-bot
Copy link
Collaborator

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

Here they are:

Comparison Report - main..51865

Metric main 51865 Delta Best Worst
Angular - node (v16.17.1, x64)
Memory used 340,534k (± 0.01%) 339,574k (± 0.02%) -960k (- 0.28%) 339,383k 339,666k
Parse Time 1.88s (± 0.83%) 1.88s (± 0.53%) +0.01s (+ 0.32%) 1.86s 1.91s
Bind Time 0.65s (± 0.95%) 0.64s (± 0.56%) -0.01s (- 0.77%) 0.64s 0.65s
Check Time 5.17s (± 0.60%) 5.14s (± 0.52%) -0.03s (- 0.50%) 5.07s 5.18s
Emit Time 5.15s (± 0.73%) 5.05s (± 0.64%) -0.10s (- 1.88%) 4.97s 5.13s
Total Time 12.84s (± 0.41%) 12.72s (± 0.16%) -0.12s (- 0.95%) 12.67s 12.76s
Compiler-Unions - node (v16.17.1, x64)
Memory used 187,013k (± 0.40%) 186,475k (± 0.57%) -538k (- 0.29%) 185,554k 189,322k
Parse Time 0.79s (± 0.76%) 0.79s (± 0.82%) +0.01s (+ 0.89%) 0.78s 0.81s
Bind Time 0.42s (± 1.12%) 0.42s (± 1.61%) +0.00s (+ 0.95%) 0.41s 0.44s
Check Time 6.06s (± 0.64%) 5.96s (± 0.57%) -0.10s (- 1.72%) 5.89s 6.03s
Emit Time 1.94s (± 0.53%) 1.94s (± 1.18%) -0.00s (- 0.15%) 1.91s 2.01s
Total Time 9.21s (± 0.46%) 9.11s (± 0.46%) -0.10s (- 1.04%) 9.01s 9.19s
Monaco - node (v16.17.1, x64)
Memory used 319,807k (± 0.05%) 318,953k (± 0.04%) -854k (- 0.27%) 318,804k 319,496k
Parse Time 1.42s (± 0.47%) 1.41s (± 0.71%) -0.00s (- 0.21%) 1.39s 1.43s
Bind Time 0.59s (± 0.88%) 0.59s (± 0.68%) 0.00s ( 0.00%) 0.58s 0.60s
Check Time 4.87s (± 0.45%) 4.92s (± 0.50%) +0.05s (+ 1.05%) 4.87s 4.97s
Emit Time 2.73s (± 0.60%) 2.70s (± 0.90%) -0.03s (- 0.95%) 2.66s 2.75s
Total Time 9.61s (± 0.37%) 9.64s (± 0.47%) +0.03s (+ 0.30%) 9.55s 9.72s
TFS - node (v16.17.1, x64)
Memory used 282,303k (± 0.01%) 281,251k (± 0.01%) -1,053k (- 0.37%) 281,203k 281,299k
Parse Time 1.15s (± 0.65%) 1.17s (± 0.79%) +0.02s (+ 1.65%) 1.16s 1.20s
Bind Time 0.65s (± 5.76%) 0.63s (± 5.63%) 🟩-0.02s (- 3.52%) 0.56s 0.70s
Check Time 4.78s (± 0.23%) 4.69s (± 0.39%) -0.09s (- 1.90%) 4.66s 4.75s
Emit Time 2.79s (± 1.77%) 2.70s (± 1.74%) 🟩-0.09s (- 3.22%) 2.64s 2.88s
Total Time 9.38s (± 0.75%) 9.19s (± 0.77%) -0.18s (- 1.96%) 9.06s 9.40s
material-ui - node (v16.17.1, x64)
Memory used 435,359k (± 0.00%) 435,377k (± 0.01%) +18k (+ 0.00%) 435,325k 435,506k
Parse Time 1.63s (± 0.53%) 1.63s (± 0.50%) -0.00s (- 0.12%) 1.61s 1.65s
Bind Time 0.51s (± 0.88%) 0.51s (± 1.02%) +0.00s (+ 0.59%) 0.50s 0.52s
Check Time 11.86s (± 1.06%) 12.12s (± 0.91%) +0.27s (+ 2.24%) 11.87s 12.35s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 14.00s (± 0.91%) 14.26s (± 0.79%) +0.26s (+ 1.88%) 13.98s 14.50s
xstate - node (v16.17.1, x64)
Memory used 516,191k (± 0.00%) 516,233k (± 0.01%) +41k (+ 0.01%) 516,159k 516,292k
Parse Time 2.28s (± 0.46%) 2.30s (± 0.41%) +0.02s (+ 0.74%) 2.28s 2.33s
Bind Time 0.84s (± 1.40%) 0.84s (± 2.13%) -0.00s (- 0.12%) 0.81s 0.90s
Check Time 1.37s (± 0.73%) 1.36s (± 0.59%) -0.01s (- 0.88%) 1.34s 1.38s
Emit Time 0.06s (± 0.00%) 0.06s (± 0.00%) 0.00s ( 0.00%) 0.06s 0.06s
Total Time 4.55s (± 0.43%) 4.55s (± 0.44%) +0.00s (+ 0.11%) 4.51s 4.61s
System
Machine Namets-ci-ubuntu
Platformlinux 5.4.0-131-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)
Benchmark Name Iterations
Current 51865 10
Baseline main 10

Developer Information:

Download Benchmark

@ahejlsberg
Copy link
Member Author

@typescript-bot perf test this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Dec 12, 2022

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

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

Everything looks good!

@typescript-bot
Copy link
Collaborator

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

Here they are:

Compiler

Comparison Report - main..51865
Metric main 51865 Delta Best Worst
Angular - node (v18.10.0, x64)
Memory used 341,047k (± 0.02%) 340,194k (± 0.01%) -854k (- 0.25%) 340,122k 340,259k
Parse Time 1.56s (± 0.74%) 1.57s (± 1.14%) +0.01s (+ 0.45%) 1.52s 1.62s
Bind Time 0.52s (± 1.07%) 0.53s (± 1.14%) +0.01s (+ 1.35%) 0.51s 0.54s
Check Time 4.01s (± 0.63%) 4.00s (± 0.74%) -0.01s (- 0.35%) 3.95s 4.06s
Emit Time 4.31s (± 0.74%) 4.25s (± 0.92%) -0.06s (- 1.37%) 4.16s 4.32s
Total Time 10.40s (± 0.40%) 10.34s (± 0.70%) -0.06s (- 0.58%) 10.22s 10.51s
Compiler-Unions - node (v18.10.0, x64)
Memory used 187,317k (± 1.10%) 187,612k (± 1.10%) +295k (+ 0.16%) 184,169k 189,955k
Parse Time 0.61s (± 1.09%) 0.62s (± 0.96%) +0.00s (+ 0.49%) 0.61s 0.63s
Bind Time 0.33s (± 1.01%) 0.33s (± 1.36%) -0.00s (- 0.61%) 0.32s 0.34s
Check Time 5.03s (± 0.60%) 4.91s (± 0.50%) -0.12s (- 2.44%) 4.86s 4.98s
Emit Time 1.55s (± 1.12%) 1.54s (± 0.76%) -0.01s (- 0.52%) 1.52s 1.57s
Total Time 7.52s (± 0.47%) 7.39s (± 0.40%) -0.13s (- 1.73%) 7.33s 7.45s
Monaco - node (v18.10.0, x64)
Memory used 320,414k (± 0.01%) 319,476k (± 0.02%) -937k (- 0.29%) 319,265k 319,591k
Parse Time 1.15s (± 0.67%) 1.16s (± 1.53%) +0.00s (+ 0.35%) 1.13s 1.21s
Bind Time 0.48s (± 1.92%) 0.49s (± 1.40%) +0.00s (+ 0.83%) 0.47s 0.50s
Check Time 3.85s (± 0.43%) 3.92s (± 0.56%) +0.07s (+ 1.87%) 3.87s 3.96s
Emit Time 2.24s (± 0.65%) 2.24s (± 1.11%) -0.01s (- 0.36%) 2.18s 2.29s
Total Time 7.72s (± 0.47%) 7.79s (± 0.40%) +0.07s (+ 0.89%) 7.73s 7.85s
TFS - node (v18.10.0, x64)
Memory used 283,688k (± 0.24%) 283,637k (± 0.15%) -52k (- 0.02%) 281,889k 283,958k
Parse Time 0.97s (± 2.73%) 0.95s (± 2.79%) -0.01s (- 1.55%) 0.92s 1.05s
Bind Time 0.46s (± 7.43%) 0.51s (± 7.92%) +0.05s (+10.65%) 0.43s 0.57s
Check Time 3.78s (± 0.52%) 3.71s (± 0.70%) -0.07s (- 1.80%) 3.67s 3.79s
Emit Time 2.22s (± 0.76%) 2.19s (± 0.72%) -0.04s (- 1.62%) 2.14s 2.21s
Total Time 7.43s (± 0.63%) 7.36s (± 0.57%) -0.07s (- 0.94%) 7.25s 7.45s
material-ui - node (v18.10.0, x64)
Memory used 436,019k (± 0.02%) 435,974k (± 0.01%) -45k (- 0.01%) 435,902k 436,162k
Parse Time 1.33s (± 0.70%) 1.32s (± 0.77%) -0.01s (- 0.68%) 1.30s 1.34s
Bind Time 0.49s (± 2.84%) 0.49s (± 1.00%) +0.01s (+ 1.86%) 0.48s 0.50s
Check Time 10.37s (± 0.55%) 10.58s (± 1.10%) +0.21s (+ 2.02%) 10.41s 10.88s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 12.18s (± 0.48%) 12.39s (± 0.98%) +0.21s (+ 1.72%) 12.22s 12.70s
xstate - node (v18.10.0, x64)
Memory used 518,648k (± 0.02%) 518,625k (± 0.02%) -23k (- 0.00%) 518,508k 518,889k
Parse Time 1.90s (± 0.68%) 1.89s (± 0.68%) -0.01s (- 0.42%) 1.87s 1.92s
Bind Time 0.72s (± 2.91%) 0.70s (± 2.83%) 🟩-0.03s (- 3.73%) 0.67s 0.77s
Check Time 1.05s (± 0.97%) 1.04s (± 1.10%) -0.01s (- 1.05%) 1.02s 1.06s
Emit Time 0.05s (± 0.00%) 0.05s (± 0.00%) 0.00s ( 0.00%) 0.05s 0.05s
Total Time 3.72s (± 0.76%) 3.68s (± 0.77%) -0.05s (- 1.21%) 3.62s 3.76s
Angular - node (v16.17.1, x64)
Memory used 340,534k (± 0.01%) 339,584k (± 0.02%) -949k (- 0.28%) 339,371k 339,682k
Parse Time 1.88s (± 0.83%) 1.87s (± 0.40%) -0.00s (- 0.16%) 1.86s 1.90s
Bind Time 0.65s (± 0.95%) 0.64s (± 0.53%) -0.01s (- 0.92%) 0.64s 0.65s
Check Time 5.17s (± 0.60%) 5.13s (± 0.48%) -0.04s (- 0.72%) 5.05s 5.17s
Emit Time 5.15s (± 0.73%) 5.08s (± 0.71%) -0.07s (- 1.34%) 5.00s 5.14s
Total Time 12.84s (± 0.41%) 12.73s (± 0.36%) -0.11s (- 0.87%) 12.62s 12.83s
Compiler-Unions - node (v16.17.1, x64)
Memory used 187,013k (± 0.40%) 186,814k (± 0.61%) -199k (- 0.11%) 185,776k 189,154k
Parse Time 0.79s (± 0.76%) 0.79s (± 0.95%) +0.00s (+ 0.25%) 0.77s 0.80s
Bind Time 0.42s (± 1.12%) 0.42s (± 0.89%) -0.01s (- 1.19%) 0.41s 0.42s
Check Time 6.06s (± 0.64%) 5.91s (± 0.52%) -0.15s (- 2.53%) 5.83s 5.97s
Emit Time 1.94s (± 0.53%) 1.92s (± 0.84%) -0.02s (- 0.98%) 1.90s 1.97s
Total Time 9.21s (± 0.46%) 9.03s (± 0.40%) -0.17s (- 1.88%) 8.93s 9.09s
Monaco - node (v16.17.1, x64)
Memory used 319,807k (± 0.05%) 318,821k (± 0.01%) -986k (- 0.31%) 318,746k 318,933k
Parse Time 1.42s (± 0.47%) 1.40s (± 0.54%) -0.01s (- 0.92%) 1.38s 1.42s
Bind Time 0.59s (± 0.88%) 0.59s (± 0.58%) -0.00s (- 0.68%) 0.58s 0.59s
Check Time 4.87s (± 0.45%) 4.91s (± 0.45%) +0.04s (+ 0.82%) 4.88s 4.96s
Emit Time 2.73s (± 0.60%) 2.69s (± 0.62%) -0.04s (- 1.50%) 2.64s 2.72s
Total Time 9.61s (± 0.37%) 9.60s (± 0.41%) -0.01s (- 0.14%) 9.51s 9.70s
TFS - node (v16.17.1, x64)
Memory used 282,303k (± 0.01%) 281,265k (± 0.01%) -1,038k (- 0.37%) 281,214k 281,317k
Parse Time 1.15s (± 0.65%) 1.16s (± 0.95%) +0.01s (+ 0.61%) 1.14s 1.19s
Bind Time 0.65s (± 5.76%) 0.63s (± 4.19%) 🟩-0.02s (- 3.37%) 0.57s 0.67s
Check Time 4.78s (± 0.23%) 4.68s (± 0.37%) -0.10s (- 2.03%) 4.65s 4.71s
Emit Time 2.79s (± 1.77%) 2.72s (± 2.14%) -0.07s (- 2.47%) 2.65s 2.88s
Total Time 9.38s (± 0.75%) 9.20s (± 0.79%) -0.18s (- 1.90%) 9.06s 9.39s
material-ui - node (v16.17.1, x64)
Memory used 435,359k (± 0.00%) 435,346k (± 0.01%) -12k (- 0.00%) 435,297k 435,414k
Parse Time 1.63s (± 0.53%) 1.63s (± 0.58%) -0.01s (- 0.31%) 1.61s 1.64s
Bind Time 0.51s (± 0.88%) 0.51s (± 1.19%) -0.00s (- 0.59%) 0.49s 0.52s
Check Time 11.86s (± 1.06%) 12.14s (± 0.68%) +0.29s (+ 2.41%) 11.97s 12.29s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 14.00s (± 0.91%) 14.27s (± 0.61%) +0.28s (+ 1.98%) 14.10s 14.44s
xstate - node (v16.17.1, x64)
Memory used 516,191k (± 0.00%) 516,160k (± 0.01%) -32k (- 0.01%) 516,087k 516,307k
Parse Time 2.28s (± 0.46%) 2.28s (± 0.44%) -0.01s (- 0.31%) 2.26s 2.30s
Bind Time 0.84s (± 1.40%) 0.82s (± 1.21%) -0.02s (- 1.91%) 0.80s 0.85s
Check Time 1.37s (± 0.73%) 1.35s (± 0.64%) -0.02s (- 1.31%) 1.34s 1.37s
Emit Time 0.06s (± 0.00%) 0.06s (± 0.00%) 0.00s ( 0.00%) 0.06s 0.06s
Total Time 4.55s (± 0.43%) 4.51s (± 0.32%) -0.04s (- 0.84%) 4.47s 4.53s
Angular - node (v14.15.1, x64)
Memory used 334,014k (± 0.00%) 333,005k (± 0.01%) -1,008k (- 0.30%) 332,956k 333,048k
Parse Time 2.04s (± 0.57%) 2.04s (± 0.67%) +0.00s (+ 0.05%) 2.01s 2.08s
Bind Time 0.69s (± 0.49%) 0.70s (± 0.68%) +0.00s (+ 0.58%) 0.69s 0.71s
Check Time 5.47s (± 0.29%) 5.47s (± 0.65%) -0.00s (- 0.04%) 5.41s 5.55s
Emit Time 5.36s (± 0.62%) 5.24s (± 0.55%) -0.11s (- 2.15%) 5.19s 5.34s
Total Time 13.56s (± 0.30%) 13.45s (± 0.23%) -0.11s (- 0.80%) 13.38s 13.51s
Compiler-Unions - node (v14.15.1, x64)
Memory used 183,370k (± 0.68%) 181,110k (± 0.42%) -2,260k (- 1.23%) 180,656k 184,199k
Parse Time 0.88s (± 0.76%) 0.89s (± 0.58%) +0.01s (+ 0.91%) 0.88s 0.90s
Bind Time 0.45s (± 1.09%) 0.46s (± 0.80%) +0.00s (+ 0.44%) 0.45s 0.46s
Check Time 6.38s (± 0.59%) 6.21s (± 0.73%) -0.17s (- 2.62%) 6.13s 6.34s
Emit Time 2.04s (± 0.78%) 2.02s (± 0.78%) -0.03s (- 1.32%) 1.99s 2.06s
Total Time 9.76s (± 0.47%) 9.57s (± 0.55%) -0.18s (- 1.89%) 9.49s 9.75s
Monaco - node (v14.15.1, x64)
Memory used 314,673k (± 0.04%) 313,670k (± 0.01%) -1,003k (- 0.32%) 313,591k 313,756k
Parse Time 1.56s (± 0.72%) 1.56s (± 0.22%) -0.00s (- 0.26%) 1.55s 1.56s
Bind Time 0.63s (± 0.47%) 0.63s (± 0.54%) +0.00s (+ 0.16%) 0.63s 0.64s
Check Time 5.17s (± 0.49%) 5.23s (± 0.45%) +0.06s (+ 1.12%) 5.18s 5.27s
Emit Time 2.87s (± 0.71%) 2.84s (± 0.88%) -0.03s (- 1.18%) 2.79s 2.90s
Total Time 10.24s (± 0.46%) 10.26s (± 0.41%) +0.02s (+ 0.19%) 10.17s 10.34s
TFS - node (v14.15.1, x64)
Memory used 279,378k (± 0.01%) 278,277k (± 0.01%) -1,102k (- 0.39%) 278,225k 278,319k
Parse Time 1.34s (± 1.74%) 1.32s (± 0.76%) -0.02s (- 1.34%) 1.30s 1.34s
Bind Time 0.59s (± 0.98%) 0.59s (± 0.76%) -0.00s (- 0.34%) 0.58s 0.60s
Check Time 5.10s (± 0.34%) 4.98s (± 0.38%) -0.12s (- 2.27%) 4.95s 5.02s
Emit Time 3.08s (± 0.72%) 3.04s (± 1.55%) -0.05s (- 1.56%) 2.87s 3.10s
Total Time 10.11s (± 0.44%) 9.93s (± 0.52%) -0.18s (- 1.77%) 9.77s 10.02s
material-ui - node (v14.15.1, x64)
Memory used 430,804k (± 0.01%) 430,786k (± 0.01%) -18k (- 0.00%) 430,715k 430,843k
Parse Time 1.86s (± 0.84%) 1.85s (± 0.40%) -0.01s (- 0.54%) 1.84s 1.87s
Bind Time 0.54s (± 0.69%) 0.53s (± 0.90%) -0.01s (- 1.50%) 0.52s 0.54s
Check Time 12.33s (± 0.79%) 12.48s (± 0.41%) +0.15s (+ 1.18%) 12.34s 12.60s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 14.73s (± 0.63%) 14.86s (± 0.38%) +0.13s (+ 0.87%) 14.72s 15.01s
xstate - node (v14.15.1, x64)
Memory used 504,601k (± 0.02%) 504,463k (± 0.01%) -138k (- 0.03%) 504,333k 504,599k
Parse Time 2.56s (± 0.35%) 2.57s (± 0.41%) +0.00s (+ 0.20%) 2.54s 2.59s
Bind Time 0.84s (± 0.66%) 0.84s (± 0.44%) -0.00s (- 0.24%) 0.83s 0.84s
Check Time 1.48s (± 0.65%) 1.46s (± 0.42%) -0.01s (- 0.95%) 1.45s 1.48s
Emit Time 0.07s (± 0.00%) 0.07s (± 0.00%) 0.00s ( 0.00%) 0.07s 0.07s
Total Time 4.94s (± 0.32%) 4.93s (± 0.19%) -0.01s (- 0.16%) 4.91s 4.95s
System
Machine Namets-ci-ubuntu
Platformlinux 5.4.0-131-generic
Architecturex64
Available Memory16 GB
Available Memory15 GB
CPUs4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Hosts
  • node (v18.10.0, x64)
  • node (v16.17.1, x64)
  • node (v14.15.1, x64)
Scenarios
  • Angular - node (v18.10.0, x64)
  • Angular - node (v16.17.1, x64)
  • Angular - node (v14.15.1, x64)
  • Compiler-Unions - node (v18.10.0, x64)
  • Compiler-Unions - node (v16.17.1, x64)
  • Compiler-Unions - node (v14.15.1, x64)
  • Monaco - node (v18.10.0, x64)
  • Monaco - node (v16.17.1, x64)
  • Monaco - node (v14.15.1, x64)
  • TFS - node (v18.10.0, x64)
  • TFS - node (v16.17.1, x64)
  • TFS - node (v14.15.1, x64)
  • material-ui - node (v18.10.0, x64)
  • material-ui - node (v16.17.1, x64)
  • material-ui - node (v14.15.1, x64)
  • xstate - node (v18.10.0, x64)
  • xstate - node (v16.17.1, x64)
  • xstate - node (v14.15.1, x64)
Benchmark Name Iterations
Current 51865 10
Baseline main 10

TSServer

Comparison Report - main..51865
Metric main 51865 Delta Best Worst
Compiler-UnionsTSServer - node (v18.10.0, x64)
Req 1 - updateOpen 1,056ms (± 0.76%) 1,051ms (± 1.16%) -4ms (- 0.39%) 1,028ms 1,084ms
Req 2 - geterr 2,586ms (± 0.69%) 2,531ms (± 0.52%) -55ms (- 2.14%) 2,514ms 2,581ms
Req 3 - references 166ms (± 0.73%) 160ms (± 0.94%) 🟩-6ms (- 3.62%) 156ms 162ms
Req 4 - navto 138ms (± 0.83%) 137ms (± 0.74%) -2ms (- 1.09%) 134ms 139ms
Req 5 - completionInfo count 1,356 (± 0.00%) 1,356 (± 0.00%) 0 ( 0.00%) 1,356 1,356
Req 5 - completionInfo 61ms (± 3.14%) 60ms (± 2.57%) -1ms (- 1.96%) 57ms 62ms
CompilerTSServer - node (v18.10.0, x64)
Req 1 - updateOpen 1,099ms (± 0.94%) 1,106ms (± 0.28%) +8ms (+ 0.72%) 1,100ms 1,112ms
Req 2 - geterr 1,601ms (± 0.54%) 1,574ms (± 0.52%) -27ms (- 1.69%) 1,560ms 1,597ms
Req 3 - references 169ms (± 1.15%) 163ms (± 0.46%) 🟩-6ms (- 3.60%) 162ms 165ms
Req 4 - navto 151ms (± 0.90%) 150ms (± 0.24%) -1ms (- 0.80%) 149ms 150ms
Req 5 - completionInfo count 1,518 (± 0.00%) 1,518 (± 0.00%) 0 ( 0.00%) 1,518 1,518
Req 5 - completionInfo 53ms (± 1.69%) 52ms (± 1.34%) -1ms (- 0.95%) 51ms 54ms
xstateTSServer - node (v18.10.0, x64)
Req 1 - updateOpen 1,508ms (± 0.98%) 1,505ms (± 0.92%) -3ms (- 0.21%) 1,478ms 1,538ms
Req 2 - geterr 555ms (± 0.84%) 554ms (± 0.62%) -2ms (- 0.27%) 547ms 561ms
Req 3 - references 59ms (± 2.47%) 58ms (± 2.21%) -0ms (- 0.68%) 57ms 63ms
Req 4 - navto 195ms (± 1.00%) 196ms (± 1.06%) +0ms (+ 0.20%) 192ms 202ms
Req 5 - completionInfo count 3,154 (± 0.00%) 3,154 (± 0.00%) 0 ( 0.00%) 3,154 3,154
Req 5 - completionInfo 214ms (± 1.40%) 211ms (± 1.03%) -3ms (- 1.49%) 205ms 217ms
Compiler-UnionsTSServer - node (v16.17.1, x64)
Req 1 - updateOpen 1,306ms (± 0.60%) 1,295ms (± 0.41%) -11ms (- 0.86%) 1,286ms 1,310ms
Req 2 - geterr 3,221ms (± 0.68%) 3,154ms (± 0.52%) -67ms (- 2.09%) 3,112ms 3,176ms
Req 3 - references 191ms (± 0.65%) 186ms (± 0.71%) -5ms (- 2.56%) 183ms 189ms
Req 4 - navto 151ms (± 0.72%) 153ms (± 1.04%) +2ms (+ 1.32%) 150ms 157ms
Req 5 - completionInfo count 1,356 (± 0.00%) 1,356 (± 0.00%) 0 ( 0.00%) 1,356 1,356
Req 5 - completionInfo 60ms (± 2.45%) 59ms (± 2.40%) -2ms (- 2.49%) 56ms 62ms
CompilerTSServer - node (v16.17.1, x64)
Req 1 - updateOpen 1,380ms (± 0.67%) 1,378ms (± 0.76%) -3ms (- 0.19%) 1,360ms 1,402ms
Req 2 - geterr 2,103ms (± 0.40%) 2,062ms (± 0.43%) -41ms (- 1.95%) 2,042ms 2,086ms
Req 3 - references 199ms (± 0.52%) 192ms (± 0.38%) 🟩-7ms (- 3.32%) 191ms 194ms
Req 4 - navto 166ms (± 1.09%) 165ms (± 0.90%) -2ms (- 0.96%) 162ms 168ms
Req 5 - completionInfo count 1,518 (± 0.00%) 1,518 (± 0.00%) 0 ( 0.00%) 1,518 1,518
Req 5 - completionInfo 58ms (± 3.32%) 55ms (± 1.26%) 🟩-3ms (- 5.87%) 53ms 56ms
xstateTSServer - node (v16.17.1, x64)
Req 1 - updateOpen 1,820ms (± 0.62%) 1,820ms (± 0.31%) -0ms (- 0.02%) 1,808ms 1,833ms
Req 2 - geterr 723ms (± 0.74%) 719ms (± 0.76%) -3ms (- 0.44%) 707ms 728ms
Req 3 - references 68ms (± 1.01%) 69ms (± 2.10%) +1ms (+ 2.07%) 67ms 73ms
Req 4 - navto 201ms (± 0.97%) 198ms (± 1.21%) -3ms (- 1.45%) 193ms 203ms
Req 5 - completionInfo count 3,154 (± 0.00%) 3,154 (± 0.00%) 0 ( 0.00%) 3,154 3,154
Req 5 - completionInfo 254ms (± 0.80%) 254ms (± 1.15%) -0ms (- 0.04%) 248ms 264ms
Compiler-UnionsTSServer - node (v14.15.1, x64)
Req 1 - updateOpen 1,449ms (± 0.45%) 1,451ms (± 0.31%) +1ms (+ 0.08%) 1,443ms 1,464ms
Req 2 - geterr 3,450ms (± 0.53%) 3,389ms (± 0.66%) -61ms (- 1.78%) 3,352ms 3,451ms
Req 3 - references 207ms (± 0.73%) 199ms (± 0.44%) 🟩-8ms (- 4.06%) 197ms 201ms
Req 4 - navto 164ms (± 0.88%) 163ms (± 0.91%) -1ms (- 0.67%) 160ms 166ms
Req 5 - completionInfo count 1,356 (± 0.00%) 1,356 (± 0.00%) 0 ( 0.00%) 1,356 1,356
Req 5 - completionInfo 58ms (± 2.17%) 61ms (± 6.58%) +3ms (+ 5.73%) 57ms 72ms
CompilerTSServer - node (v14.15.1, x64)
Req 1 - updateOpen 1,526ms (± 0.59%) 1,530ms (± 0.39%) +4ms (+ 0.28%) 1,519ms 1,542ms
Req 2 - geterr 2,291ms (± 0.41%) 2,242ms (± 0.21%) -49ms (- 2.15%) 2,231ms 2,250ms
Req 3 - references 216ms (± 1.11%) 211ms (± 0.82%) -5ms (- 2.46%) 207ms 215ms
Req 4 - navto 174ms (± 0.86%) 171ms (± 0.91%) -3ms (- 1.72%) 168ms 176ms
Req 5 - completionInfo count 1,518 (± 0.00%) 1,518 (± 0.00%) 0 ( 0.00%) 1,518 1,518
Req 5 - completionInfo 56ms (± 1.30%) 56ms (± 0.72%) +0ms (+ 0.18%) 55ms 57ms
xstateTSServer - node (v14.15.1, x64)
Req 1 - updateOpen 1,996ms (± 0.37%) 1,993ms (± 0.33%) -3ms (- 0.15%) 1,981ms 2,014ms
Req 2 - geterr 746ms (± 0.30%) 746ms (± 0.55%) -0ms (- 0.01%) 735ms 754ms
Req 3 - references 72ms (± 1.31%) 72ms (± 0.86%) -0ms (- 0.41%) 71ms 74ms
Req 4 - navto 218ms (± 0.55%) 218ms (± 0.40%) -0ms (- 0.09%) 217ms 220ms
Req 5 - completionInfo count 3,154 (± 0.00%) 3,154 (± 0.00%) 0 ( 0.00%) 3,154 3,154
Req 5 - completionInfo 270ms (± 0.56%) 270ms (± 0.76%) -1ms (- 0.19%) 267ms 277ms
System
Machine Namets-ci-ubuntu
Platformlinux 5.4.0-131-generic
Architecturex64
Available Memory16 GB
Available Memory15 GB
CPUs4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Hosts
  • node (v18.10.0, x64)
  • node (v16.17.1, x64)
  • node (v14.15.1, x64)
Scenarios
  • Compiler-UnionsTSServer - node (v18.10.0, x64)
  • Compiler-UnionsTSServer - node (v16.17.1, x64)
  • Compiler-UnionsTSServer - node (v14.15.1, x64)
  • CompilerTSServer - node (v18.10.0, x64)
  • CompilerTSServer - node (v16.17.1, x64)
  • CompilerTSServer - node (v14.15.1, x64)
  • xstateTSServer - node (v18.10.0, x64)
  • xstateTSServer - node (v16.17.1, x64)
  • xstateTSServer - node (v14.15.1, x64)
Benchmark Name Iterations
Current 51865 10
Baseline main 10

Startup

Comparison Report - main..51865
Metric main 51865 Delta Best Worst
tsc-startup - node (v16.17.1, x64)
Execution time 118.71ms (± 0.52%) 118.47ms (± 0.45%) -0.24ms (- 0.20%) 115.83ms 124.48ms
tsserver-startup - node (v16.17.1, x64)
Execution time 198.83ms (± 0.44%) 198.31ms (± 0.32%) -0.52ms (- 0.26%) 194.93ms 205.74ms
tsserverlibrary-startup - node (v16.17.1, x64)
Execution time 194.52ms (± 0.45%) 192.92ms (± 0.34%) -1.60ms (- 0.82%) 189.67ms 202.20ms
typescript-startup - node (v16.17.1, x64)
Execution time 178.06ms (± 0.38%) 178.61ms (± 0.42%) +0.55ms (+ 0.31%) 174.97ms 186.48ms
System
Machine Namets-ci-ubuntu
Platformlinux 5.4.0-131-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
  • tsc-startup - node (v16.17.1, x64)
  • tsserver-startup - node (v16.17.1, x64)
  • tsserverlibrary-startup - node (v16.17.1, x64)
  • typescript-startup - node (v16.17.1, x64)
Benchmark Name Iterations
Current 51865 10
Baseline main 10

Developer Information:

Download Benchmark

@RyanCavanaugh
Copy link
Member

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Dec 12, 2022

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Dec 12, 2022

Hey @RyanCavanaugh, 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/140211/artifacts?artifactName=tgz&fileId=CA3B45CC535782C19A9298B874660B26676FB269A6536FD945B1E1CBC0B5671E02&fileName=/typescript-5.0.0-insiders.20221212.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.0.0-pr-51865-14".;

@ahejlsberg
Copy link
Member Author

@typescript-bot perf test faster

@typescript-bot
Copy link
Collaborator

typescript-bot commented Dec 13, 2022

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

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

Here they are:

Comparison Report - main..51865

Metric main 51865 Delta Best Worst
Angular - node (v16.17.1, x64)
Memory used 340,488k (± 0.02%) 339,603k (± 0.02%) -886k (- 0.26%) 339,389k 339,673k
Parse Time 1.85s (± 0.55%) 1.87s (± 0.59%) +0.02s (+ 1.24%) 1.84s 1.89s
Bind Time 0.64s (± 0.90%) 0.64s (± 0.56%) +0.00s (+ 0.16%) 0.64s 0.65s
Check Time 5.16s (± 0.64%) 5.13s (± 0.41%) -0.03s (- 0.68%) 5.09s 5.17s
Emit Time 5.06s (± 1.14%) 5.09s (± 0.54%) +0.03s (+ 0.67%) 5.02s 5.16s
Total Time 12.71s (± 0.62%) 12.74s (± 0.41%) +0.02s (+ 0.20%) 12.64s 12.87s
Compiler-Unions - node (v16.17.1, x64)
Memory used 188,320k (± 0.66%) 186,500k (± 0.55%) -1,820k (- 0.97%) 185,754k 189,388k
Parse Time 0.79s (± 1.20%) 0.79s (± 0.94%) +0.01s (+ 0.76%) 0.78s 0.81s
Bind Time 0.42s (± 0.89%) 0.42s (± 1.23%) +0.01s (+ 1.45%) 0.41s 0.43s
Check Time 6.04s (± 0.51%) 5.98s (± 0.87%) -0.06s (- 1.06%) 5.85s 6.07s
Emit Time 1.94s (± 0.73%) 1.92s (± 0.77%) -0.01s (- 0.72%) 1.90s 1.97s
Total Time 9.19s (± 0.37%) 9.12s (± 0.66%) -0.07s (- 0.74%) 8.96s 9.22s
Monaco - node (v16.17.1, x64)
Memory used 319,729k (± 0.01%) 318,830k (± 0.01%) -899k (- 0.28%) 318,769k 318,926k
Parse Time 1.40s (± 0.50%) 1.42s (± 0.71%) +0.02s (+ 1.14%) 1.40s 1.44s
Bind Time 0.59s (± 0.76%) 0.59s (± 0.50%) +0.00s (+ 0.68%) 0.59s 0.60s
Check Time 4.85s (± 0.69%) 4.93s (± 0.37%) +0.08s (+ 1.59%) 4.87s 4.96s
Emit Time 2.71s (± 0.59%) 2.70s (± 0.60%) -0.01s (- 0.33%) 2.66s 2.73s
Total Time 9.55s (± 0.49%) 9.64s (± 0.39%) +0.09s (+ 0.98%) 9.53s 9.70s
TFS - node (v16.17.1, x64)
Memory used 282,309k (± 0.01%) 281,279k (± 0.00%) -1,031k (- 0.37%) 281,258k 281,296k
Parse Time 1.16s (± 0.94%) 1.16s (± 0.91%) +0.01s (+ 0.52%) 1.15s 1.20s
Bind Time 0.64s (± 4.33%) 0.65s (± 4.70%) +0.01s (+ 1.56%) 0.58s 0.70s
Check Time 4.74s (± 0.57%) 4.68s (± 0.39%) -0.06s (- 1.26%) 4.64s 4.72s
Emit Time 2.76s (± 2.15%) 2.74s (± 1.88%) -0.01s (- 0.51%) 2.63s 2.83s
Total Time 9.30s (± 0.68%) 9.24s (± 0.59%) -0.06s (- 0.62%) 9.08s 9.32s
material-ui - node (v16.17.1, x64)
Memory used 435,352k (± 0.01%) 435,351k (± 0.01%) -1k (- 0.00%) 435,292k 435,451k
Parse Time 1.61s (± 0.40%) 1.63s (± 0.61%) +0.02s (+ 1.30%) 1.62s 1.66s
Bind Time 0.50s (± 1.04%) 0.51s (± 1.09%) +0.01s (+ 1.40%) 0.50s 0.52s
Check Time 11.75s (± 0.58%) 12.14s (± 0.71%) +0.39s (+ 3.31%) 11.95s 12.30s
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) 0.00s ( NaN%) 0.00s 0.00s
Total Time 13.87s (± 0.51%) 14.29s (± 0.64%) +0.42s (+ 2.99%) 14.08s 14.46s
xstate - node (v16.17.1, x64)
Memory used 516,243k (± 0.01%) 516,312k (± 0.01%) +70k (+ 0.01%) 516,214k 516,489k
Parse Time 2.26s (± 0.29%) 2.29s (± 0.41%) +0.03s (+ 1.41%) 2.27s 2.31s
Bind Time 0.82s (± 0.75%) 0.83s (± 1.39%) +0.01s (+ 1.58%) 0.82s 0.88s
Check Time 1.35s (± 0.59%) 1.35s (± 0.81%) 0.00s ( 0.00%) 1.32s 1.37s
Emit Time 0.06s (± 0.00%) 0.06s (± 0.00%) 0.00s ( 0.00%) 0.06s 0.06s
Total Time 4.50s (± 0.27%) 4.54s (± 0.34%) +0.04s (+ 0.80%) 4.51s 4.59s
System
Machine Namets-ci-ubuntu
Platformlinux 5.4.0-131-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)
Benchmark Name Iterations
Current 51865 10
Baseline main 10

Developer Information:

Download Benchmark

@mon-jai
Copy link

mon-jai commented Dec 13, 2022

Could this fix #13923 too?

@Mlocik97
Copy link

Mlocik97 commented Feb 1, 2023

OK, I tried testing more, and it seems it works fine in CJS, but not ESM. I will try to provide repro soon.

@Mlocik97
Copy link

Mlocik97 commented Feb 3, 2023

hello, I tested it more, and found out why I got the error... TSC correctly handled that type, but VSCode for some reason didn't picked line:

"typescript.tsdk": "./node_modules/typescript/lib" when I had workspace opened.

So my issue is now solved.

@SalathielGenese
Copy link

SalathielGenese commented Feb 9, 2023

Hi @ahejlsberg ,

I have a hard time (from your initial example) discovering the rule that makes the inferred type of x4 (a union) vs. that of x2 (a tuple).

Thanks for helping here, please.

@Andarist
Copy link
Contributor

Andarist commented Feb 9, 2023

@SalathielGenese notice that both use different functions, x2 is the foo's result and x4 is the bar's result.

It's important to check what is the result of the same bar call without the const type parameter:

declare function bar<T>(obj: [T, T]): T;

const x4 = bar([{ a: 1, b: 'x' }, { a: 2, b: 'y' }]);
// ?^ const x4: { a: number; b: string; }

and what is the result of the same thing with a regular as const assertion:

declare function bar<T>(obj: [T, T]): T;

const x4 = bar([{ a: 1, b: 'x' } as const, { a: 2, b: 'y' } as const]);
// ^? const x4: { readonly a: 1; readonly b: "x"; } | { readonly a: 2; readonly b: "y"; }

We can see here that it's consistent with the existing behavior of const assertions. The union is allowed likely because both arguments have a common supertype or something like that (a call like this fails to typecheck: bar(['', 10]))

@Conaclos Conaclos mentioned this pull request Feb 22, 2023
2 tasks
dominikjasek added a commit to dominikjasek/required-properties that referenced this pull request Jun 16, 2023
@JavaScriptBach
Copy link

This is a great feature. Would you consider allowing it for type aliases as well?

Example use case: Automattic/mongoose#12782

@RyanCavanaugh
Copy link
Member

@JavaScriptBach huh? There's no straightforward correspondence from this feature to something that would apply to type aliases. const type parameters in generic functions change the interpretation of objects in expression-space, but type aliases operate entirely in type-space.

@JavaScriptBach
Copy link

@RyanCavanaugh My point is that I have generic type aliases that expect to take in const type parameters, e.g. something like

type Foo<T> = ...

It would be great to be able to type T as a const parameter instead of requiring all callers to specify as const. I can't speak for implementation, but I think supporting const type parameters for type aliases would be useful for the same reasons that they're useful today for functions, methods, and classes.

@RyanCavanaugh
Copy link
Member

It would be great to be able to type T as a const parameter instead of requiring all callers to specify as const

Again, not understanding. You can't write this, it's syntactically illegal:

type X = Foo<{ x: 3 } as const>;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Const Type Constraint Suggestion: Const contexts for generic type inference