-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Filter return type inferences by constraint applicability #58910
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
Merged
jakebailey
merged 12 commits into
microsoft:main
from
Andarist:fix/filter-out-return-type-inference-by-constraint-applicability
Dec 3, 2025
Merged
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b9936e3
Filter return type inferences by constraint applicability
Andarist ffb2da0
Merge remote-tracking branch 'origin/main' into fix/filter-out-return…
Andarist 74e6799
Merge remote-tracking branch 'origin/main' into fix/filter-out-return…
Andarist 0ecedb6
fixed a silly mistake
Andarist b7418c7
add extra test case
Andarist 1c744ca
add an extra test case
Andarist 24f5964
dont forget about the fallback type
Andarist b0563e8
Merge remote-tracking branch 'origin/main' into fix/filter-out-return…
Andarist 97c164d
add extra test case
Andarist c8e938c
Merge remote-tracking branch 'origin/main' into fix/filter-out-return…
Andarist 6db731b
Merge remote-tracking branch 'origin/main' into fix/filter-out-return…
Andarist 35e0002
match ts-go changes
Andarist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
tests/baselines/reference/inferenceContextualReturnTypeUnion1.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| //// [tests/cases/compiler/inferenceContextualReturnTypeUnion1.ts] //// | ||
|
|
||
| === inferenceContextualReturnTypeUnion1.ts === | ||
| // https://github.com/microsoft/TypeScript/issues/50719 | ||
|
|
||
| declare function useCallback1<T extends Function>(fn: T): T; | ||
| >useCallback1 : Symbol(useCallback1, Decl(inferenceContextualReturnTypeUnion1.ts, 0, 0)) | ||
| >T : Symbol(T, Decl(inferenceContextualReturnTypeUnion1.ts, 2, 30)) | ||
| >Function : Symbol(Function, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
| >fn : Symbol(fn, Decl(inferenceContextualReturnTypeUnion1.ts, 2, 50)) | ||
| >T : Symbol(T, Decl(inferenceContextualReturnTypeUnion1.ts, 2, 30)) | ||
| >T : Symbol(T, Decl(inferenceContextualReturnTypeUnion1.ts, 2, 30)) | ||
|
|
||
| declare function ex2(callback?: (x: number) => void): void; | ||
| >ex2 : Symbol(ex2, Decl(inferenceContextualReturnTypeUnion1.ts, 2, 60)) | ||
| >callback : Symbol(callback, Decl(inferenceContextualReturnTypeUnion1.ts, 4, 21)) | ||
| >x : Symbol(x, Decl(inferenceContextualReturnTypeUnion1.ts, 4, 33)) | ||
|
|
||
| ex2(useCallback1((x) => {})); | ||
| >ex2 : Symbol(ex2, Decl(inferenceContextualReturnTypeUnion1.ts, 2, 60)) | ||
| >useCallback1 : Symbol(useCallback1, Decl(inferenceContextualReturnTypeUnion1.ts, 0, 0)) | ||
| >x : Symbol(x, Decl(inferenceContextualReturnTypeUnion1.ts, 5, 18)) | ||
|
|
||
| declare function ex3(callback: ((x: number) => void) | 5): void; | ||
| >ex3 : Symbol(ex3, Decl(inferenceContextualReturnTypeUnion1.ts, 5, 29)) | ||
| >callback : Symbol(callback, Decl(inferenceContextualReturnTypeUnion1.ts, 7, 21)) | ||
| >x : Symbol(x, Decl(inferenceContextualReturnTypeUnion1.ts, 7, 33)) | ||
|
|
||
| ex3(useCallback1((x) => {})); | ||
| >ex3 : Symbol(ex3, Decl(inferenceContextualReturnTypeUnion1.ts, 5, 29)) | ||
| >useCallback1 : Symbol(useCallback1, Decl(inferenceContextualReturnTypeUnion1.ts, 0, 0)) | ||
| >x : Symbol(x, Decl(inferenceContextualReturnTypeUnion1.ts, 8, 18)) | ||
|
|
||
| // https://github.com/microsoft/TypeScript/issues/41461 | ||
|
|
||
| declare function useCallback2<T extends (...args: any[]) => any>( | ||
| >useCallback2 : Symbol(useCallback2, Decl(inferenceContextualReturnTypeUnion1.ts, 8, 29)) | ||
| >T : Symbol(T, Decl(inferenceContextualReturnTypeUnion1.ts, 12, 30)) | ||
| >args : Symbol(args, Decl(inferenceContextualReturnTypeUnion1.ts, 12, 41)) | ||
|
|
||
| callback: T, | ||
| >callback : Symbol(callback, Decl(inferenceContextualReturnTypeUnion1.ts, 12, 65)) | ||
| >T : Symbol(T, Decl(inferenceContextualReturnTypeUnion1.ts, 12, 30)) | ||
|
|
||
| ): T; | ||
| >T : Symbol(T, Decl(inferenceContextualReturnTypeUnion1.ts, 12, 30)) | ||
|
|
||
| const test: ((x: string) => void) | undefined = useCallback2((x) => {}); | ||
| >test : Symbol(test, Decl(inferenceContextualReturnTypeUnion1.ts, 15, 5)) | ||
| >x : Symbol(x, Decl(inferenceContextualReturnTypeUnion1.ts, 15, 14)) | ||
| >useCallback2 : Symbol(useCallback2, Decl(inferenceContextualReturnTypeUnion1.ts, 8, 29)) | ||
| >x : Symbol(x, Decl(inferenceContextualReturnTypeUnion1.ts, 15, 62)) | ||
|
|
82 changes: 82 additions & 0 deletions
82
tests/baselines/reference/inferenceContextualReturnTypeUnion1.types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| //// [tests/cases/compiler/inferenceContextualReturnTypeUnion1.ts] //// | ||
|
|
||
| === inferenceContextualReturnTypeUnion1.ts === | ||
| // https://github.com/microsoft/TypeScript/issues/50719 | ||
|
|
||
| declare function useCallback1<T extends Function>(fn: T): T; | ||
| >useCallback1 : <T extends Function>(fn: T) => T | ||
| > : ^ ^^^^^^^^^ ^^ ^^ ^^^^^ | ||
| >fn : T | ||
| > : ^ | ||
|
|
||
| declare function ex2(callback?: (x: number) => void): void; | ||
| >ex2 : (callback?: (x: number) => void) => void | ||
| > : ^ ^^^ ^^^^^ | ||
| >callback : ((x: number) => void) | undefined | ||
| > : ^^ ^^ ^^^^^ ^^^^^^^^^^^^^ | ||
| >x : number | ||
| > : ^^^^^^ | ||
|
|
||
| ex2(useCallback1((x) => {})); | ||
| >ex2(useCallback1((x) => {})) : void | ||
| > : ^^^^ | ||
| >ex2 : (callback?: (x: number) => void) => void | ||
| > : ^ ^^^ ^^^^^ | ||
| >useCallback1((x) => {}) : (x: number) => void | ||
| > : ^ ^^^^^^^^^^^^^^^^^ | ||
| >useCallback1 : <T extends Function>(fn: T) => T | ||
| > : ^ ^^^^^^^^^ ^^ ^^ ^^^^^ | ||
| >(x) => {} : (x: number) => void | ||
| > : ^ ^^^^^^^^^^^^^^^^^ | ||
| >x : number | ||
| > : ^^^^^^ | ||
|
|
||
| declare function ex3(callback: ((x: number) => void) | 5): void; | ||
| >ex3 : (callback: ((x: number) => void) | 5) => void | ||
| > : ^ ^^ ^^^^^ | ||
| >callback : ((x: number) => void) | 5 | ||
| > : ^^ ^^ ^^^^^ ^^^^^ | ||
| >x : number | ||
| > : ^^^^^^ | ||
|
|
||
| ex3(useCallback1((x) => {})); | ||
| >ex3(useCallback1((x) => {})) : void | ||
| > : ^^^^ | ||
| >ex3 : (callback: ((x: number) => void) | 5) => void | ||
| > : ^ ^^ ^^^^^ | ||
| >useCallback1((x) => {}) : (x: number) => void | ||
| > : ^ ^^^^^^^^^^^^^^^^^ | ||
| >useCallback1 : <T extends Function>(fn: T) => T | ||
| > : ^ ^^^^^^^^^ ^^ ^^ ^^^^^ | ||
| >(x) => {} : (x: number) => void | ||
| > : ^ ^^^^^^^^^^^^^^^^^ | ||
| >x : number | ||
| > : ^^^^^^ | ||
|
|
||
| // https://github.com/microsoft/TypeScript/issues/41461 | ||
|
|
||
| declare function useCallback2<T extends (...args: any[]) => any>( | ||
| >useCallback2 : <T extends (...args: any[]) => any>(callback: T) => T | ||
| > : ^ ^^^^^^^^^ ^^ ^^ ^^^^^ | ||
| >args : any[] | ||
| > : ^^^^^ | ||
|
|
||
| callback: T, | ||
| >callback : T | ||
| > : ^ | ||
|
|
||
| ): T; | ||
| const test: ((x: string) => void) | undefined = useCallback2((x) => {}); | ||
| >test : ((x: string) => void) | undefined | ||
| > : ^^ ^^ ^^^^^ ^^^^^^^^^^^^^ | ||
| >x : string | ||
| > : ^^^^^^ | ||
| >useCallback2((x) => {}) : (x: string) => void | ||
| > : ^ ^^^^^^^^^^^^^^^^^ | ||
| >useCallback2 : <T extends (...args: any[]) => any>(callback: T) => T | ||
| > : ^ ^^^^^^^^^ ^^ ^^ ^^^^^ | ||
| >(x) => {} : (x: string) => void | ||
| > : ^ ^^^^^^^^^^^^^^^^^ | ||
| >x : string | ||
| > : ^^^^^^ | ||
|
|
19 changes: 19 additions & 0 deletions
19
tests/cases/compiler/inferenceContextualReturnTypeUnion1.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // @strict: true | ||
| // @noEmit: true | ||
|
|
||
| // https://github.com/microsoft/TypeScript/issues/50719 | ||
|
|
||
| declare function useCallback1<T extends Function>(fn: T): T; | ||
|
|
||
| declare function ex2(callback?: (x: number) => void): void; | ||
| ex2(useCallback1((x) => {})); | ||
|
|
||
| declare function ex3(callback: ((x: number) => void) | 5): void; | ||
| ex3(useCallback1((x) => {})); | ||
|
|
||
| // https://github.com/microsoft/TypeScript/issues/41461 | ||
|
|
||
| declare function useCallback2<T extends (...args: any[]) => any>( | ||
| callback: T, | ||
| ): T; | ||
| const test: ((x: string) => void) | undefined = useCallback2((x) => {}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only need to bother with this filtering if the constraint check for the whole union fails, right? (Since if it passes, no filtering will occur, as every union member must also pass.) This can probably be inside the comparison branch below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That’s right but it felt cleaner to have this in a separate branch. Shouldnt filterType already optimize for array/type allocations?