-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Improve contextual typing of ending tuple elements #53036
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
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5f947f8
Improve contextual typing of ending tuple elements
ahejlsberg adeafcc
Accept new baselines
ahejlsberg b2f0543
Add tests
ahejlsberg a3d6017
Merge branch 'main' into fix52846
ahejlsberg bad7048
Merge branch 'main' into fix52846
ahejlsberg 53ad72c
Accept new baselines
ahejlsberg da43359
Merge branch 'main' into fix52846
ahejlsberg 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
72 changes: 72 additions & 0 deletions
72
tests/baselines/reference/contextualTypeTupleEnd.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
tests/cases/conformance/types/tuple/contextualTypeTupleEnd.ts(8,1): error TS2345: Argument of type '[]' is not assignable to parameter of type '[...((arg: number) => void)[], (arg: string) => void]'. | ||
Source has 0 element(s) but target requires 1. | ||
tests/cases/conformance/types/tuple/contextualTypeTupleEnd.ts(13,7): error TS2322: Type '[]' is not assignable to type 'Funcs'. | ||
Source has 0 element(s) but target requires 1. | ||
tests/cases/conformance/types/tuple/contextualTypeTupleEnd.ts(43,12): error TS2339: Property 'foo' does not exist on type 'number'. | ||
tests/cases/conformance/types/tuple/contextualTypeTupleEnd.ts(44,12): error TS2339: Property 'bar' does not exist on type 'number'. | ||
|
||
|
||
==== tests/cases/conformance/types/tuple/contextualTypeTupleEnd.ts (4 errors) ==== | ||
type Funcs = [...((arg: number) => void)[], (arg: string) => void]; | ||
|
||
declare function num(x: number): void; | ||
declare function str(x: string): void; | ||
|
||
declare function f1(...args: Funcs): void; | ||
|
||
f1(); // Error | ||
~~~~ | ||
!!! error TS2345: Argument of type '[]' is not assignable to parameter of type '[...((arg: number) => void)[], (arg: string) => void]'. | ||
!!! error TS2345: Source has 0 element(s) but target requires 1. | ||
f1(x => str(x)); | ||
f1(x => num(x), x => str(x)); | ||
f1(x => num(x), x => num(x), x => str(x)); | ||
|
||
const a0: Funcs = []; // Error | ||
~~ | ||
!!! error TS2322: Type '[]' is not assignable to type 'Funcs'. | ||
!!! error TS2322: Source has 0 element(s) but target requires 1. | ||
const a1: Funcs = [x => str(x)]; | ||
const a2: Funcs = [x => num(x), x => str(x)]; | ||
const a3: Funcs = [x => num(x), x => num(x), x => str(x)]; | ||
|
||
// Repro from #43122 | ||
|
||
export type Selector<State> = (state: State) => unknown; | ||
export type SelectorTuple<State> = Selector<State>[]; | ||
|
||
export type ExampleState = { | ||
foo: "foo"; | ||
bar: 42; | ||
}; | ||
|
||
export function createSelector<S extends SelectorTuple<ExampleState>>(...selectors: [...selectors: S, f: (x: any) => any]) { | ||
console.log(selectors); | ||
} | ||
|
||
createSelector( | ||
x => x.foo, | ||
x => x.bar, | ||
() => 42 | ||
); | ||
|
||
// Repro from #43122 | ||
|
||
declare function example(...args: [...((n: number) => void)[], (x: any) => void]): void | ||
|
||
example( | ||
x => x.foo, // Error | ||
~~~ | ||
!!! error TS2339: Property 'foo' does not exist on type 'number'. | ||
x => x.bar, // Error | ||
~~~ | ||
!!! error TS2339: Property 'bar' does not exist on type 'number'. | ||
x => x.baz, | ||
); | ||
|
||
// Repro from #52846 | ||
|
||
declare function test(...args: [...((arg: number) => void)[], (arg: string) => void]): void; | ||
|
||
test(a => a, b => b, c => c); | ||
|
184 changes: 184 additions & 0 deletions
184
tests/baselines/reference/contextualTypeTupleEnd.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,184 @@ | ||
=== tests/cases/conformance/types/tuple/contextualTypeTupleEnd.ts === | ||
type Funcs = [...((arg: number) => void)[], (arg: string) => void]; | ||
>Funcs : Symbol(Funcs, Decl(contextualTypeTupleEnd.ts, 0, 0)) | ||
>arg : Symbol(arg, Decl(contextualTypeTupleEnd.ts, 0, 19)) | ||
>arg : Symbol(arg, Decl(contextualTypeTupleEnd.ts, 0, 45)) | ||
|
||
declare function num(x: number): void; | ||
>num : Symbol(num, Decl(contextualTypeTupleEnd.ts, 0, 67)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 2, 21)) | ||
|
||
declare function str(x: string): void; | ||
>str : Symbol(str, Decl(contextualTypeTupleEnd.ts, 2, 38)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 3, 21)) | ||
|
||
declare function f1(...args: Funcs): void; | ||
>f1 : Symbol(f1, Decl(contextualTypeTupleEnd.ts, 3, 38)) | ||
>args : Symbol(args, Decl(contextualTypeTupleEnd.ts, 5, 20)) | ||
>Funcs : Symbol(Funcs, Decl(contextualTypeTupleEnd.ts, 0, 0)) | ||
|
||
f1(); // Error | ||
>f1 : Symbol(f1, Decl(contextualTypeTupleEnd.ts, 3, 38)) | ||
|
||
f1(x => str(x)); | ||
>f1 : Symbol(f1, Decl(contextualTypeTupleEnd.ts, 3, 38)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 8, 3)) | ||
>str : Symbol(str, Decl(contextualTypeTupleEnd.ts, 2, 38)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 8, 3)) | ||
|
||
f1(x => num(x), x => str(x)); | ||
>f1 : Symbol(f1, Decl(contextualTypeTupleEnd.ts, 3, 38)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 9, 3)) | ||
>num : Symbol(num, Decl(contextualTypeTupleEnd.ts, 0, 67)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 9, 3)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 9, 15)) | ||
>str : Symbol(str, Decl(contextualTypeTupleEnd.ts, 2, 38)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 9, 15)) | ||
|
||
f1(x => num(x), x => num(x), x => str(x)); | ||
>f1 : Symbol(f1, Decl(contextualTypeTupleEnd.ts, 3, 38)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 10, 3)) | ||
>num : Symbol(num, Decl(contextualTypeTupleEnd.ts, 0, 67)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 10, 3)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 10, 15)) | ||
>num : Symbol(num, Decl(contextualTypeTupleEnd.ts, 0, 67)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 10, 15)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 10, 28)) | ||
>str : Symbol(str, Decl(contextualTypeTupleEnd.ts, 2, 38)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 10, 28)) | ||
|
||
const a0: Funcs = []; // Error | ||
>a0 : Symbol(a0, Decl(contextualTypeTupleEnd.ts, 12, 5)) | ||
>Funcs : Symbol(Funcs, Decl(contextualTypeTupleEnd.ts, 0, 0)) | ||
|
||
const a1: Funcs = [x => str(x)]; | ||
>a1 : Symbol(a1, Decl(contextualTypeTupleEnd.ts, 13, 5)) | ||
>Funcs : Symbol(Funcs, Decl(contextualTypeTupleEnd.ts, 0, 0)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 13, 19)) | ||
>str : Symbol(str, Decl(contextualTypeTupleEnd.ts, 2, 38)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 13, 19)) | ||
|
||
const a2: Funcs = [x => num(x), x => str(x)]; | ||
>a2 : Symbol(a2, Decl(contextualTypeTupleEnd.ts, 14, 5)) | ||
>Funcs : Symbol(Funcs, Decl(contextualTypeTupleEnd.ts, 0, 0)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 14, 19)) | ||
>num : Symbol(num, Decl(contextualTypeTupleEnd.ts, 0, 67)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 14, 19)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 14, 31)) | ||
>str : Symbol(str, Decl(contextualTypeTupleEnd.ts, 2, 38)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 14, 31)) | ||
|
||
const a3: Funcs = [x => num(x), x => num(x), x => str(x)]; | ||
>a3 : Symbol(a3, Decl(contextualTypeTupleEnd.ts, 15, 5)) | ||
>Funcs : Symbol(Funcs, Decl(contextualTypeTupleEnd.ts, 0, 0)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 15, 19)) | ||
>num : Symbol(num, Decl(contextualTypeTupleEnd.ts, 0, 67)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 15, 19)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 15, 31)) | ||
>num : Symbol(num, Decl(contextualTypeTupleEnd.ts, 0, 67)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 15, 31)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 15, 44)) | ||
>str : Symbol(str, Decl(contextualTypeTupleEnd.ts, 2, 38)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 15, 44)) | ||
|
||
// Repro from #43122 | ||
|
||
export type Selector<State> = (state: State) => unknown; | ||
>Selector : Symbol(Selector, Decl(contextualTypeTupleEnd.ts, 15, 58)) | ||
>State : Symbol(State, Decl(contextualTypeTupleEnd.ts, 19, 21)) | ||
>state : Symbol(state, Decl(contextualTypeTupleEnd.ts, 19, 31)) | ||
>State : Symbol(State, Decl(contextualTypeTupleEnd.ts, 19, 21)) | ||
|
||
export type SelectorTuple<State> = Selector<State>[]; | ||
>SelectorTuple : Symbol(SelectorTuple, Decl(contextualTypeTupleEnd.ts, 19, 56)) | ||
>State : Symbol(State, Decl(contextualTypeTupleEnd.ts, 20, 26)) | ||
>Selector : Symbol(Selector, Decl(contextualTypeTupleEnd.ts, 15, 58)) | ||
>State : Symbol(State, Decl(contextualTypeTupleEnd.ts, 20, 26)) | ||
|
||
export type ExampleState = { | ||
>ExampleState : Symbol(ExampleState, Decl(contextualTypeTupleEnd.ts, 20, 53)) | ||
|
||
foo: "foo"; | ||
>foo : Symbol(foo, Decl(contextualTypeTupleEnd.ts, 22, 28)) | ||
|
||
bar: 42; | ||
>bar : Symbol(bar, Decl(contextualTypeTupleEnd.ts, 23, 15)) | ||
|
||
}; | ||
|
||
export function createSelector<S extends SelectorTuple<ExampleState>>(...selectors: [...selectors: S, f: (x: any) => any]) { | ||
>createSelector : Symbol(createSelector, Decl(contextualTypeTupleEnd.ts, 25, 2)) | ||
>S : Symbol(S, Decl(contextualTypeTupleEnd.ts, 27, 31)) | ||
>SelectorTuple : Symbol(SelectorTuple, Decl(contextualTypeTupleEnd.ts, 19, 56)) | ||
>ExampleState : Symbol(ExampleState, Decl(contextualTypeTupleEnd.ts, 20, 53)) | ||
>selectors : Symbol(selectors, Decl(contextualTypeTupleEnd.ts, 27, 70)) | ||
>S : Symbol(S, Decl(contextualTypeTupleEnd.ts, 27, 31)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 27, 106)) | ||
|
||
console.log(selectors); | ||
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) | ||
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>selectors : Symbol(selectors, Decl(contextualTypeTupleEnd.ts, 27, 70)) | ||
} | ||
|
||
createSelector( | ||
>createSelector : Symbol(createSelector, Decl(contextualTypeTupleEnd.ts, 25, 2)) | ||
|
||
x => x.foo, | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 31, 15)) | ||
>x.foo : Symbol(foo, Decl(contextualTypeTupleEnd.ts, 22, 28)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 31, 15)) | ||
>foo : Symbol(foo, Decl(contextualTypeTupleEnd.ts, 22, 28)) | ||
|
||
x => x.bar, | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 32, 15)) | ||
>x.bar : Symbol(bar, Decl(contextualTypeTupleEnd.ts, 23, 15)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 32, 15)) | ||
>bar : Symbol(bar, Decl(contextualTypeTupleEnd.ts, 23, 15)) | ||
|
||
() => 42 | ||
); | ||
|
||
// Repro from #43122 | ||
|
||
declare function example(...args: [...((n: number) => void)[], (x: any) => void]): void | ||
>example : Symbol(example, Decl(contextualTypeTupleEnd.ts, 35, 2)) | ||
>args : Symbol(args, Decl(contextualTypeTupleEnd.ts, 39, 25)) | ||
>n : Symbol(n, Decl(contextualTypeTupleEnd.ts, 39, 40)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 39, 64)) | ||
|
||
example( | ||
>example : Symbol(example, Decl(contextualTypeTupleEnd.ts, 35, 2)) | ||
|
||
x => x.foo, // Error | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 41, 8)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 41, 8)) | ||
|
||
x => x.bar, // Error | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 42, 15)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 42, 15)) | ||
|
||
x => x.baz, | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 43, 15)) | ||
>x : Symbol(x, Decl(contextualTypeTupleEnd.ts, 43, 15)) | ||
|
||
); | ||
|
||
// Repro from #52846 | ||
|
||
declare function test(...args: [...((arg: number) => void)[], (arg: string) => void]): void; | ||
>test : Symbol(test, Decl(contextualTypeTupleEnd.ts, 45, 2)) | ||
>args : Symbol(args, Decl(contextualTypeTupleEnd.ts, 49, 22)) | ||
>arg : Symbol(arg, Decl(contextualTypeTupleEnd.ts, 49, 37)) | ||
>arg : Symbol(arg, Decl(contextualTypeTupleEnd.ts, 49, 63)) | ||
|
||
test(a => a, b => b, c => c); | ||
>test : Symbol(test, Decl(contextualTypeTupleEnd.ts, 45, 2)) | ||
>a : Symbol(a, Decl(contextualTypeTupleEnd.ts, 51, 5)) | ||
>a : Symbol(a, Decl(contextualTypeTupleEnd.ts, 51, 5)) | ||
>b : Symbol(b, Decl(contextualTypeTupleEnd.ts, 51, 12)) | ||
>b : Symbol(b, Decl(contextualTypeTupleEnd.ts, 51, 12)) | ||
>c : Symbol(c, Decl(contextualTypeTupleEnd.ts, 51, 20)) | ||
>c : Symbol(c, Decl(contextualTypeTupleEnd.ts, 51, 20)) | ||
|
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.
Maybe it should be treated as a separate issue... but I feel that perhaps this feature here might directly complicate things for that issue so it's at least worth mentioning now.
Take a look at this TS playground. The problem there is that
getTypeOfPropertyOfContextualType
gets thegetElementTypeOfSliceOfTupleType
, so we end up with a union of all element types instead of types of individual tuple members.I prepared a quick fix for this issue in this PR: #53042 but it doesn't handle the case that is being fixed here (ending tuple elements).
The case outlined there is currently a little bit silly (
T[K & keyof T]
within[K in keyof T2]: { ... }
) but I partially encountered this need when working on inference for intersected mapped types (PR here). This case would be better if rewritten like this:There are 2 problems with this code today though:
A rest element type must be an array type.ts(2574)
. That probably could be improved as well.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.
@ahejlsberg Any concerns given the above?
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.
No, that's a separate issue.
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.
Yes, I just meant specifically the "this feature here might directly complicate things for that issue so it's at least worth mentioning now" bit.