-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Improve logic that chooses co- vs. contra-variant inferences #57909
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 11 commits into
microsoft:main
from
Andarist:tweak-co-contra-inference
Jun 17, 2024
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
80491eb
Improve logic that chooses co- vs. contra-variant inferences
Andarist f78cea6
rename test file
Andarist 237aba6
ignore covariant any inferences when contravariant candidates are ava…
Andarist 6688f3c
Merge remote-tracking branch 'origin/main' into tweak-co-contra-infer…
Andarist 03d0eb1
Merge remote-tracking branch 'origin/main' into tweak-co-contra-infer…
Andarist 810943a
Merge remote-tracking branch 'origin/main' into tweak-co-contra-infer…
Andarist b715e32
add an extra test case
Andarist 2226e0b
tweak comment
Andarist 1c3f6c9
Merge remote-tracking branch 'origin/main' into tweak-co-contra-infer…
Andarist 169d720
update baseline
Andarist 91d69a1
update a baseline
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
77 changes: 77 additions & 0 deletions
77
tests/baselines/reference/coAndContraVariantInferences7.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,77 @@ | ||
//// [tests/cases/compiler/coAndContraVariantInferences7.ts] //// | ||
|
||
=== coAndContraVariantInferences7.ts === | ||
type Request<TSchema extends Schema> = { | ||
>Request : Symbol(Request, Decl(coAndContraVariantInferences7.ts, 0, 0)) | ||
>TSchema : Symbol(TSchema, Decl(coAndContraVariantInferences7.ts, 0, 13)) | ||
>Schema : Symbol(Schema, Decl(coAndContraVariantInferences7.ts, 2, 2)) | ||
|
||
query: TSchema["query"]; | ||
>query : Symbol(query, Decl(coAndContraVariantInferences7.ts, 0, 40)) | ||
>TSchema : Symbol(TSchema, Decl(coAndContraVariantInferences7.ts, 0, 13)) | ||
|
||
}; | ||
|
||
type Schema = { query?: unknown; body?: unknown }; | ||
>Schema : Symbol(Schema, Decl(coAndContraVariantInferences7.ts, 2, 2)) | ||
>query : Symbol(query, Decl(coAndContraVariantInferences7.ts, 4, 15)) | ||
>body : Symbol(body, Decl(coAndContraVariantInferences7.ts, 4, 32)) | ||
|
||
declare function route<TSchema extends Schema>(obj: { | ||
>route : Symbol(route, Decl(coAndContraVariantInferences7.ts, 4, 50)) | ||
>TSchema : Symbol(TSchema, Decl(coAndContraVariantInferences7.ts, 6, 23)) | ||
>Schema : Symbol(Schema, Decl(coAndContraVariantInferences7.ts, 2, 2)) | ||
>obj : Symbol(obj, Decl(coAndContraVariantInferences7.ts, 6, 47)) | ||
|
||
pre: (a: TSchema) => void; | ||
>pre : Symbol(pre, Decl(coAndContraVariantInferences7.ts, 6, 53)) | ||
>a : Symbol(a, Decl(coAndContraVariantInferences7.ts, 7, 8)) | ||
>TSchema : Symbol(TSchema, Decl(coAndContraVariantInferences7.ts, 6, 23)) | ||
|
||
schema: TSchema; | ||
>schema : Symbol(schema, Decl(coAndContraVariantInferences7.ts, 7, 28)) | ||
>TSchema : Symbol(TSchema, Decl(coAndContraVariantInferences7.ts, 6, 23)) | ||
|
||
handle: (req: Request<TSchema>) => void; | ||
>handle : Symbol(handle, Decl(coAndContraVariantInferences7.ts, 8, 18)) | ||
>req : Symbol(req, Decl(coAndContraVariantInferences7.ts, 9, 11)) | ||
>Request : Symbol(Request, Decl(coAndContraVariantInferences7.ts, 0, 0)) | ||
>TSchema : Symbol(TSchema, Decl(coAndContraVariantInferences7.ts, 6, 23)) | ||
|
||
}): void; | ||
|
||
const validate = (_: { query?: unknown; body?: unknown }) => {}; | ||
>validate : Symbol(validate, Decl(coAndContraVariantInferences7.ts, 12, 5)) | ||
>_ : Symbol(_, Decl(coAndContraVariantInferences7.ts, 12, 18)) | ||
>query : Symbol(query, Decl(coAndContraVariantInferences7.ts, 12, 22)) | ||
>body : Symbol(body, Decl(coAndContraVariantInferences7.ts, 12, 39)) | ||
|
||
route({ | ||
>route : Symbol(route, Decl(coAndContraVariantInferences7.ts, 4, 50)) | ||
|
||
pre: validate, | ||
>pre : Symbol(pre, Decl(coAndContraVariantInferences7.ts, 14, 7)) | ||
>validate : Symbol(validate, Decl(coAndContraVariantInferences7.ts, 12, 5)) | ||
|
||
schema: { | ||
>schema : Symbol(schema, Decl(coAndContraVariantInferences7.ts, 15, 16)) | ||
|
||
query: "", | ||
>query : Symbol(query, Decl(coAndContraVariantInferences7.ts, 16, 11)) | ||
|
||
}, | ||
handle: (req) => { | ||
>handle : Symbol(handle, Decl(coAndContraVariantInferences7.ts, 18, 4)) | ||
>req : Symbol(req, Decl(coAndContraVariantInferences7.ts, 19, 11)) | ||
|
||
const test: string = req.query; | ||
>test : Symbol(test, Decl(coAndContraVariantInferences7.ts, 20, 9)) | ||
>req.query : Symbol(query, Decl(coAndContraVariantInferences7.ts, 0, 40)) | ||
>req : Symbol(req, Decl(coAndContraVariantInferences7.ts, 19, 11)) | ||
>query : Symbol(query, Decl(coAndContraVariantInferences7.ts, 0, 40)) | ||
|
||
}, | ||
}); | ||
|
||
export {}; | ||
|
107 changes: 107 additions & 0 deletions
107
tests/baselines/reference/coAndContraVariantInferences7.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,107 @@ | ||
//// [tests/cases/compiler/coAndContraVariantInferences7.ts] //// | ||
|
||
=== coAndContraVariantInferences7.ts === | ||
type Request<TSchema extends Schema> = { | ||
>Request : Request<TSchema> | ||
> : ^^^^^^^^^^^^^^^^ | ||
|
||
query: TSchema["query"]; | ||
>query : TSchema["query"] | ||
> : ^^^^^^^^^^^^^^^^ | ||
|
||
}; | ||
|
||
type Schema = { query?: unknown; body?: unknown }; | ||
>Schema : Schema | ||
> : ^^^^^^ | ||
>query : unknown | ||
> : ^^^^^^^ | ||
>body : unknown | ||
> : ^^^^^^^ | ||
|
||
declare function route<TSchema extends Schema>(obj: { | ||
>route : <TSchema extends Schema>(obj: { pre: (a: TSchema) => void; schema: TSchema; handle: (req: Request<TSchema>) => void; }) => void | ||
> : ^ ^^^^^^^^^^^^^^^^^ ^^ ^^^^^ | ||
>obj : { pre: (a: TSchema) => void; schema: TSchema; handle: (req: Request<TSchema>) => void; } | ||
> : ^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^ ^^^ | ||
|
||
pre: (a: TSchema) => void; | ||
>pre : (a: TSchema) => void | ||
> : ^ ^^ ^^^^^ | ||
>a : TSchema | ||
> : ^^^^^^^ | ||
|
||
schema: TSchema; | ||
>schema : TSchema | ||
> : ^^^^^^^ | ||
|
||
handle: (req: Request<TSchema>) => void; | ||
>handle : (req: Request<TSchema>) => void | ||
> : ^ ^^ ^^^^^ | ||
>req : Request<TSchema> | ||
> : ^^^^^^^^^^^^^^^^ | ||
|
||
}): void; | ||
|
||
const validate = (_: { query?: unknown; body?: unknown }) => {}; | ||
>validate : (_: { query?: unknown; body?: unknown; }) => void | ||
> : ^ ^^ ^^^^^^^^^ | ||
>(_: { query?: unknown; body?: unknown }) => {} : (_: { query?: unknown; body?: unknown; }) => void | ||
> : ^ ^^ ^^^^^^^^^ | ||
>_ : { query?: unknown; body?: unknown; } | ||
> : ^^^^^^^^^^ ^^^^^^^^^ ^^^ | ||
>query : unknown | ||
> : ^^^^^^^ | ||
>body : unknown | ||
> : ^^^^^^^ | ||
|
||
route({ | ||
>route({ pre: validate, schema: { query: "", }, handle: (req) => { const test: string = req.query; },}) : void | ||
> : ^^^^ | ||
>route : <TSchema extends Schema>(obj: { pre: (a: TSchema) => void; schema: TSchema; handle: (req: Request<TSchema>) => void; }) => void | ||
> : ^ ^^^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^ | ||
>{ pre: validate, schema: { query: "", }, handle: (req) => { const test: string = req.query; },} : { pre: (_: { query?: unknown; body?: unknown; }) => void; schema: { query: string; }; handle: (req: Request<{ query: string; }>) => void; } | ||
> : ^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
pre: validate, | ||
>pre : (_: { query?: unknown; body?: unknown; }) => void | ||
> : ^ ^^ ^^^^^^^^^ | ||
>validate : (_: { query?: unknown; body?: unknown; }) => void | ||
> : ^ ^^ ^^^^^^^^^ | ||
|
||
schema: { | ||
>schema : { query: string; } | ||
> : ^^^^^^^^^^^^^^^^^^ | ||
>{ query: "", } : { query: string; } | ||
> : ^^^^^^^^^^^^^^^^^^ | ||
|
||
query: "", | ||
>query : string | ||
> : ^^^^^^ | ||
>"" : "" | ||
> : ^^ | ||
|
||
}, | ||
handle: (req) => { | ||
>handle : (req: Request<{ query: string; }>) => void | ||
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
>(req) => { const test: string = req.query; } : (req: Request<{ query: string; }>) => void | ||
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
>req : Request<{ query: string; }> | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
const test: string = req.query; | ||
>test : string | ||
> : ^^^^^^ | ||
>req.query : string | ||
> : ^^^^^^ | ||
>req : Request<{ query: string; }> | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
>query : string | ||
> : ^^^^^^ | ||
|
||
}, | ||
}); | ||
|
||
export {}; | ||
|
29 changes: 29 additions & 0 deletions
29
tests/baselines/reference/coAndContraVariantInferences8.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,29 @@ | ||
//// [tests/cases/compiler/coAndContraVariantInferences8.ts] //// | ||
|
||
=== coAndContraVariantInferences8.ts === | ||
// https://github.com/microsoft/TypeScript/issues/58468 | ||
|
||
declare const fn: (() => void) | ((a: number) => void); | ||
>fn : Symbol(fn, Decl(coAndContraVariantInferences8.ts, 2, 13)) | ||
>a : Symbol(a, Decl(coAndContraVariantInferences8.ts, 2, 35)) | ||
|
||
declare const x: number; | ||
>x : Symbol(x, Decl(coAndContraVariantInferences8.ts, 4, 13)) | ||
|
||
declare const y: any; | ||
>y : Symbol(y, Decl(coAndContraVariantInferences8.ts, 5, 13)) | ||
|
||
fn.call(null, x); | ||
>fn.call : Symbol(CallableFunction.call, Decl(lib.es5.d.ts, --, --)) | ||
>fn : Symbol(fn, Decl(coAndContraVariantInferences8.ts, 2, 13)) | ||
>call : Symbol(CallableFunction.call, Decl(lib.es5.d.ts, --, --)) | ||
>x : Symbol(x, Decl(coAndContraVariantInferences8.ts, 4, 13)) | ||
|
||
fn.call(null, y); | ||
>fn.call : Symbol(CallableFunction.call, Decl(lib.es5.d.ts, --, --)) | ||
>fn : Symbol(fn, Decl(coAndContraVariantInferences8.ts, 2, 13)) | ||
>call : Symbol(CallableFunction.call, Decl(lib.es5.d.ts, --, --)) | ||
>y : Symbol(y, Decl(coAndContraVariantInferences8.ts, 5, 13)) | ||
|
||
export {}; | ||
|
43 changes: 43 additions & 0 deletions
43
tests/baselines/reference/coAndContraVariantInferences8.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,43 @@ | ||
//// [tests/cases/compiler/coAndContraVariantInferences8.ts] //// | ||
|
||
=== coAndContraVariantInferences8.ts === | ||
// https://github.com/microsoft/TypeScript/issues/58468 | ||
|
||
declare const fn: (() => void) | ((a: number) => void); | ||
>fn : (() => void) | ((a: number) => void) | ||
> : ^^^^^^^ ^^^^^^ ^^ ^^^^^ ^ | ||
>a : number | ||
> : ^^^^^^ | ||
|
||
declare const x: number; | ||
>x : number | ||
> : ^^^^^^ | ||
|
||
declare const y: any; | ||
>y : any | ||
|
||
fn.call(null, x); | ||
>fn.call(null, x) : void | ||
> : ^^^^ | ||
>fn.call : <T, A extends any[], R>(this: (this: T, ...args: A) => R, thisArg: T, ...args: A) => R | ||
> : ^ ^^ ^^^^^^^^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^ ^^ ^^^^^^ | ||
>fn : (() => void) | ((a: number) => void) | ||
> : ^^^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^^ | ||
>call : <T, A extends any[], R>(this: (this: T, ...args: A) => R, thisArg: T, ...args: A) => R | ||
> : ^ ^^ ^^^^^^^^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^ ^^ ^^^^^^ | ||
>x : number | ||
> : ^^^^^^ | ||
|
||
fn.call(null, y); | ||
>fn.call(null, y) : void | ||
> : ^^^^ | ||
>fn.call : <T, A extends any[], R>(this: (this: T, ...args: A) => R, thisArg: T, ...args: A) => R | ||
> : ^ ^^ ^^^^^^^^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^ ^^ ^^^^^^ | ||
>fn : (() => void) | ((a: number) => void) | ||
> : ^^^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^^ | ||
>call : <T, A extends any[], R>(this: (this: T, ...args: A) => R, thisArg: T, ...args: A) => R | ||
> : ^ ^^ ^^^^^^^^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^ ^^ ^^^^^^ | ||
>y : any | ||
|
||
export {}; | ||
|
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,28 @@ | ||
// @strict: true | ||
// @noEmit: true | ||
|
||
type Request<TSchema extends Schema> = { | ||
query: TSchema["query"]; | ||
}; | ||
|
||
type Schema = { query?: unknown; body?: unknown }; | ||
|
||
declare function route<TSchema extends Schema>(obj: { | ||
pre: (a: TSchema) => void; | ||
schema: TSchema; | ||
handle: (req: Request<TSchema>) => void; | ||
}): void; | ||
|
||
const validate = (_: { query?: unknown; body?: unknown }) => {}; | ||
|
||
route({ | ||
pre: validate, | ||
schema: { | ||
query: "", | ||
}, | ||
handle: (req) => { | ||
const test: string = req.query; | ||
}, | ||
}); | ||
|
||
export {}; |
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,14 @@ | ||
// @strict: true | ||
// @noEmit: true | ||
|
||
// https://github.com/microsoft/TypeScript/issues/58468 | ||
|
||
declare const fn: (() => void) | ((a: number) => void); | ||
|
||
declare const x: number; | ||
declare const y: any; | ||
|
||
fn.call(null, x); | ||
fn.call(null, y); | ||
|
||
export {}; |
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.
Uh oh!
There was an error while loading. Please reload this page.