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

Fixed an issue with satisfies "resolving" generic types of references #60710

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Andarist
Copy link
Contributor

@Andarist Andarist commented Dec 7, 2024

fixes #52394
fixes #60698

@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Dec 7, 2024
const exprType = checkExpression(expression, checkMode);
const targetType = getTypeFromTypeNode(target);
if (isErrorType(targetType)) {
return targetType;
}
const errorNode = findAncestor(target.parent, n => n.kind === SyntaxKind.SatisfiesExpression || n.kind === SyntaxKind.JSDocSatisfiesTag);
checkTypeAssignableToAndOptionallyElaborate(exprType, targetType, errorNode, expression, Diagnostics.Type_0_does_not_satisfy_the_expected_type_1);
return exprType;
return checkExpression(expression, checkMode | CheckMode.SatisfiesOutput);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

exprType is used to check the assignability, it may contain "resolved" types for narrowable references. So the assignability works like it works in:

const x: "A" = genericReferenceNarrowedDownToA;

But the output type is different because we should propagate that "resolved" type in:

// actual on main: `"A"`
// expected: `AB extends "A" | "B"`
const x = genericReferenceNarrowedDownToA satisfies "A";

@@ -160,8 +160,8 @@ function g1<T extends Box<T> | undefined>(x: T) {
> : ^^^^^^^
>isBox : (x: any) => x is Box<unknown>
> : ^ ^^ ^^^^^
>x : Box<T> | undefined
> : ^^^^^^^^^^^^^^^^^^
>x : T
Copy link
Contributor Author

Choose a reason for hiding this comment

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

all the baseline changes like this are caused by the fact that now hasContextualTypeWithNoGenericTypes returns false when the contextual type is any, and thus substituteConstraints gets computed as false in getNarrowableTypeForReference

Comment on lines +30 to +31
description: "Add missing function declaration 'added2'",
index: 1,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this was a mistake in the original test case, goTo.marker("2") has no effect on verify.codeFix, we need to use a global index here to test the codefix at that marker

@@ -19,24 +19,24 @@ verify.codeFix({
added2(et)
}

function added1(et: string) {
function added1<T extends "phone" | "home">(et: T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe this was the original true intention here but the type widened since it had a union constraint. This was introduced in #49727 , cc @JoshuaKGoldberg

@@ -15,7 +15,7 @@ verify.codeFix({
added(keyofTypeof);
}

function added(keyofTypeof: string | number | symbol) {
function added<T>(keyofTypeof: keyof T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This isn't a particularly great codefix... since keyof T when T doesn't appear "naked" somewhere in the parameters list is usually a lousy parameter. I don't think it's worth fighting it here though as it could appear naked somewhere.

: getContextualType(node, /*contextFlags*/ undefined));
return contextualType && !isGenericType(contextualType);
getContextualType(node, (checkMode & CheckMode.RestBindingElement ? ContextFlags.SkipBindingPatterns : 0) | (checkMode & CheckMode.SatisfiesOutput ? ContextFlags.SkipSatisfies : 0));
return contextualType && !(contextualType.flags & TypeFlags.Any) && !isGenericType(contextualType);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The added !(contextualType.flags & TypeFlags.Any) bit here isn't exactly related to satisfies at all. I have added it to account for auto types, that affected all the baselines that had any as the contextual type in a few places. It also affected those codefixes for missing function declarations since the error type (a special kind of any) was the contextual type for those arguments here. I think those changes are positive

@jakebailey
Copy link
Member

@typescript-bot test it

@typescript-bot
Copy link
Collaborator

typescript-bot commented Dec 7, 2024

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 ✅ Results
perf test this faster ✅ Started 👀 Results

@typescript-bot
Copy link
Collaborator

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

Everything looks good!

@typescript-bot
Copy link
Collaborator

Hey @jakebailey, the results of running the DT tests are ready.

Everything looks the same!

You can check the log here.

@typescript-bot
Copy link
Collaborator

@jakebailey
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 - node (v18.15.0, x64)
Errors 34 34 ~ ~ ~ p=1.000 n=6
Symbols 62,363 62,363 ~ ~ ~ p=1.000 n=6
Types 50,395 50,395 ~ ~ ~ p=1.000 n=6
Memory used 193,669k (± 0.80%) 194,216k (± 0.95%) ~ 192,984k 196,647k p=0.810 n=6
Parse Time 1.31s (± 0.89%) 1.30s (± 1.73%) ~ 1.26s 1.32s p=0.933 n=6
Bind Time 0.72s 0.72s ~ ~ ~ p=1.000 n=6
Check Time 9.78s (± 0.36%) 9.78s (± 0.47%) ~ 9.71s 9.84s p=0.809 n=6
Emit Time 2.74s (± 0.64%) 2.74s (± 0.60%) ~ 2.72s 2.76s p=0.317 n=6
Total Time 14.54s (± 0.19%) 14.54s (± 0.41%) ~ 14.42s 14.59s p=0.872 n=6
angular-1 - node (v18.15.0, x64)
Errors 37 37 ~ ~ ~ p=1.000 n=6
Symbols 947,936 947,941 +5 (+ 0.00%) ~ ~ p=0.001 n=6
Types 410,955 410,961 +6 (+ 0.00%) ~ ~ p=0.001 n=6
Memory used 1,225,954k (± 0.01%) 1,225,980k (± 0.00%) ~ 1,225,905k 1,226,043k p=0.689 n=6
Parse Time 8.06s (± 1.00%) 8.10s (± 0.78%) ~ 8.01s 8.19s p=0.748 n=6
Bind Time 2.27s (± 0.95%) 2.26s (± 0.71%) ~ 2.24s 2.28s p=0.285 n=6
Check Time 38.05s (± 0.32%) 38.02s (± 0.36%) ~ 37.84s 38.26s p=0.376 n=6
Emit Time 18.19s (± 0.60%) 18.25s (± 0.71%) ~ 18.05s 18.45s p=0.575 n=6
Total Time 66.58s (± 0.28%) 66.62s (± 0.23%) ~ 66.40s 66.85s p=0.748 n=6
mui-docs - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 2,501,827 2,501,827 ~ ~ ~ p=1.000 n=6
Types 909,625 909,625 ~ ~ ~ p=1.000 n=6
Memory used 2,319,331k (± 0.00%) 2,319,335k (± 0.00%) ~ 2,319,309k 2,319,372k p=0.689 n=6
Parse Time 9.29s (± 0.22%) 9.29s (± 0.20%) ~ 9.27s 9.32s p=0.513 n=6
Bind Time 2.16s (± 0.48%) 2.17s (± 0.41%) ~ 2.16s 2.18s p=0.054 n=6
Check Time 74.97s (± 0.28%) 74.96s (± 0.42%) ~ 74.57s 75.44s p=0.810 n=6
Emit Time 0.29s (± 4.29%) 0.28s (± 1.45%) ~ 0.28s 0.29s p=0.849 n=6
Total Time 86.70s (± 0.26%) 86.70s (± 0.37%) ~ 86.30s 87.20s p=1.000 n=6
self-build-src - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,225,328 1,225,449 +121 (+ 0.01%) ~ ~ p=0.001 n=6
Types 266,569 266,609 +40 (+ 0.02%) ~ ~ p=0.001 n=6
Memory used 2,414,947k (± 6.13%) 2,414,959k (± 6.14%) ~ 2,353,555k 2,717,923k p=0.936 n=6
Parse Time 5.26s (± 1.26%) 5.24s (± 1.04%) ~ 5.18s 5.34s p=0.575 n=6
Bind Time 1.76s (± 0.43%) 1.77s (± 1.13%) ~ 1.75s 1.80s p=0.406 n=6
Check Time 35.25s (± 0.93%) 35.12s (± 0.64%) ~ 34.72s 35.35s p=0.471 n=6
Emit Time 2.97s (± 1.63%) 2.95s (± 0.79%) ~ 2.91s 2.97s p=0.229 n=6
Total Time 45.27s (± 0.63%) 45.09s (± 0.38%) ~ 44.78s 45.26s p=0.230 n=6
self-build-src-public-api - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,225,328 1,225,449 +121 (+ 0.01%) ~ ~ p=0.001 n=6
Types 266,569 266,609 +40 (+ 0.02%) ~ ~ p=0.001 n=6
Memory used 2,482,749k (± 5.94%) 2,543,398k (±11.63%) ~ 2,421,832k 3,147,758k p=0.936 n=6
Parse Time 5.46s (± 1.25%) 5.52s (± 1.22%) ~ 5.43s 5.63s p=0.173 n=6
Bind Time 1.81s (± 1.30%) 1.82s (± 1.03%) ~ 1.78s 1.83s p=0.415 n=6
Check Time 35.18s (± 0.70%) 35.34s (± 0.28%) ~ 35.17s 35.44s p=0.230 n=6
Emit Time 2.98s (± 1.73%) 3.01s (± 1.14%) ~ 2.97s 3.06s p=0.332 n=6
Total Time 45.44s (± 0.57%) 45.68s (± 0.24%) ~ 45.55s 45.88s p=0.093 n=6
self-compiler - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 262,267 262,298 +31 (+ 0.01%) ~ ~ p=0.001 n=6
Types 106,628 106,636 +8 (+ 0.01%) ~ ~ p=0.001 n=6
Memory used 439,878k (± 0.02%) 439,933k (± 0.01%) ~ 439,861k 440,002k p=0.230 n=6
Parse Time 3.54s (± 1.19%) 3.53s (± 0.69%) ~ 3.49s 3.55s p=0.809 n=6
Bind Time 1.32s (± 0.92%) 1.32s (± 1.35%) ~ 1.30s 1.35s p=0.934 n=6
Check Time 18.93s (± 0.21%) 19.00s (± 0.45%) ~ 18.87s 19.10s p=0.199 n=6
Emit Time 1.52s (± 0.68%) 1.53s (± 0.49%) ~ 1.52s 1.54s p=0.063 n=6
Total Time 25.31s (± 0.31%) 25.38s (± 0.32%) ~ 25.25s 25.47s p=0.261 n=6
ts-pre-modules - node (v18.15.0, x64)
Errors 70 70 ~ ~ ~ p=1.000 n=6
Symbols 226,062 226,062 ~ ~ ~ p=1.000 n=6
Types 94,488 94,488 ~ ~ ~ p=1.000 n=6
Memory used 371,608k (± 0.01%) 371,600k (± 0.02%) ~ 371,554k 371,751k p=0.336 n=6
Parse Time 2.88s (± 0.75%) 2.90s (± 1.09%) ~ 2.86s 2.94s p=0.419 n=6
Bind Time 1.59s (± 1.02%) 1.58s (± 1.86%) ~ 1.54s 1.62s p=0.625 n=6
Check Time 16.49s (± 0.38%) 16.45s (± 0.36%) ~ 16.35s 16.51s p=0.332 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 20.97s (± 0.26%) 20.93s (± 0.31%) ~ 20.83s 21.01s p=0.336 n=6
vscode - node (v18.15.0, x64)
Errors 3 3 ~ ~ ~ p=1.000 n=6
Symbols 3,197,830 3,199,023 +1,193 (+ 0.04%) ~ ~ p=0.001 n=6
Types 1,099,273 1,099,757 +484 (+ 0.04%) ~ ~ p=0.001 n=6
Memory used 3,271,538k (± 0.01%) 3,271,479k (± 0.01%) ~ 3,271,074k 3,272,000k p=1.000 n=6
Parse Time 14.20s (± 0.99%) 14.19s (± 0.28%) ~ 14.14s 14.23s p=0.521 n=6
Bind Time 4.62s (± 2.57%) 4.52s (± 0.24%) -0.10s (- 2.24%) 4.51s 4.54s p=0.014 n=6
Check Time 88.91s (± 3.04%) 89.00s (± 3.26%) ~ 86.71s 94.21s p=1.000 n=6
Emit Time 27.39s (± 8.16%) 27.97s (± 2.00%) ~ 27.27s 28.56s p=0.810 n=6
Total Time 135.12s (± 0.53%) 135.68s (± 1.88%) ~ 133.85s 140.54s p=0.936 n=6
webpack - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 288,747 288,747 ~ ~ ~ p=1.000 n=6
Types 117,158 117,158 ~ ~ ~ p=1.000 n=6
Memory used 441,065k (± 0.03%) 441,071k (± 0.02%) ~ 440,943k 441,209k p=0.810 n=6
Parse Time 4.06s (± 1.10%) 4.09s (± 0.68%) ~ 4.06s 4.14s p=0.168 n=6
Bind Time 1.76s (± 1.21%) 1.76s (± 1.27%) ~ 1.74s 1.80s p=0.870 n=6
Check Time 18.84s (± 0.88%) 18.91s (± 0.66%) ~ 18.76s 19.11s p=0.689 n=6
Emit Time 0.00s 0.00s (±244.70%) ~ 0.00s 0.01s p=0.405 n=6
Total Time 24.67s (± 0.72%) 24.77s (± 0.51%) ~ 24.62s 24.99s p=0.572 n=6
xstate-main - node (v18.15.0, x64)
Errors 5 5 ~ ~ ~ p=1.000 n=6
Symbols 552,389 552,405 +16 (+ 0.00%) ~ ~ p=0.001 n=6
Types 185,093 185,111 +18 (+ 0.01%) ~ ~ p=0.001 n=6
Memory used 492,536k (± 0.01%) 492,463k (± 0.01%) -73k (- 0.01%) 492,432k 492,533k p=0.031 n=6
Parse Time 3.43s (± 0.70%) 3.43s (± 0.68%) ~ 3.40s 3.47s p=1.000 n=6
Bind Time 1.18s (± 0.54%) 1.18s (± 1.25%) ~ 1.16s 1.20s p=0.737 n=6
Check Time 19.53s (± 0.20%) 19.50s (± 0.21%) ~ 19.45s 19.55s p=0.102 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 24.14s (± 0.11%) 24.11s (± 0.14%) ~ 24.06s 24.16s p=0.090 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)
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

Developer Information:

Download Benchmarks

@typescript-bot
Copy link
Collaborator

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

Everything looks good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
Status: Not started
Development

Successfully merging this pull request may close these issues.

satisfies changes the generic type, "resolves" it immediately satisfies changes typechecking behavior
3 participants