Skip to content

Widen types assigned to auto-typed variables#3497

Open
Andarist wants to merge 2 commits intomicrosoft:mainfrom
Andarist:fix/auto-type-lack-of-widening
Open

Widen types assigned to auto-typed variables#3497
Andarist wants to merge 2 commits intomicrosoft:mainfrom
Andarist:fix/auto-type-lack-of-widening

Conversation

@Andarist
Copy link
Copy Markdown
Contributor

fixes #3491

@ahejlsberg
Copy link
Copy Markdown
Member

@typescript-bot test it

@typescript-bot
Copy link
Copy Markdown

typescript-bot commented Apr 22, 2026

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
test top400 ✅ Started 👀 Results
user test this ✅ Started 👀 Results
run dt ✅ Started
perf test this faster ✅ Started 👀 Results

@typescript-bot
Copy link
Copy Markdown

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

Here they are:

tsc

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
Compiler-Unions - native
Errors 4 4 ~ ~ ~ p=1.000 n=6
Symbols 81,818 (± 0.01%) 81,804 (± 0.02%) ~ 81,775 81,810 p=0.079 n=6
Types 98,821 98,821 ~ ~ ~ p=1.000 n=6
Memory Used 175,086k (± 0.07%) 175,040k (± 0.05%) ~ 174,968k 175,216k p=0.471 n=6
Memory Allocs 1,771,550 (± 0.02%) 1,771,671 (± 0.01%) ~ 1,771,436 1,772,026 p=0.471 n=6
Config Time 0.000s 0.000s ~ ~ ~ p=1.000 n=6
Parse Time 0.056s (± 5.57%) 0.057s (± 6.83%) ~ 0.054s 0.064s p=0.936 n=6
Bind Time 0s 0s ~ ~ ~ p=1.000 n=6
Check Time 0s 0s ~ ~ ~ p=1.000 n=6
Emit Time 0.856s (± 0.74%) 0.871s (± 1.01%) +0.015s (+ 1.73%) 0.860s 0.881s p=0.013 n=6
Total Time 0.913s (± 0.52%) 0.928s (± 1.14%) +0.015s (+ 1.70%) 0.917s 0.946s p=0.010 n=6
angular-1 - native
Errors 3 3 ~ ~ ~ p=1.000 n=6
Symbols 884,979 (± 0.10%) 884,934 (± 0.07%) ~ 883,759 885,351 p=0.810 n=6
Types 263,959 (± 0.00%) 263,958 (± 0.00%) ~ 263,958 263,959 p=0.248 n=6
Memory Used 829,023k (± 0.07%) 828,754k (± 0.09%) ~ 827,917k 829,784k p=0.689 n=6
Memory Allocs 6,719,629 (± 0.51%) 6,716,181 (± 0.25%) ~ 6,696,988 6,744,701 p=0.689 n=6
Config Time 0.029s (± 7.92%) 0.028s (± 5.75%) ~ 0.027s 0.031s p=0.383 n=6
Parse Time 0.233s (± 1.90%) 0.241s (± 5.54%) ~ 0.225s 0.256s p=0.290 n=6
Bind Time 0s 0s ~ ~ ~ p=1.000 n=6
Check Time 0s 0s ~ ~ ~ p=1.000 n=6
Emit Time 1.911s (± 2.27%) 1.925s (± 1.13%) ~ 1.900s 1.954s p=0.575 n=6
Total Time 2.174s (± 2.05%) 2.196s (± 1.48%) ~ 2.162s 2.230s p=0.748 n=6
mui-docs - native
Errors 3 3 ~ ~ ~ p=1.000 n=6
Symbols 4,308,625 4,308,626 +1 (+ 0.00%) ~ ~ p=0.001 n=6
Types 1,537,195 1,537,198 +3 (+ 0.00%) ~ ~ p=0.001 n=6
Memory Used 5,454,036k (± 0.04%) 5,456,344k (± 0.03%) ~ 5,454,731k 5,459,189k p=0.230 n=6
Memory Allocs 86,504,869 (±15.27%) 84,481,981 (±11.05%) ~ 73,316,615 101,497,663 p=1.000 n=6
Config Time 0.022s (± 5.35%) 0.023s (± 4.55%) ~ 0.021s 0.024s p=0.214 n=6
Parse Time 0.928s (±24.06%) 0.875s (±19.04%) ~ 0.726s 1.200s p=0.936 n=6
Bind Time 0.002s (±22.11%) 0.002s ~ ~ ~ p=0.174 n=6
Check Time 16.267s (± 1.17%) 16.237s (± 0.80%) ~ 16.139s 16.487s p=0.810 n=6
Emit Time 0.435s (± 3.70%) 0.444s (± 3.67%) ~ 0.421s 0.459s p=0.470 n=6
Total Time 18.133s (± 0.95%) 18.002s (± 1.51%) ~ 17.782s 18.442s p=0.230 n=6
self-build-src - native
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,396,661 1,396,664 +3 (+ 0.00%) ~ ~ p=0.001 n=6
Types 442,174 442,177 +3 (+ 0.00%) ~ ~ p=0.001 n=6
Memory Used 1,647,340k (± 0.21%) 1,648,737k (± 0.18%) ~ 1,644,871k 1,652,978k p=0.575 n=6
Memory Allocs 57,281,290 (± 0.06%) 57,266,484 (± 0.09%) ~ 57,201,898 57,332,305 p=0.748 n=6
Config Time 0.020s (±43.89%) 0.013s (±44.87%) ~ 0.006s 0.022s p=0.173 n=6
Parse Time 0.283s (± 7.31%) 0.288s (± 2.73%) ~ 0.274s 0.298s p=0.471 n=6
Bind Time 0.000s 0.000s ~ ~ ~ p=1.000 n=6
Check Time 2.310s (± 0.52%) 2.330s (± 1.01%) ~ 2.308s 2.375s p=0.078 n=6
Emit Time 0.219s (± 5.43%) 0.220s (± 2.93%) ~ 0.211s 0.230s p=0.520 n=6
Total Time 29.723s (± 1.01%) 29.834s (± 0.68%) ~ 29.492s 30.099s p=0.471 n=6
self-compiler - native
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 338,176 338,176 ~ ~ ~ p=1.000 n=6
Types 199,525 199,526 +1 (+ 0.00%) ~ ~ p=0.001 n=6
Memory Used 331,846k (± 0.05%) 331,775k (± 0.04%) ~ 331,597k 331,923k p=0.575 n=6
Memory Allocs 2,551,260 (± 0.03%) 2,550,834 (± 0.04%) ~ 2,549,594 2,552,763 p=0.471 n=6
Config Time 0.001s 0.001s ~ ~ ~ p=1.000 n=6
Parse Time 0.134s (± 3.41%) 0.133s (± 3.90%) ~ 0.127s 0.140s p=0.517 n=6
Bind Time 0.000s 0.000s ~ ~ ~ p=1.000 n=6
Check Time 1.394s (± 0.81%) 1.396s (± 0.85%) ~ 1.379s 1.409s p=0.748 n=6
Emit Time 0.091s (±13.04%) 0.080s (± 9.03%) ~ 0.074s 0.091s p=0.090 n=6
Total Time 1.669s (± 0.84%) 1.658s (± 0.69%) ~ 1.647s 1.678s p=0.298 n=6
ts-pre-modules - native
Errors 3 3 ~ ~ ~ p=1.000 n=6
Symbols 752 752 ~ ~ ~ p=1.000 n=6
Types 356 356 ~ ~ ~ p=1.000 n=6
Memory Used 104,603k (± 0.04%) 104,607k (± 0.04%) ~ 104,550k 104,664k p=0.936 n=6
Memory Allocs 111,133 (± 0.31%) 111,036 (± 0.25%) ~ 110,584 111,378 p=0.810 n=6
Config Time 0.001s (±48.94%) 0.001s (±48.94%) ~ 0.000s 0.001s p=1.000 n=6
Parse Time 0.123s (± 6.01%) 0.117s (± 4.29%) ~ 0.113s 0.124s p=0.103 n=6
Bind Time 0s 0s ~ ~ ~ p=1.000 n=6
Check Time 0s 0s ~ ~ ~ p=1.000 n=6
Emit Time 0.000s 0.000s ~ ~ ~ p=1.000 n=6
Total Time 0.124s (± 5.96%) 0.118s (± 4.25%) ~ 0.114s 0.125s p=0.103 n=6
vscode - native
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 6,026,217 6,026,143 -74 (- 0.00%) ~ ~ p=0.001 n=6
Types 2,218,346 2,218,359 +13 (+ 0.00%) ~ ~ p=0.001 n=6
Memory Used 4,081,708k (± 0.02%) 4,083,576k (± 0.06%) ~ 4,081,632k 4,086,925k p=0.471 n=6
Memory Allocs 29,439,929 (± 0.08%) 29,436,105 (± 0.05%) ~ 29,420,622 29,459,889 p=1.000 n=6
Config Time 0.123s (± 7.77%) 0.127s (± 2.41%) ~ 0.122s 0.130s p=0.468 n=6
Parse Time 0.709s (± 4.57%) 0.709s (± 3.25%) ~ 0.681s 0.738s p=0.689 n=6
Bind Time 0.133s (±36.78%) 0.129s (±19.16%) ~ 0.111s 0.177s p=0.468 n=6
Check Time 7.883s (± 1.70%) 7.882s (± 1.31%) ~ 7.769s 8.062s p=1.000 n=6
Emit Time 1.843s (±12.49%) 1.783s (±11.24%) ~ 1.645s 2.124s p=0.936 n=6
Total Time 10.706s (± 1.15%) 10.653s (± 1.31%) ~ 10.516s 10.894s p=0.689 n=6
webpack - native
Errors 944 945 +1 (+ 0.11%) ~ ~ p=0.001 n=6
Symbols 695,186 695,185 -1 (- 0.00%) ~ ~ p=0.001 n=6
Types 332,898 332,913 +15 (+ 0.00%) ~ ~ p=0.001 n=6
Memory Used 547,335k (± 0.03%) 547,275k (± 0.03%) ~ 546,967k 547,488k p=0.575 n=6
Memory Allocs 4,152,894 (± 0.12%) 4,156,619 (± 0.23%) ~ 4,144,254 4,172,863 p=0.575 n=6
Config Time 0.013s (±22.81%) 0.015s (±21.23%) ~ 0.010s 0.018s p=0.625 n=6
Parse Time 0.140s (± 6.52%) 0.145s (± 3.35%) ~ 0.140s 0.152s p=0.090 n=6
Bind Time 0.033s (±23.99%) 0.031s (±28.15%) ~ 0.023s 0.042s p=0.629 n=6
Check Time 1.096s (± 0.80%) 1.096s (± 1.12%) ~ 1.078s 1.110s p=1.000 n=6
Emit Time 0.001s (±38.69%) 0.001s (±34.96%) ~ 0.001s 0.002s p=0.595 n=6
Total Time 1.303s (± 1.30%) 1.305s (± 1.42%) ~ 1.291s 1.332s p=1.000 n=6
xstate-main - native
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 991,654 991,654 ~ ~ ~ p=1.000 n=6
Types 350,842 350,843 +1 (+ 0.00%) ~ ~ p=0.001 n=6
Memory Used 603,337k (± 0.01%) 603,335k (± 0.01%) ~ 603,252k 603,378k p=0.936 n=6
Memory Allocs 4,948,795 (± 0.11%) 4,949,532 (± 0.24%) ~ 4,935,544 4,969,490 p=0.936 n=6
Config Time 0.007s (±25.25%) 0.008s (±20.43%) ~ 0.005s 0.009s p=0.155 n=6
Parse Time 0.133s (± 5.26%) 0.131s (± 4.07%) ~ 0.124s 0.136s p=0.810 n=6
Bind Time 0.032s (±20.09%) 0.042s (± 8.19%) 🔻+0.010s (+29.38%) 0.037s 0.047s p=0.016 n=6
Check Time 1.122s (± 1.88%) 1.131s (± 0.71%) ~ 1.118s 1.138s p=0.470 n=6
Emit Time 0.001s 0.001s ~ ~ ~ p=1.000 n=6
Total Time 1.300s (± 1.78%) 1.317s (± 0.74%) ~ 1.303s 1.327s p=0.335 n=6
System info unknown
Hosts
  • native
