Skip to content

Fixed local extends constraints of infer type parameters when they can contain type variables - #60345

Closed
Mateusz Burzyński (Andarist) wants to merge 2 commits into
microsoft:mainfrom
Andarist:fix/infer-type-params-with-instantiatable-extends-constraints
Closed

Fixed local extends constraints of infer type parameters when they can contain type variables#60345
Mateusz Burzyński (Andarist) wants to merge 2 commits into
microsoft:mainfrom
Andarist:fix/infer-type-params-with-instantiatable-extends-constraints

Conversation

@Andarist

@AndaristMateusz Burzyński (Andarist) commented Oct 26, 2024

Copy link
Copy Markdown
Contributor

fixes#60299

this reverts a good chunk of #57362 but it also makes some modifications to the code that was removed there

cc Wesley Wigham (@weswigham)

@typescript-botTypeScript Bot (typescript-bot) added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Oct 26, 2024
@Andarist
Mateusz Burzyński (Andarist)force-pushed the fix/infer-type-params-with-instantiatable-extends-constraints branch from 659c426 to 7cf0d7cCompareOctober 26, 2024 19:36
Comment on lines +19227 to +19229
function maybeCloneInferTypeParameter(p: TypeParameter) {
return getConstraintDeclaration(p) && couldContainTypeVariables(getConstraintFromTypeParameter(p)!) ? cloneTypeParameter(p) : p;
}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

this only clones type parameters with "local" constraints (the ones added by infer R extends C syntax)

It could do the same with all constraints, using this patch:

