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

Fix Callback argument type not inferred for union of interfaces #59309 #59672

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

Conversation

MichalMarsalek
Copy link

Please verify that:

  • There is an associated issue in the Backlog milestone (required)
  • Code is up-to-date with the main branch
  • You've successfully run hereby runtests locally
  • There are new or updated unit tests validating the change

Fixes #59309

@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Aug 18, 2024
src/compiler/checker.ts Outdated Show resolved Hide resolved
// This signature will contribute to contextual union signature
signatureList = [signature];
}
else if (!compareSignaturesIdentical(signatureList[0], signature, /*partialMatch*/ true, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true, compareTypesSubtypeOf)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

If you are loosening here how parameter types are meant to be compared I'd probably expect here:

Suggested change
else if (!compareSignaturesIdentical(signatureList[0], signature, /*partialMatch*/ true, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true, compareTypesSubtypeOf)) {
else if (!compareSignaturesIdentical(signatureList[0], signature, /*partialMatch*/ true, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true, compareTypesAssignable)) {

Unless there is already a reason why the subtype relationship was used here.

Copy link
Author

Choose a reason for hiding this comment

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

I'm not really sure when each relation should be used. I used compareTypesSubtypeOf because that's what is used in a call to compareSignaturesIdentical with partialMatch === true in the function findMatchingSignatures.

Copy link
Contributor

Choose a reason for hiding this comment

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

findMatchingSignatures gets only called by getUnionSignatures so I agree that this probably makes sense

const types = (type as UnionType).types
.map(type => ({ type, signature: getContextualCallSignature(type, node) }))
.filter(type => type.signature)
.sort((t1, t2) => t1.signature!.parameters.length - t2.signature!.parameters.length);
Copy link
Contributor

@Andarist Andarist Aug 18, 2024

Choose a reason for hiding this comment

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

You need to account for rest parameters here so it would be more appropriate to use getParameterCount here over directly checking the .parameters.length. Please also consider cases like

((a: string, b?: string) => void) | ((a: string, ...rest: string[]) => void)

Both of those signatures have the same parameter count but perhaps you'd like to specifically sort one of them as the first one.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you. I think that actually what I need is getMinArgumentCount.
For the case ((a: string, b?: string) => void) | ((a: string, ...rest: string[]) => void) I don't think it matters which one is first.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if it will make any difference but to exercise even more funky test cases you could add test cases with signatures of this shape:

((a: string, ...rest: [string, number?] | [number, boolean?]) => void) | ((a: string, ...rest: [string, number] | [number, boolean]) => void)

Note that I didn't think through what types you should use in those rest parameters, just that they can be unions of tuples and that one of the signatures might have some of the elements there required and some might have them optional etc.

@jakebailey
Copy link
Member

@typescript-bot test it

@typescript-bot
Copy link
Collaborator

typescript-bot commented Aug 19, 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

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 Here are the results of running the user tests with tsc comparing main and refs/pull/59672/merge:

Something interesting changed - please have a look.

Details

webpack

tsconfig.types.test.json

@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 30 30 ~ ~ ~ p=1.000 n=6
Symbols 62,153 62,153 ~ ~ ~ p=1.000 n=6
Types 50,242 50,242 ~ ~ ~ p=1.000 n=6
Memory used 194,208k (± 0.97%) 193,534k (± 0.94%) ~ 192,326k 195,902k p=0.298 n=6
Parse Time 1.31s (± 0.64%) 1.31s (± 0.79%) ~ 1.29s 1.32s p=0.788 n=6
Bind Time 0.71s 0.71s (± 0.57%) ~ 0.71s 0.72s p=0.405 n=6
Check Time 9.57s (± 0.39%) 9.54s (± 0.29%) ~ 9.51s 9.57s p=0.295 n=6
Emit Time 2.73s (± 1.08%) 2.74s (± 0.55%) ~ 2.72s 2.76s p=0.681 n=6
Total Time 14.31s (± 0.34%) 14.29s (± 0.16%) ~ 14.27s 14.33s p=0.195 n=6
angular-1 - node (v18.15.0, x64)
Errors 7 7 ~ ~ ~ p=1.000 n=6
Symbols 945,757 945,760 +3 (+ 0.00%) ~ ~ p=0.001 n=6
Types 410,067 410,072 +5 (+ 0.00%) ~ ~ p=0.001 n=6
Memory used 1,222,599k (± 0.00%) 1,222,685k (± 0.00%) +86k (+ 0.01%) 1,222,663k 1,222,733k p=0.005 n=6
Parse Time 6.66s (± 0.44%) 6.64s (± 0.11%) ~ 6.63s 6.65s p=0.280 n=6
Bind Time 1.86s (± 0.34%) 1.86s (± 0.63%) ~ 1.85s 1.88s p=1.000 n=6
Check Time 31.04s (± 0.45%) 31.24s (± 0.28%) +0.20s (+ 0.64%) 31.15s 31.38s p=0.006 n=6
Emit Time 14.90s (± 0.69%) 14.96s (± 0.37%) ~ 14.87s 15.01s p=0.171 n=6
Total Time 54.47s (± 0.32%) 54.71s (± 0.24%) ~ 54.54s 54.88s p=0.054 n=6
mui-docs - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 2,505,734 2,506,432 +698 (+ 0.03%) ~ ~ p=0.001 n=6
Types 993,039 993,701 +662 (+ 0.07%) ~ ~ p=0.001 n=6
Memory used 2,376,628k (± 0.00%) 2,377,892k (± 0.00%) +1,264k (+ 0.05%) 2,377,797k 2,378,050k p=0.005 n=6
Parse Time 9.29s (± 0.19%) 9.30s (± 0.32%) ~ 9.28s 9.35s p=0.506 n=6
Bind Time 2.19s (± 0.48%) 2.19s (± 0.55%) ~ 2.18s 2.21s p=0.282 n=6
Check Time 73.77s (± 0.25%) 73.92s (± 0.71%) ~ 73.26s 74.86s p=0.575 n=6
Emit Time 0.28s (± 2.70%) 0.28s (± 2.88%) ~ 0.27s 0.29s p=0.306 n=6
Total Time 85.52s (± 0.21%) 85.70s (± 0.64%) ~ 85.01s 86.70s p=0.378 n=6
self-build-src - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,231,749 1,231,762 +13 (+ 0.00%) ~ ~ p=0.001 n=6
Types 264,525 264,531 +6 (+ 0.00%) ~ ~ p=0.001 n=6
Memory used 2,353,013k (± 0.02%) 2,353,054k (± 0.02%) ~ 2,352,453k 2,353,510k p=1.000 n=6
Parse Time 4.99s (± 0.42%) 4.97s (± 0.70%) ~ 4.92s 5.01s p=0.261 n=6
Bind Time 1.88s (± 0.75%) 1.88s (± 0.48%) ~ 1.87s 1.89s p=1.000 n=6
Check Time 34.69s (± 0.35%) 34.83s (± 0.23%) ~ 34.68s 34.91s p=0.092 n=6
Emit Time 3.37s (± 1.00%) 3.39s (± 2.87%) ~ 3.30s 3.57s p=1.000 n=6
Total Time 44.94s (± 0.32%) 45.08s (± 0.40%) ~ 44.79s 45.32s p=0.128 n=6
self-build-src-public-api - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,231,749 1,231,762 +13 (+ 0.00%) ~ ~ p=0.001 n=6
Types 264,525 264,531 +6 (+ 0.00%) ~ ~ p=0.001 n=6
Memory used 2,427,537k (± 0.02%) 2,427,445k (± 0.02%) ~ 2,426,785k 2,428,164k p=0.810 n=6
Parse Time 6.25s (± 0.46%) 6.22s (± 0.74%) ~ 6.17s 6.27s p=0.810 n=6
Bind Time 2.03s (± 0.76%) 2.04s (± 0.80%) ~ 2.02s 2.06s p=0.463 n=6
Check Time 41.67s (± 0.58%) 41.63s (± 0.55%) ~ 41.23s 41.90s p=0.689 n=6
Emit Time 4.04s (± 0.40%) 4.15s (± 4.13%) ~ 3.98s 4.43s p=0.148 n=6
Total Time 54.00s (± 0.48%) 54.05s (± 0.39%) ~ 53.82s 54.30s p=0.936 n=6
self-compiler - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 256,947 256,960 +13 (+ 0.01%) ~ ~ p=0.001 n=6
Types 105,643 105,649 +6 (+ 0.01%) ~ ~ p=0.001 n=6
Memory used 429,391k (± 0.01%) 429,444k (± 0.02%) ~ 429,297k 429,528k p=0.093 n=6
Parse Time 3.35s (± 0.56%) 3.35s (± 0.56%) ~ 3.33s 3.38s p=1.000 n=6
Bind Time 1.30s (± 0.94%) 1.29s (± 0.63%) ~ 1.28s 1.30s p=0.120 n=6
Check Time 18.08s (± 0.34%) 18.12s (± 0.17%) ~ 18.08s 18.16s p=0.332 n=6
Emit Time 1.65s (± 0.86%) 1.67s (± 1.28%) ~ 1.64s 1.69s p=0.168 n=6
Total Time 24.39s (± 0.21%) 24.44s (± 0.13%) ~ 24.41s 24.49s p=0.293 n=6
ts-pre-modules - node (v18.15.0, x64)
Errors 35 35 ~ ~ ~ p=1.000 n=6
Symbols 225,018 225,018 ~ ~ ~ p=1.000 n=6
Types 94,245 94,245 ~ ~ ~ p=1.000 n=6
Memory used 370,189k (± 0.02%) 370,154k (± 0.04%) ~ 370,024k 370,428k p=0.230 n=6
Parse Time 2.76s (± 0.87%) 2.75s (± 0.48%) ~ 2.73s 2.76s p=0.373 n=6
Bind Time 1.56s (± 0.52%) 1.56s (± 0.75%) ~ 1.55s 1.58s p=0.738 n=6
Check Time 15.64s (± 0.19%) 15.64s (± 0.21%) ~ 15.58s 15.67s p=0.809 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 19.97s (± 0.13%) 19.95s (± 0.18%) ~ 19.88s 19.98s p=0.687 n=6
vscode - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 3,006,672 3,006,672 ~ ~ ~ p=1.000 n=6
Types 1,035,540 1,035,540 ~ ~ ~ p=1.000 n=6
Memory used 3,130,150k (± 0.00%) 3,130,170k (± 0.00%) ~ 3,130,060k 3,130,255k p=0.575 n=6
Parse Time 13.98s (± 0.21%) 13.95s (± 0.50%) ~ 13.85s 14.04s p=0.260 n=6
Bind Time 4.33s (± 2.16%) 4.29s (± 0.36%) ~ 4.27s 4.31s p=0.413 n=6
Check Time 79.82s (± 0.28%) 79.92s (± 0.25%) ~ 79.68s 80.16s p=0.630 n=6
Emit Time 20.48s (± 0.64%) 20.52s (± 0.37%) ~ 20.41s 20.61s p=0.335 n=6
Total Time 118.62s (± 0.22%) 118.68s (± 0.15%) ~ 118.48s 118.88s p=0.936 n=6
webpack - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 275,352 275,353 +1 (+ 0.00%) ~ ~ p=0.001 n=6
Types 112,436 112,436 ~ ~ ~ p=1.000 n=6
Memory used 424,135k (± 0.01%) 424,239k (± 0.02%) +104k (+ 0.02%) 424,120k 424,370k p=0.045 n=6
Parse Time 3.98s (± 0.54%) 3.97s (± 0.49%) ~ 3.94s 3.99s p=0.745 n=6
Bind Time 1.73s (± 0.60%) 1.72s (± 0.85%) ~ 1.71s 1.74s p=0.190 n=6
Check Time 17.44s (± 0.46%) 17.47s (± 0.37%) ~ 17.38s 17.57s p=0.471 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 23.16s (± 0.35%) 23.16s (± 0.34%) ~ 23.07s 23.28s p=1.000 n=6
xstate-main - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 534,239 534,254 +15 (+ 0.00%) ~ ~ p=0.001 n=6
Types 176,225 176,228 +3 (+ 0.00%) ~ ~ p=0.001 n=6
Memory used 479,429k (± 0.01%) 479,319k (± 0.01%) -110k (- 0.02%) 479,275k 479,412k p=0.013 n=6
Parse Time 4.26s (± 0.62%) 4.25s (± 0.44%) ~ 4.22s 4.27s p=0.257 n=6
Bind Time 1.55s (± 1.00%) 1.54s (± 0.86%) ~ 1.53s 1.56s p=0.323 n=6
Check Time 22.27s (± 0.43%) 22.25s (± 0.21%) ~ 22.19s 22.33s p=1.000 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 28.08s (± 0.35%) 28.05s (± 0.13%) ~ 28.01s 28.10s p=0.748 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

@Andarist
Copy link
Contributor

The reported error in webpack is correct. Right now it errors earlier (which is not mentioned by this comment): TS playground. It's enough to add an extra unused parameter to this function to get the same error as the one reported by the bot.

That said, maybe you could also filter out here signatures that can't be applicable in the first place. Like, if the function is async then only functions that return Promise<any> are valid as contextual signatures for it. Note that the contextual signature could still return a union containing a type assignable to Promise<any>. The same principle applies to generators (and async generators). I've done similar filtering on contextual return types here. That patch would still be relevant, I think, but some contextual signatures could even be discarded earlier

@typescript-bot
Copy link
Collaborator

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

Something interesting changed - please have a look.

Details

vuejs/pinia

5 of 7 projects failed to build with the old tsc and were ignored

tsconfig.json

  • error TS2339: Property 'nonA' does not exist on type '{ a: boolean; nested: { foo: string; a: { b: string; }; }; } & _StoreWithGetters<{}> & PiniaCustomProperties<string, StateTree, _GettersTree<StateTree>, _ActionsTree>'.
  • error TS7023: 'getNonA' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
  • error TS2339: Property 'nonA' does not exist on type '{ getNonA(): any; simple(): "simple"; toggle(): boolean; setFoo(foo: string): void; combined(): void; throws(): never; rejects(): Promise<never>; } & { a: boolean; nested: { ...; }; } & _StoreWithState<...> & _StoreWithGetters<...> & PiniaCustomProperties<...>'.

@MichalMarsalek
Copy link
Author

Like, if the function is async then only functions that return Promise<any> are valid as contextual signatures for it.

That's smart, I added that.

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.

Callback argument type not inferred for union of interfaces
4 participants