Scenarios
  • Compiler-Unions - native
  • angular-1 - native
  • mui-docs - native
  • self-build-src - native
  • self-compiler - native
  • ts-pre-modules - native
  • vscode - native
  • webpack - native
  • xstate-main - native
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

Developer Information:

Download Benchmarks

@typescript-bot
Copy link
Copy Markdown

@ahejlsberg Here are the results of running the user tests with tsc comparing main and refs/pull/3497/merge:

There were infrastructure failures potentially unrelated to your change:

  • 1 instance of "Git clone failed"

Otherwise...

Something interesting changed - please have a look.

Details

webpack

tsconfig.types.test.json

  • [NEW] error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.

tsconfig.types.json

  • [NEW] error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.

tsconfig.json

  • [NEW] error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.

@typescript-bot
Copy link
Copy Markdown

@ahejlsberg Here are the results of running the top 400 repos with tsc comparing main and refs/pull/3497/merge:

Something interesting changed - please have a look.

Details

RooCodeInc/Roo-Code

8 of 15 projects failed to build with the old tsc and were ignored

src/tsconfig.json

@ahejlsberg
Copy link
Copy Markdown
Member

Looks like we're fine on perf, but wondering what the changes in user tests and top 400 are about?

@Andarist
Copy link
Copy Markdown
Contributor Author