git diff
diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts
index a8803f5823..52f44bcebe 100644
--- a/src/compiler/checker.ts+++ b/src/compiler/checker.ts@@ -19117,9 +19117,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}
const context = createInferenceContext(freshParams, /*signature*/ undefined, InferenceFlags.None);
- if (mapper) {- context.nonFixingMapper = combineTypeMappers(context.nonFixingMapper, mapper);- }
if (!checkTypeDeferred) {
// We don't want inferences from constraints as they may cause us to eagerly resolve the
// conditional type instead of deferring resolution. Also, we always want strict function
@@ -19220,7 +19217,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
function maybeCloneInferTypeParameter(p: TypeParameter) {
- return getConstraintDeclaration(p) && couldContainTypeVariables(getConstraintFromTypeParameter(p)!) ? cloneTypeParameter(p) : p;+ const constraint = getConstraintFromTypeParameter(p);+ return constraint && couldContainTypeVariables(constraint) ? cloneTypeParameter(p) : p;
}
function getTrueTypeFromConditionalType(type: ConditionalType) {
diff --git a/tests/baselines/reference/inferTypes1.types b/tests/baselines/reference/inferTypes1.types
index edc97a2ea1..f6018d54a3 100644
--- a/tests/baselines/reference/inferTypes1.types+++ b/tests/baselines/reference/inferTypes1.types@@ -1,5 +1,8 @@
//// [tests/cases/conformance/types/conditional/inferTypes1.ts] ////
+=== Performance Stats ===+Instantiation count: 1,000+
=== inferTypes1.ts ===
type Unpacked<T> =
>Unpacked : Unpacked<T>

As we can see, we could drop the context.nonFixingMapper manipulation and simplify the logic here - but that would create more type parameter clones and that's even caught by this diff (Instantiation count was reported by one of the tests when using this patch)

@zedryas

Copy link
Copy Markdown

thank you very much Mateusz Burzyński (@Andarist), how long do you reckon before this might be merged ?

@Andarist

Copy link
Copy Markdown
ContributorAuthor

seb (@zedryas) that's not up to me, we have to wait for the review and merge by one of the TS team members

@jakebailey

Copy link
Copy Markdown
Member

TypeScript Bot (@typescript-bot) test it

@typescript-bot

TypeScript Bot (typescript-bot) commented Oct 28, 2024

Copy link
Copy Markdown
Contributor

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

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

@typescript-bot

Copy link
Copy Markdown
Contributor

Jake Bailey (@jakebailey) Here are the results of running the user tests with tsc comparing main and refs/pull/60345/merge:

Everything looks good!

@typescript-bot

Copy link
Copy Markdown
Contributor

Hey Jake Bailey (@jakebailey), the results of running the DT tests are ready.

Everything looks the same!

You can check the log here.

@typescript-bot

Copy link
Copy Markdown
Contributor

Jake Bailey (@jakebailey)
The results of the perf run you requested are in!

Here they are:

tsc

Comparison Report - baseline..pr
MetricbaselineprDeltaBestWorstp-value
Compiler-Unions - node (v18.15.0, x64)
Errors3131~~~p=1.000 n=6
Symbols62,34062,340~~~p=1.000 n=6
Types50,37950,379~~~p=1.000 n=6
Memory used193,467k (± 0.76%)192,851k (± 0.07%)~192,763k193,126kp=0.471 n=6
Parse Time1.31s (± 0.80%)1.31s (± 1.12%)~1.29s1.32sp=0.557 n=6
Bind Time0.72s0.72s~~~p=1.000 n=6
Check Time9.75s (± 0.46%)9.74s (± 0.22%)~9.72s9.77sp=0.870 n=6
Emit Time2.72s (± 1.56%)2.71s (± 1.51%)~2.63s2.74sp=0.373 n=6
Total Time14.50s (± 0.39%)14.48s (± 0.30%)~14.41s14.53sp=0.810 n=6
angular-1 - node (v18.15.0, x64)
Errors3333~~~p=1.000 n=6
Symbols947,886947,886~~~p=1.000 n=6
Types410,840410,840~~~p=1.000 n=6
Memory used1,224,823k (± 0.01%)1,224,845k (± 0.01%)~1,224,779k1,224,926kp=0.575 n=6
Parse Time8.10s (± 0.83%)8.08s (± 0.77%)~8.01s8.17sp=0.630 n=6
Bind Time2.28s (± 0.66%)2.27s (± 0.82%)~2.25s2.29sp=0.365 n=6
Check Time37.86s (± 0.28%)37.93s (± 0.59%)~37.65s38.32sp=0.689 n=6
Emit Time18.30s (± 0.55%)18.27s (± 0.47%)~18.16s18.40sp=0.748 n=6
Total Time66.54s (± 0.20%)66.55s (± 0.41%)~66.23s66.96sp=0.936 n=6
mui-docs - node (v18.15.0, x64)
Errors00~~~p=1.000 n=6
Symbols2,494,6032,494,603~~~p=1.000 n=6
Types908,300908,300~~~p=1.000 n=6
Memory used2,307,228k (± 0.00%)2,307,217k (± 0.00%)~2,307,176k2,307,258kp=0.810 n=6
Parse Time9.32s (± 0.18%)9.34s (± 0.16%)+0.02s (+ 0.27%)9.32s9.36sp=0.036 n=6
Bind Time2.14s (± 0.46%)2.14s (± 0.75%)~2.11s2.15sp=0.720 n=6
Check Time74.77s (± 0.13%)74.96s (± 0.32%)~74.60s75.21sp=0.173 n=6
Emit Time0.28s (± 3.53%)0.29s (± 2.85%)~0.27s0.29sp=0.144 n=6
Total Time86.51s (± 0.13%)86.73s (± 0.29%)~86.36s87.01sp=0.128 n=6
self-build-src - node (v18.15.0, x64)
Errors00~~~p=1.000 n=6
Symbols1,258,0441,258,052+8 (+ 0.00%)~~p=0.001 n=6
Types266,229266,231+2 (+ 0.00%)~~p=0.001 n=6
Memory used2,422,962k (± 0.04%)2,422,861k (± 0.01%)~2,422,473k2,423,341kp=1.000 n=6
Parse Time5.22s (± 0.63%)5.22s (± 0.62%)~5.19s5.27sp=0.872 n=6
Bind Time1.93s (± 0.28%)1.93s (± 0.60%)~1.91s1.94sp=0.152 n=6
Check Time35.49s (± 0.23%)35.53s (± 0.25%)~35.40s35.65sp=0.521 n=6
Emit Time3.09s (± 2.54%)3.02s (± 1.82%)~2.93s3.08sp=0.173 n=6
Total Time45.73s (± 0.35%)45.71s (± 0.30%)~45.49s45.85sp=0.689 n=6
self-build-src-public-api - node (v18.15.0, x64)
Errors00~~~p=1.000 n=6
Symbols1,258,0441,258,052+8 (+ 0.00%)~~p=0.001 n=6
Types266,229266,231+2 (+ 0.00%)~~p=0.001 n=6
Memory used2,766,242k (±12.86%)2,619,337k (±11.21%)~2,497,875k3,219,236kp=0.689 n=6
Parse Time6.65s (± 2.47%)6.57s (± 1.56%)~6.47s6.74sp=0.521 n=6
Bind Time2.15s (± 1.74%)2.18s (± 2.76%)~2.14s2.30sp=0.226 n=6
Check Time43.15s (± 0.50%)43.11s (± 0.31%)~42.93s43.26sp=0.575 n=6
Emit Time3.54s (± 2.87%)3.63s (± 5.83%)~3.42s4.02sp=0.471 n=6
Total Time55.49s (± 0.51%)55.49s (± 0.39%)~55.28s55.85sp=0.810 n=6
self-compiler - node (v18.15.0, x64)
Errors00~~~p=1.000 n=6
Symbols261,754261,762+8 (+ 0.00%)~~p=0.001 n=6
Types106,477106,479+2 (+ 0.00%)~~p=0.001 n=6
Memory used438,571k (± 0.01%)438,587k (± 0.02%)~438,515k438,671kp=0.936 n=6
Parse Time2.89s (± 0.61%)2.89s (± 0.58%)~2.86s2.90sp=0.507 n=6
Bind Time1.10s1.10s (± 0.37%)~1.10s1.11sp=0.405 n=6
Check Time15.73s (± 0.12%)15.76s (± 0.19%)~15.72s15.80sp=0.105 n=6
Emit Time1.30s (± 1.57%)1.30s (± 1.16%)~1.28s1.32sp=0.804 n=6
Total Time21.01s (± 0.11%)21.05s (± 0.13%)+0.04s (+ 0.17%)21.03s21.10sp=0.028 n=6
ts-pre-modules - node (v18.15.0, x64)
Errors6868~~~p=1.000 n=6
Symbols225,919225,919~~~p=1.000 n=6
Types94,41594,415~~~p=1.000 n=6
Memory used371,139k (± 0.01%)371,095k (± 0.01%)~371,047k371,150kp=0.230 n=6
Parse Time2.91s (± 0.92%)2.92s (± 1.61%)~2.88s3.01sp=0.936 n=6
Bind Time1.60s (± 1.33%)1.59s (± 1.34%)~1.57s1.62sp=0.256 n=6
Check Time16.39s (± 0.46%)16.37s (± 0.40%)~16.31s16.49sp=0.808 n=6
Emit Time0.00s0.00s~~~p=1.000 n=6
Total Time20.89s (± 0.33%)20.88s (± 0.44%)~20.80s21.03sp=0.809 n=6
vscode - node (v18.15.0, x64)
Errors33~~~p=1.000 n=6
Symbols3,127,9093,127,909~~~p=1.000 n=6
Types1,078,1881,078,188~~~p=1.000 n=6
Memory used3,220,310k (± 0.01%)3,220,233k (± 0.01%)~3,219,375k3,220,477kp=0.936 n=6
Parse Time17.41s (± 0.65%)17.31s (± 0.36%)~17.19s17.38sp=0.297 n=6
Bind Time5.50s (± 2.51%)5.45s (± 2.12%)~5.36s5.68sp=0.575 n=6
Check Time104.79s (± 2.91%)105.55s (± 3.00%)~102.85s110.96sp=0.575 n=6
Emit Time32.19s (± 6.17%)32.18s (± 6.87%)~27.68s33.42sp=0.689 n=6
Total Time159.89s (± 1.11%)160.48s (± 1.07%)~158.42s162.42sp=0.575 n=6
webpack - node (v18.15.0, x64)
Errors00~~~p=1.000 n=6
Symbols286,866286,866~~~p=1.000 n=6
Types116,245116,245~~~p=1.000 n=6
Memory used437,860k (± 0.03%)437,716k (± 0.06%)~437,374k437,993kp=0.298 n=6
Parse Time4.02s (± 0.73%)4.03s (± 0.92%)~3.98s4.07sp=0.685 n=6
Bind Time1.74s (± 0.43%)1.74s (± 1.23%)~1.71s1.76sp=1.000 n=6
Check Time18.50s (± 0.69%)18.81s (± 2.35%)~18.42s19.38sp=0.298 n=6
Emit Time0.00s0.00s~~~p=1.000 n=6
Total Time24.26s (± 0.53%)24.58s (± 1.72%)~24.19s25.15sp=0.199 n=6
xstate-main - node (v18.15.0, x64)
Errors33~~~p=1.000 n=6
Symbols543,130543,130~~~p=1.000 n=6
Types181,889181,889~~~p=1.000 n=6
Memory used485,557k (± 0.01%)485,577k (± 0.01%)~485,518k485,636kp=0.575 n=6
Parse Time4.18s (± 0.47%)4.19s (± 0.58%)~4.15s4.22sp=0.803 n=6
Bind Time1.47s (± 0.82%)1.47s (± 0.70%)~1.45s1.48sp=0.408 n=6
Check Time23.78s (± 0.24%)23.80s (± 0.36%)~23.63s23.86sp=0.289 n=6
Emit Time0.00s0.00s~~~p=1.000 n=6
Total Time29.44s (± 0.22%)29.45s (± 0.37%)~29.24s29.54sp=0.422 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • Compiler-Unions - node (v18.15.0, x64)
  • angular-1 - node (v18.15.0, x64)
  • mui-docs - node (v18.15.0, x64)
  • self-build-src - node (v18.15.0, x64)
  • self-build-src-public-api - node (v18.15.0, x64)
  • self-compiler - node (v18.15.0, x64)
  • ts-pre-modules - node (v18.15.0, x64)
  • vscode - node (v18.15.0, x64)
  • webpack - node (v18.15.0, x64)
  • xstate-main - node (v18.15.0, x64)
BenchmarkNameIterations
Currentpr6
Baselinebaseline6

Developer Information:

Download Benchmarks

@typescript-bot

Copy link
Copy Markdown
Contributor

Jake Bailey (@jakebailey) Here are the results of running the top 400 repos with tsc comparing main and refs/pull/60345/merge:

Everything looks good!

@zedryas

Copy link
Copy Markdown

Any update on this merge request? cc Wesley Wigham (@weswigham) (i do tag because you were the reviewer on #57362 )

@typescript-bot

Copy link
Copy Markdown
Contributor

With 6.0 out as the final release vehicle for this codebase, we're closing all PRs that don't fit the merge criteria for post-6.0 patches. If you think this was a mistake and this PR fits the post-6.0 patch criteria, please post to the 6.0 iteration issue with details (specifically, which PR and which patch criteria it satisfies).

Next steps for PRs:

  • For crash bugfixes or language service improvements, PRs are currently accepted at the typescript-go repo
  • Changes to type system behavior should wait until after 7.0, at which point mainline TypeScript development will resume in this repository with the Go codebase
  • Library file updates (lib.d.ts etc) continue to live in this repo or the DOM Generator repo as appropriate

@github-project-automationgithub-project-automationBot moved this from Not started to Done in PR BacklogMar 24, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Uncommitted BugPR for untriaged, rejected, closed or missing bug

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Can extends [any, any] but not generic which is exactly [any, any] (woks till 5.3.3 breaks from 5.4.5)

5 participants

@Andarist@zedryas@jakebailey@typescript-bot@RyanCavanaugh