-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Combine inferences from distributive conditional types #62093
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
Open
Andarist
wants to merge
4
commits into
microsoft:main
Choose a base branch
from
Andarist:fix/generic-functions-parameters
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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
145 changes: 145 additions & 0 deletions
145
tests/baselines/reference/genericFunctionParametersConditionalType1.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,145 @@ | ||
//// [tests/cases/compiler/genericFunctionParametersConditionalType1.ts] //// | ||
|
||
=== genericFunctionParametersConditionalType1.ts === | ||
// https://github.com/microsoft/TypeScript/issues/62079 | ||
|
||
export {}; | ||
|
||
interface _Map { | ||
>_Map : Symbol(_Map, Decl(genericFunctionParametersConditionalType1.ts, 2, 10)) | ||
|
||
foo: { a: 123 } | ||
>foo : Symbol(_Map.foo, Decl(genericFunctionParametersConditionalType1.ts, 4, 16)) | ||
>a : Symbol(a, Decl(genericFunctionParametersConditionalType1.ts, 5, 8)) | ||
} | ||
|
||
type ModuleSubType = "bar" & { brand: true }; | ||
>ModuleSubType : Symbol(ModuleSubType, Decl(genericFunctionParametersConditionalType1.ts, 6, 1)) | ||
>brand : Symbol(brand, Decl(genericFunctionParametersConditionalType1.ts, 8, 30)) | ||
|
||
type Map = _Map & Record<ModuleSubType, { blah: string }> | ||
>Map : Symbol(Map, Decl(genericFunctionParametersConditionalType1.ts, 8, 45)) | ||
>_Map : Symbol(_Map, Decl(genericFunctionParametersConditionalType1.ts, 2, 10)) | ||
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
>ModuleSubType : Symbol(ModuleSubType, Decl(genericFunctionParametersConditionalType1.ts, 6, 1)) | ||
>blah : Symbol(blah, Decl(genericFunctionParametersConditionalType1.ts, 10, 41)) | ||
|
||
type SubTypeGet< | ||
>SubTypeGet : Symbol(SubTypeGet, Decl(genericFunctionParametersConditionalType1.ts, 10, 57)) | ||
|
||
SubType extends string, | ||
>SubType : Symbol(SubType, Decl(genericFunctionParametersConditionalType1.ts, 12, 16)) | ||
|
||
Map extends Record<SubType, unknown>, | ||
>Map : Symbol(Map, Decl(genericFunctionParametersConditionalType1.ts, 13, 25)) | ||
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
>SubType : Symbol(SubType, Decl(genericFunctionParametersConditionalType1.ts, 12, 16)) | ||
|
||
> = SubType extends unknown | ||
>SubType : Symbol(SubType, Decl(genericFunctionParametersConditionalType1.ts, 12, 16)) | ||
|
||
? { type?: SubType } & Map[SubType] | ||
>type : Symbol(type, Decl(genericFunctionParametersConditionalType1.ts, 16, 3)) | ||
>SubType : Symbol(SubType, Decl(genericFunctionParametersConditionalType1.ts, 12, 16)) | ||
>Map : Symbol(Map, Decl(genericFunctionParametersConditionalType1.ts, 13, 25)) | ||
>SubType : Symbol(SubType, Decl(genericFunctionParametersConditionalType1.ts, 12, 16)) | ||
|
||
: never; | ||
|
||
type TestParameters = Parameters<<T extends "foo" | ModuleSubType>(arg: SubTypeGet<T, Map>) => void> | ||
>TestParameters : Symbol(TestParameters, Decl(genericFunctionParametersConditionalType1.ts, 17, 8)) | ||
>Parameters : Symbol(Parameters, Decl(lib.es5.d.ts, --, --)) | ||
>T : Symbol(T, Decl(genericFunctionParametersConditionalType1.ts, 19, 34)) | ||
>ModuleSubType : Symbol(ModuleSubType, Decl(genericFunctionParametersConditionalType1.ts, 6, 1)) | ||
>arg : Symbol(arg, Decl(genericFunctionParametersConditionalType1.ts, 19, 67)) | ||
>SubTypeGet : Symbol(SubTypeGet, Decl(genericFunctionParametersConditionalType1.ts, 10, 57)) | ||
>T : Symbol(T, Decl(genericFunctionParametersConditionalType1.ts, 19, 34)) | ||
>Map : Symbol(Map, Decl(genericFunctionParametersConditionalType1.ts, 8, 45)) | ||
|
||
declare class Test<T extends "foo" | ModuleSubType> { | ||
>Test : Symbol(Test, Decl(genericFunctionParametersConditionalType1.ts, 19, 100)) | ||
>T : Symbol(T, Decl(genericFunctionParametersConditionalType1.ts, 21, 19)) | ||
>ModuleSubType : Symbol(ModuleSubType, Decl(genericFunctionParametersConditionalType1.ts, 6, 1)) | ||
|
||
constructor(arg: SubTypeGet<T, Map>); | ||
>arg : Symbol(arg, Decl(genericFunctionParametersConditionalType1.ts, 22, 14)) | ||
>SubTypeGet : Symbol(SubTypeGet, Decl(genericFunctionParametersConditionalType1.ts, 10, 57)) | ||
>T : Symbol(T, Decl(genericFunctionParametersConditionalType1.ts, 21, 19)) | ||
>Map : Symbol(Map, Decl(genericFunctionParametersConditionalType1.ts, 8, 45)) | ||
} | ||
|
||
type TestConstructorParameters = ConstructorParameters<typeof Test>; | ||
>TestConstructorParameters : Symbol(TestConstructorParameters, Decl(genericFunctionParametersConditionalType1.ts, 23, 1)) | ||
>ConstructorParameters : Symbol(ConstructorParameters, Decl(lib.es5.d.ts, --, --)) | ||
>Test : Symbol(Test, Decl(genericFunctionParametersConditionalType1.ts, 19, 100)) | ||
|
||
declare class Animal { eat(): void; } | ||
>Animal : Symbol(Animal, Decl(genericFunctionParametersConditionalType1.ts, 25, 68)) | ||
>eat : Symbol(Animal.eat, Decl(genericFunctionParametersConditionalType1.ts, 27, 22)) | ||
|
||
declare class Cat extends Animal { meow(): void; } | ||
>Cat : Symbol(Cat, Decl(genericFunctionParametersConditionalType1.ts, 27, 37)) | ||
>Animal : Symbol(Animal, Decl(genericFunctionParametersConditionalType1.ts, 25, 68)) | ||
>meow : Symbol(Cat.meow, Decl(genericFunctionParametersConditionalType1.ts, 28, 34)) | ||
|
||
declare class Dog extends Animal { bark(): void; } | ||
>Dog : Symbol(Dog, Decl(genericFunctionParametersConditionalType1.ts, 28, 50)) | ||
>Animal : Symbol(Animal, Decl(genericFunctionParametersConditionalType1.ts, 25, 68)) | ||
>bark : Symbol(Dog.bark, Decl(genericFunctionParametersConditionalType1.ts, 29, 34)) | ||
|
||
type WithDistributiveConditionalDirectlyInParam = <T extends Cat | Dog>( | ||
>WithDistributiveConditionalDirectlyInParam : Symbol(WithDistributiveConditionalDirectlyInParam, Decl(genericFunctionParametersConditionalType1.ts, 29, 50)) | ||
>T : Symbol(T, Decl(genericFunctionParametersConditionalType1.ts, 31, 51)) | ||
>Cat : Symbol(Cat, Decl(genericFunctionParametersConditionalType1.ts, 27, 37)) | ||
>Dog : Symbol(Dog, Decl(genericFunctionParametersConditionalType1.ts, 28, 50)) | ||
|
||
arg: T extends unknown ? T : never, | ||
>arg : Symbol(arg, Decl(genericFunctionParametersConditionalType1.ts, 31, 72)) | ||
>T : Symbol(T, Decl(genericFunctionParametersConditionalType1.ts, 31, 51)) | ||
>T : Symbol(T, Decl(genericFunctionParametersConditionalType1.ts, 31, 51)) | ||
|
||
) => void; | ||
|
||
type Result1 = Parameters<WithDistributiveConditionalDirectlyInParam>; | ||
>Result1 : Symbol(Result1, Decl(genericFunctionParametersConditionalType1.ts, 33, 10)) | ||
>Parameters : Symbol(Parameters, Decl(lib.es5.d.ts, --, --)) | ||
>WithDistributiveConditionalDirectlyInParam : Symbol(WithDistributiveConditionalDirectlyInParam, Decl(genericFunctionParametersConditionalType1.ts, 29, 50)) | ||
|
||
type WithDistributiveConditionalNested = <T extends Cat | Dog>( | ||
>WithDistributiveConditionalNested : Symbol(WithDistributiveConditionalNested, Decl(genericFunctionParametersConditionalType1.ts, 35, 70)) | ||
>T : Symbol(T, Decl(genericFunctionParametersConditionalType1.ts, 37, 42)) | ||
>Cat : Symbol(Cat, Decl(genericFunctionParametersConditionalType1.ts, 27, 37)) | ||
>Dog : Symbol(Dog, Decl(genericFunctionParametersConditionalType1.ts, 28, 50)) | ||
|
||
arg: T extends unknown ? { animal: T } : never, | ||
>arg : Symbol(arg, Decl(genericFunctionParametersConditionalType1.ts, 37, 63)) | ||
>T : Symbol(T, Decl(genericFunctionParametersConditionalType1.ts, 37, 42)) | ||
>animal : Symbol(animal, Decl(genericFunctionParametersConditionalType1.ts, 38, 28)) | ||
>T : Symbol(T, Decl(genericFunctionParametersConditionalType1.ts, 37, 42)) | ||
|
||
) => void; | ||
|
||
type Result2 = Parameters<WithDistributiveConditionalNested>; | ||
>Result2 : Symbol(Result2, Decl(genericFunctionParametersConditionalType1.ts, 39, 10)) | ||
>Parameters : Symbol(Parameters, Decl(lib.es5.d.ts, --, --)) | ||
>WithDistributiveConditionalNested : Symbol(WithDistributiveConditionalNested, Decl(genericFunctionParametersConditionalType1.ts, 35, 70)) | ||
|
||
type WithNonDistributiveConditionalNested = <T extends Cat | Dog>( | ||
>WithNonDistributiveConditionalNested : Symbol(WithNonDistributiveConditionalNested, Decl(genericFunctionParametersConditionalType1.ts, 41, 61)) | ||
>T : Symbol(T, Decl(genericFunctionParametersConditionalType1.ts, 43, 45)) | ||
>Cat : Symbol(Cat, Decl(genericFunctionParametersConditionalType1.ts, 27, 37)) | ||
>Dog : Symbol(Dog, Decl(genericFunctionParametersConditionalType1.ts, 28, 50)) | ||
|
||
arg: [T] extends [unknown] ? { animal: T } : never, | ||
>arg : Symbol(arg, Decl(genericFunctionParametersConditionalType1.ts, 43, 66)) | ||
>T : Symbol(T, Decl(genericFunctionParametersConditionalType1.ts, 43, 45)) | ||
>animal : Symbol(animal, Decl(genericFunctionParametersConditionalType1.ts, 44, 32)) | ||
>T : Symbol(T, Decl(genericFunctionParametersConditionalType1.ts, 43, 45)) | ||
|
||
) => void; | ||
|
||
type Result3 = Parameters<WithNonDistributiveConditionalNested>; | ||
>Result3 : Symbol(Result3, Decl(genericFunctionParametersConditionalType1.ts, 45, 10)) | ||
>Parameters : Symbol(Parameters, Decl(lib.es5.d.ts, --, --)) | ||
>WithNonDistributiveConditionalNested : Symbol(WithNonDistributiveConditionalNested, Decl(genericFunctionParametersConditionalType1.ts, 41, 61)) | ||
|
131 changes: 131 additions & 0 deletions
131
tests/baselines/reference/genericFunctionParametersConditionalType1.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,131 @@ | ||
//// [tests/cases/compiler/genericFunctionParametersConditionalType1.ts] //// | ||
|
||
=== genericFunctionParametersConditionalType1.ts === | ||
// https://github.com/microsoft/TypeScript/issues/62079 | ||
|
||
export {}; | ||
|
||
interface _Map { | ||
foo: { a: 123 } | ||
>foo : { a: 123; } | ||
> : ^^^^^ ^^^ | ||
>a : 123 | ||
> : ^^^ | ||
} | ||
|
||
type ModuleSubType = "bar" & { brand: true }; | ||
>ModuleSubType : ModuleSubType | ||
> : ^^^^^^^^^^^^^ | ||
>brand : true | ||
> : ^^^^ | ||
>true : true | ||
> : ^^^^ | ||
|
||
type Map = _Map & Record<ModuleSubType, { blah: string }> | ||
>Map : Map | ||
> : ^^^ | ||
>blah : string | ||
> : ^^^^^^ | ||
|
||
type SubTypeGet< | ||
>SubTypeGet : SubTypeGet<SubType, Map> | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
SubType extends string, | ||
Map extends Record<SubType, unknown>, | ||
> = SubType extends unknown | ||
? { type?: SubType } & Map[SubType] | ||
>type : SubType | undefined | ||
> : ^^^^^^^^^^^^^^^^^^^ | ||
|
||
: never; | ||
|
||
type TestParameters = Parameters<<T extends "foo" | ModuleSubType>(arg: SubTypeGet<T, Map>) => void> | ||
>TestParameters : [arg: { type?: ModuleSubType | undefined; } | ({ type?: "foo" | undefined; } & { a: 123; })] | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^ | ||
>arg : SubTypeGet<T, Map> | ||
> : ^^^^^^^^^^^^^^^^^^ | ||
|
||
declare class Test<T extends "foo" | ModuleSubType> { | ||
>Test : Test<T> | ||
> : ^^^^^^^ | ||
|
||
constructor(arg: SubTypeGet<T, Map>); | ||
>arg : SubTypeGet<T, Map> | ||
> : ^^^^^^^^^^^^^^^^^^ | ||
} | ||
|
||
type TestConstructorParameters = ConstructorParameters<typeof Test>; | ||
>TestConstructorParameters : [arg: { type?: ModuleSubType | undefined; } | ({ type?: "foo" | undefined; } & { a: 123; })] | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^ | ||
>Test : typeof Test | ||
> : ^^^^^^^^^^^ | ||
|
||
declare class Animal { eat(): void; } | ||
>Animal : Animal | ||
> : ^^^^^^ | ||
>eat : () => void | ||
> : ^^^^^^ | ||
|
||
declare class Cat extends Animal { meow(): void; } | ||
>Cat : Cat | ||
> : ^^^ | ||
>Animal : Animal | ||
> : ^^^^^^ | ||
>meow : () => void | ||
> : ^^^^^^ | ||
|
||
declare class Dog extends Animal { bark(): void; } | ||
>Dog : Dog | ||
> : ^^^ | ||
>Animal : Animal | ||
> : ^^^^^^ | ||
>bark : () => void | ||
> : ^^^^^^ | ||
|
||
type WithDistributiveConditionalDirectlyInParam = <T extends Cat | Dog>( | ||
>WithDistributiveConditionalDirectlyInParam : WithDistributiveConditionalDirectlyInParam | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
arg: T extends unknown ? T : never, | ||
>arg : T extends unknown ? T : never | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
) => void; | ||
|
||
type Result1 = Parameters<WithDistributiveConditionalDirectlyInParam>; | ||
>Result1 : [arg: Cat | Dog] | ||
> : ^^^^^^^^^^^^^^^^ | ||
|
||
type WithDistributiveConditionalNested = <T extends Cat | Dog>( | ||
>WithDistributiveConditionalNested : WithDistributiveConditionalNested | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
arg: T extends unknown ? { animal: T } : never, | ||
>arg : T extends unknown ? { animal: T; } : never | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^ | ||
>animal : T | ||
> : ^ | ||
|
||
) => void; | ||
|
||
type Result2 = Parameters<WithDistributiveConditionalNested>; | ||
>Result2 : [arg: { animal: Cat; } | { animal: Dog; }] | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
type WithNonDistributiveConditionalNested = <T extends Cat | Dog>( | ||
>WithNonDistributiveConditionalNested : WithNonDistributiveConditionalNested | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
arg: [T] extends [unknown] ? { animal: T } : never, | ||
>arg : [T] extends [unknown] ? { animal: T; } : never | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^ | ||
>animal : T | ||
> : ^ | ||
|
||
) => void; | ||
|
||
type Result3 = Parameters<WithNonDistributiveConditionalNested>; | ||
>Result3 : [arg: { animal: Cat | Dog; }] | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
Oops, something went wrong.
Oops, something went wrong.
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.
I don't quite like how this turned out - I probably should move this thing to a separate
InferenceInfo
property (similar totopLevel
) instead of shoving it into.priority
.That said, those changes would be cosmetic and wouldn't really change the idea behind this fix. In the meantime, I'd love to see what user/top tests have to say about this (cc @jakebailey )