webpack repro

This one makes Record<string, CodeValue> | {} to be reduced to {} (where previously it would reduce to Record<string, CodeValue>). But note that {} is a special JSLiteral type that has an implicit index signature - widening loses some flags on the type (but IIRC from my debugging session it doesn't lose JSLiteral). And because the flags are different, the strict subtype relationship checks in the union subtype reduction change the result. I need to look a little bit more into this to understand the exact rules there.

But if we strip the fact this has a mix of different sources for the this.defaultValues and reduce it to one unannotated one, then we can see it does error in the very same way: TS playground

And we can further demo the difference between the subtype reduction rules here (TS playground):

/** @typedef {string | number | boolean} CodeValue */

/**
 * @param {Record<string, CodeValue>} arg
 */
function test(arg) {
	const obj = {};
	const result1 = Math.random() ? arg : obj
	//    ^? const result1: {}
	const result2 = Math.random() ? arg : {}
	//    ^? const result2: Record<string, CodeValue>

}

The original webpack break is really close to the result1, in my opinion. The final subtype reduction for a constructor-declared type happens in a "non-fresh" context. So I think this one is actually mildly surprising at first but correct.

RooCodeInc/Roo-Code repro

This one is weird. I didn't yet have the time to debug why this has changed. However, we can clearly see that result contains { success: boolean; error: string; } at all relevant control flow points. So the fact that it result.warnings access didn't error before this change feels just wrong.

So overall, I think both breaks are expected. I'll push relevant tests to this PR later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Excess-property check fires on a nested object literal that is later assigned to an annotated return type, where tsc 6.0 does not

3 participants