-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Improve contextually typed parameters with initializers #56506
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
gabritto
merged 6 commits into
microsoft:main
from
Andarist:fix/contextual-never-param-type-vs-initializer
Dec 5, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6aabb88
Prefer the initializer type over contextual `never` parameter type
Andarist aff1cfa
go beyond `never`
Andarist f55e403
Widen after checking against the contextual type
Andarist bcbcd65
Merge remote-tracking branch 'origin/main' into fix/contextual-never-…
Andarist 73d964c
tweak the widening moment again
Andarist 7a7f6aa
tweak test case
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
6 changes: 3 additions & 3 deletions
6
...ypedParametersWithInitializers.errors.txt → ...pedParametersWithInitializers1.errors.txt
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
374 changes: 187 additions & 187 deletions
374
...lyTypedParametersWithInitializers.symbols → ...yTypedParametersWithInitializers1.symbols
Large diffs are not rendered by default.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
...allyTypedParametersWithInitializers.types → ...llyTypedParametersWithInitializers1.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
58 changes: 58 additions & 0 deletions
58
tests/baselines/reference/contextuallyTypedParametersWithInitializers2.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,58 @@ | ||
//// [tests/cases/compiler/contextuallyTypedParametersWithInitializers2.ts] //// | ||
|
||
=== contextuallyTypedParametersWithInitializers2.ts === | ||
declare function test1< | ||
>test1 : Symbol(test1, Decl(contextuallyTypedParametersWithInitializers2.ts, 0, 0)) | ||
|
||
TContext, | ||
>TContext : Symbol(TContext, Decl(contextuallyTypedParametersWithInitializers2.ts, 0, 23)) | ||
|
||
TMethods extends Record<string, (ctx: TContext, ...args: never[]) => unknown>, | ||
>TMethods : Symbol(TMethods, Decl(contextuallyTypedParametersWithInitializers2.ts, 1, 11)) | ||
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
>ctx : Symbol(ctx, Decl(contextuallyTypedParametersWithInitializers2.ts, 2, 35)) | ||
>TContext : Symbol(TContext, Decl(contextuallyTypedParametersWithInitializers2.ts, 0, 23)) | ||
>args : Symbol(args, Decl(contextuallyTypedParametersWithInitializers2.ts, 2, 49)) | ||
|
||
>(context: TContext, methods: TMethods): void; | ||
>context : Symbol(context, Decl(contextuallyTypedParametersWithInitializers2.ts, 3, 2)) | ||
>TContext : Symbol(TContext, Decl(contextuallyTypedParametersWithInitializers2.ts, 0, 23)) | ||
>methods : Symbol(methods, Decl(contextuallyTypedParametersWithInitializers2.ts, 3, 20)) | ||
>TMethods : Symbol(TMethods, Decl(contextuallyTypedParametersWithInitializers2.ts, 1, 11)) | ||
|
||
test1( | ||
>test1 : Symbol(test1, Decl(contextuallyTypedParametersWithInitializers2.ts, 0, 0)) | ||
{ | ||
count: 0, | ||
>count : Symbol(count, Decl(contextuallyTypedParametersWithInitializers2.ts, 6, 3)) | ||
|
||
}, | ||
{ | ||
checkLimit: (ctx, max = 500) => {}, | ||
>checkLimit : Symbol(checkLimit, Decl(contextuallyTypedParametersWithInitializers2.ts, 9, 3)) | ||
>ctx : Symbol(ctx, Decl(contextuallyTypedParametersWithInitializers2.ts, 10, 17)) | ||
>max : Symbol(max, Decl(contextuallyTypedParametersWithInitializers2.ts, 10, 21)) | ||
|
||
hasAccess: (ctx, user: { name: string }) => {}, | ||
>hasAccess : Symbol(hasAccess, Decl(contextuallyTypedParametersWithInitializers2.ts, 10, 39)) | ||
>ctx : Symbol(ctx, Decl(contextuallyTypedParametersWithInitializers2.ts, 11, 16)) | ||
>user : Symbol(user, Decl(contextuallyTypedParametersWithInitializers2.ts, 11, 20)) | ||
>name : Symbol(name, Decl(contextuallyTypedParametersWithInitializers2.ts, 11, 28)) | ||
|
||
}, | ||
); | ||
|
||
declare const num: number; | ||
>num : Symbol(num, Decl(contextuallyTypedParametersWithInitializers2.ts, 15, 13)) | ||
|
||
const test2: (arg: 1 | 2) => void = (arg = num) => {}; | ||
>test2 : Symbol(test2, Decl(contextuallyTypedParametersWithInitializers2.ts, 16, 5)) | ||
>arg : Symbol(arg, Decl(contextuallyTypedParametersWithInitializers2.ts, 16, 14)) | ||
>arg : Symbol(arg, Decl(contextuallyTypedParametersWithInitializers2.ts, 16, 37)) | ||
>num : Symbol(num, Decl(contextuallyTypedParametersWithInitializers2.ts, 15, 13)) | ||
|
||
const test3: (arg: number) => void = (arg = 1) => {}; | ||
>test3 : Symbol(test3, Decl(contextuallyTypedParametersWithInitializers2.ts, 18, 5)) | ||
>arg : Symbol(arg, Decl(contextuallyTypedParametersWithInitializers2.ts, 18, 14)) | ||
>arg : Symbol(arg, Decl(contextuallyTypedParametersWithInitializers2.ts, 18, 38)) | ||
|
63 changes: 63 additions & 0 deletions
63
tests/baselines/reference/contextuallyTypedParametersWithInitializers2.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,63 @@ | ||
//// [tests/cases/compiler/contextuallyTypedParametersWithInitializers2.ts] //// | ||
|
||
=== contextuallyTypedParametersWithInitializers2.ts === | ||
declare function test1< | ||
>test1 : <TContext, TMethods extends Record<string, (ctx: TContext, ...args: never[]) => unknown>>(context: TContext, methods: TMethods) => void | ||
|
||
TContext, | ||
TMethods extends Record<string, (ctx: TContext, ...args: never[]) => unknown>, | ||
>ctx : TContext | ||
>args : never[] | ||
|
||
>(context: TContext, methods: TMethods): void; | ||
>context : TContext | ||
>methods : TMethods | ||
|
||
test1( | ||
>test1( { count: 0, }, { checkLimit: (ctx, max = 500) => {}, hasAccess: (ctx, user: { name: string }) => {}, },) : void | ||
>test1 : <TContext, TMethods extends Record<string, (ctx: TContext, ...args: never[]) => unknown>>(context: TContext, methods: TMethods) => void | ||
{ | ||
>{ count: 0, } : { count: number; } | ||
|
||
count: 0, | ||
>count : number | ||
>0 : 0 | ||
|
||
}, | ||
{ | ||
>{ checkLimit: (ctx, max = 500) => {}, hasAccess: (ctx, user: { name: string }) => {}, } : { checkLimit: (ctx: { count: number; }, max?: number) => void; hasAccess: (ctx: { count: number; }, user: { name: string;}) => void; } | ||
|
||
checkLimit: (ctx, max = 500) => {}, | ||
>checkLimit : (ctx: { count: number; }, max?: number) => void | ||
>(ctx, max = 500) => {} : (ctx: { count: number; }, max?: number) => void | ||
>ctx : { count: number; } | ||
>max : number | ||
>500 : 500 | ||
|
||
hasAccess: (ctx, user: { name: string }) => {}, | ||
>hasAccess : (ctx: { count: number; }, user: { name: string;}) => void | ||
>(ctx, user: { name: string }) => {} : (ctx: { count: number; }, user: { name: string;}) => void | ||
>ctx : { count: number; } | ||
>user : { name: string; } | ||
>name : string | ||
|
||
}, | ||
); | ||
|
||
declare const num: number; | ||
>num : number | ||
|
||
const test2: (arg: 1 | 2) => void = (arg = num) => {}; | ||
>test2 : (arg: 1 | 2) => void | ||
>arg : 1 | 2 | ||
>(arg = num) => {} : (arg?: number) => void | ||
>arg : number | ||
>num : number | ||
|
||
const test3: (arg: number) => void = (arg = 1) => {}; | ||
>test3 : (arg: number) => void | ||
>arg : number | ||
>(arg = 1) => {} : (arg?: number) => void | ||
>arg : number | ||
>1 : 1 | ||
|
40 changes: 40 additions & 0 deletions
40
tests/baselines/reference/contextuallyTypedParametersWithInitializers3.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,40 @@ | ||
//// [tests/cases/compiler/contextuallyTypedParametersWithInitializers3.ts] //// | ||
|
||
=== contextuallyTypedParametersWithInitializers3.ts === | ||
type CanvasDirection = "RIGHT" | "LEFT"; | ||
>CanvasDirection : Symbol(CanvasDirection, Decl(contextuallyTypedParametersWithInitializers3.ts, 0, 0)) | ||
|
||
interface GraphActions { | ||
>GraphActions : Symbol(GraphActions, Decl(contextuallyTypedParametersWithInitializers3.ts, 0, 40)) | ||
|
||
setDirection: (direction: CanvasDirection) => void; | ||
>setDirection : Symbol(GraphActions.setDirection, Decl(contextuallyTypedParametersWithInitializers3.ts, 2, 24)) | ||
>direction : Symbol(direction, Decl(contextuallyTypedParametersWithInitializers3.ts, 3, 17)) | ||
>CanvasDirection : Symbol(CanvasDirection, Decl(contextuallyTypedParametersWithInitializers3.ts, 0, 0)) | ||
} | ||
|
||
export declare function create<T>(config: T): void; | ||
>create : Symbol(create, Decl(contextuallyTypedParametersWithInitializers3.ts, 4, 1)) | ||
>T : Symbol(T, Decl(contextuallyTypedParametersWithInitializers3.ts, 6, 31)) | ||
>config : Symbol(config, Decl(contextuallyTypedParametersWithInitializers3.ts, 6, 34)) | ||
>T : Symbol(T, Decl(contextuallyTypedParametersWithInitializers3.ts, 6, 31)) | ||
|
||
declare function takesDirection(direction: CanvasDirection): void; | ||
>takesDirection : Symbol(takesDirection, Decl(contextuallyTypedParametersWithInitializers3.ts, 6, 51)) | ||
>direction : Symbol(direction, Decl(contextuallyTypedParametersWithInitializers3.ts, 8, 32)) | ||
>CanvasDirection : Symbol(CanvasDirection, Decl(contextuallyTypedParametersWithInitializers3.ts, 0, 0)) | ||
|
||
create<GraphActions>({ | ||
>create : Symbol(create, Decl(contextuallyTypedParametersWithInitializers3.ts, 4, 1)) | ||
>GraphActions : Symbol(GraphActions, Decl(contextuallyTypedParametersWithInitializers3.ts, 0, 40)) | ||
|
||
setDirection: (direction = "RIGHT") => { | ||
>setDirection : Symbol(setDirection, Decl(contextuallyTypedParametersWithInitializers3.ts, 10, 22)) | ||
>direction : Symbol(direction, Decl(contextuallyTypedParametersWithInitializers3.ts, 11, 17)) | ||
|
||
takesDirection(direction); | ||
>takesDirection : Symbol(takesDirection, Decl(contextuallyTypedParametersWithInitializers3.ts, 6, 51)) | ||
>direction : Symbol(direction, Decl(contextuallyTypedParametersWithInitializers3.ts, 11, 17)) | ||
|
||
}, | ||
}); |
38 changes: 38 additions & 0 deletions
38
tests/baselines/reference/contextuallyTypedParametersWithInitializers3.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,38 @@ | ||
//// [tests/cases/compiler/contextuallyTypedParametersWithInitializers3.ts] //// | ||
|
||
=== contextuallyTypedParametersWithInitializers3.ts === | ||
type CanvasDirection = "RIGHT" | "LEFT"; | ||
>CanvasDirection : "RIGHT" | "LEFT" | ||
|
||
interface GraphActions { | ||
setDirection: (direction: CanvasDirection) => void; | ||
>setDirection : (direction: CanvasDirection) => void | ||
>direction : CanvasDirection | ||
} | ||
|
||
export declare function create<T>(config: T): void; | ||
>create : <T>(config: T) => void | ||
>config : T | ||
|
||
declare function takesDirection(direction: CanvasDirection): void; | ||
>takesDirection : (direction: CanvasDirection) => void | ||
>direction : CanvasDirection | ||
|
||
create<GraphActions>({ | ||
>create<GraphActions>({ setDirection: (direction = "RIGHT") => { takesDirection(direction); },}) : void | ||
>create : <T>(config: T) => void | ||
>{ setDirection: (direction = "RIGHT") => { takesDirection(direction); },} : { setDirection: (direction?: CanvasDirection) => void; } | ||
|
||
setDirection: (direction = "RIGHT") => { | ||
>setDirection : (direction?: CanvasDirection) => void | ||
>(direction = "RIGHT") => { takesDirection(direction); } : (direction?: CanvasDirection) => void | ||
>direction : CanvasDirection | ||
>"RIGHT" : "RIGHT" | ||
|
||
takesDirection(direction); | ||
>takesDirection(direction) : void | ||
>takesDirection : (direction: CanvasDirection) => void | ||
>direction : CanvasDirection | ||
|
||
}, | ||
}); |
38 changes: 38 additions & 0 deletions
38
tests/baselines/reference/contextuallyTypedParametersWithInitializers4.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,38 @@ | ||
//// [tests/cases/compiler/contextuallyTypedParametersWithInitializers4.ts] //// | ||
|
||
=== contextuallyTypedParametersWithInitializers4.ts === | ||
declare function test< | ||
>test : Symbol(test, Decl(contextuallyTypedParametersWithInitializers4.ts, 0, 0)) | ||
|
||
TContext, | ||
>TContext : Symbol(TContext, Decl(contextuallyTypedParametersWithInitializers4.ts, 0, 22)) | ||
|
||
TMethods extends Record<string, (ctx: TContext, ...args: (1 | 2)[]) => unknown>, | ||
>TMethods : Symbol(TMethods, Decl(contextuallyTypedParametersWithInitializers4.ts, 1, 11)) | ||
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
>ctx : Symbol(ctx, Decl(contextuallyTypedParametersWithInitializers4.ts, 2, 35)) | ||
>TContext : Symbol(TContext, Decl(contextuallyTypedParametersWithInitializers4.ts, 0, 22)) | ||
>args : Symbol(args, Decl(contextuallyTypedParametersWithInitializers4.ts, 2, 49)) | ||
|
||
>(context: TContext, methods: TMethods): void; | ||
>context : Symbol(context, Decl(contextuallyTypedParametersWithInitializers4.ts, 3, 2)) | ||
>TContext : Symbol(TContext, Decl(contextuallyTypedParametersWithInitializers4.ts, 0, 22)) | ||
>methods : Symbol(methods, Decl(contextuallyTypedParametersWithInitializers4.ts, 3, 20)) | ||
>TMethods : Symbol(TMethods, Decl(contextuallyTypedParametersWithInitializers4.ts, 1, 11)) | ||
|
||
test( | ||
>test : Symbol(test, Decl(contextuallyTypedParametersWithInitializers4.ts, 0, 0)) | ||
{ | ||
count: 0, | ||
>count : Symbol(count, Decl(contextuallyTypedParametersWithInitializers4.ts, 6, 3)) | ||
|
||
}, | ||
{ | ||
checkLimit: (ctx, max = 3) => {}, | ||
>checkLimit : Symbol(checkLimit, Decl(contextuallyTypedParametersWithInitializers4.ts, 9, 3)) | ||
>ctx : Symbol(ctx, Decl(contextuallyTypedParametersWithInitializers4.ts, 10, 17)) | ||
>max : Symbol(max, Decl(contextuallyTypedParametersWithInitializers4.ts, 10, 21)) | ||
|
||
}, | ||
); | ||
|
39 changes: 39 additions & 0 deletions
39
tests/baselines/reference/contextuallyTypedParametersWithInitializers4.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,39 @@ | ||
//// [tests/cases/compiler/contextuallyTypedParametersWithInitializers4.ts] //// | ||
|
||
=== contextuallyTypedParametersWithInitializers4.ts === | ||
declare function test< | ||
>test : <TContext, TMethods extends Record<string, (ctx: TContext, ...args: (1 | 2)[]) => unknown>>(context: TContext, methods: TMethods) => void | ||
|
||
TContext, | ||
TMethods extends Record<string, (ctx: TContext, ...args: (1 | 2)[]) => unknown>, | ||
>ctx : TContext | ||
>args : (1 | 2)[] | ||
|
||
>(context: TContext, methods: TMethods): void; | ||
>context : TContext | ||
>methods : TMethods | ||
|
||
test( | ||
>test( { count: 0, }, { checkLimit: (ctx, max = 3) => {}, },) : void | ||
>test : <TContext, TMethods extends Record<string, (ctx: TContext, ...args: (1 | 2)[]) => unknown>>(context: TContext, methods: TMethods) => void | ||
{ | ||
>{ count: 0, } : { count: number; } | ||
|
||
count: 0, | ||
>count : number | ||
>0 : 0 | ||
|
||
}, | ||
{ | ||
>{ checkLimit: (ctx, max = 3) => {}, } : { checkLimit: (ctx: { count: number; }, max?: number) => void; } | ||
|
||
checkLimit: (ctx, max = 3) => {}, | ||
>checkLimit : (ctx: { count: number; }, max?: number) => void | ||
>(ctx, max = 3) => {} : (ctx: { count: number; }, max?: number) => void | ||
>ctx : { count: number; } | ||
>max : number | ||
>3 : 3 | ||
|
||
}, | ||
); | ||
|
File renamed without changes.
22 changes: 22 additions & 0 deletions
22
tests/cases/compiler/contextuallyTypedParametersWithInitializers2.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,22 @@ | ||
// @strict: true | ||
// @noEmit: true | ||
|
||
declare function test1< | ||
TContext, | ||
TMethods extends Record<string, (ctx: TContext, ...args: never[]) => unknown>, | ||
>(context: TContext, methods: TMethods): void; | ||
|
||
test1( | ||
{ | ||
count: 0, | ||
}, | ||
{ | ||
checkLimit: (ctx, max = 500) => {}, | ||
hasAccess: (ctx, user: { name: string }) => {}, | ||
}, | ||
); | ||
|
||
declare const num: number; | ||
const test2: (arg: 1 | 2) => void = (arg = num) => {}; | ||
|
||
const test3: (arg: number) => void = (arg = 1) => {}; |
18 changes: 18 additions & 0 deletions
18
tests/cases/compiler/contextuallyTypedParametersWithInitializers3.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,18 @@ | ||
// @strict: true | ||
// @noEmit: true | ||
|
||
type CanvasDirection = "RIGHT" | "LEFT"; | ||
|
||
interface GraphActions { | ||
setDirection: (direction: CanvasDirection) => void; | ||
} | ||
|
||
export declare function create<T>(config: T): void; | ||
|
||
declare function takesDirection(direction: CanvasDirection): void; | ||
|
||
create<GraphActions>({ | ||
setDirection: (direction = "RIGHT") => { | ||
takesDirection(direction); | ||
}, | ||
}); |
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 think you want to use the widened initializer type in this second condition (
isTypeAssignableTo(type, initializerType)
), because consider the following example:The initializer type is
3
, I think, and the contextual type is1 | 2
. We will still error on that case, and we still want to pick typenumber
over type1 | 2
.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.
done