-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Fix contextual typing for post-spread tuple elements #52769
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3f00485
Fix contextual typing for post-spread array literal elements
ahejlsberg 0fab031
No union reduction for contextual types originating in tuples
ahejlsberg b043afc
Add tests
ahejlsberg 54f49e9
Merge branch 'main' into fix52684
ahejlsberg 9588571
No union reduction of contextual tuple rest type
ahejlsberg 6e84767
Accept new baselines
ahejlsberg 2631e2f
Add more tests
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 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 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
139 changes: 139 additions & 0 deletions
139
tests/baselines/reference/spreadsAndContextualTupleTypes.symbols
This file contains 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,139 @@ | ||
=== tests/cases/compiler/spreadsAndContextualTupleTypes.ts === | ||
declare function fx1<T extends [string, string, string, 'a' | 'b']>(x: T): T; | ||
>fx1 : Symbol(fx1, Decl(spreadsAndContextualTupleTypes.ts, 0, 0)) | ||
>T : Symbol(T, Decl(spreadsAndContextualTupleTypes.ts, 0, 21)) | ||
>x : Symbol(x, Decl(spreadsAndContextualTupleTypes.ts, 0, 68)) | ||
>T : Symbol(T, Decl(spreadsAndContextualTupleTypes.ts, 0, 21)) | ||
>T : Symbol(T, Decl(spreadsAndContextualTupleTypes.ts, 0, 21)) | ||
|
||
declare function fx2<T extends [...string[], 'a' | 'b']>(x: T): T; | ||
>fx2 : Symbol(fx2, Decl(spreadsAndContextualTupleTypes.ts, 0, 77)) | ||
>T : Symbol(T, Decl(spreadsAndContextualTupleTypes.ts, 1, 21)) | ||
>x : Symbol(x, Decl(spreadsAndContextualTupleTypes.ts, 1, 57)) | ||
>T : Symbol(T, Decl(spreadsAndContextualTupleTypes.ts, 1, 21)) | ||
>T : Symbol(T, Decl(spreadsAndContextualTupleTypes.ts, 1, 21)) | ||
|
||
const t3 = ['x', 'y', 'z'] as const; | ||
>t3 : Symbol(t3, Decl(spreadsAndContextualTupleTypes.ts, 3, 5)) | ||
>const : Symbol(const) | ||
|
||
fx1(['x', 'y', 'z', 'a']); | ||
>fx1 : Symbol(fx1, Decl(spreadsAndContextualTupleTypes.ts, 0, 0)) | ||
|
||
fx1([...t3, 'a']); | ||
>fx1 : Symbol(fx1, Decl(spreadsAndContextualTupleTypes.ts, 0, 0)) | ||
>t3 : Symbol(t3, Decl(spreadsAndContextualTupleTypes.ts, 3, 5)) | ||
|
||
fx2(['x', 'y', 'z', 'a']); | ||
>fx2 : Symbol(fx2, Decl(spreadsAndContextualTupleTypes.ts, 0, 77)) | ||
|
||
fx2([...t3, 'a']); | ||
>fx2 : Symbol(fx2, Decl(spreadsAndContextualTupleTypes.ts, 0, 77)) | ||
>t3 : Symbol(t3, Decl(spreadsAndContextualTupleTypes.ts, 3, 5)) | ||
|
||
const x1: [...string[], '!'] = ['!']; | ||
>x1 : Symbol(x1, Decl(spreadsAndContextualTupleTypes.ts, 11, 5)) | ||
|
||
const x2: [...string[], '!'] = ['a', '!']; | ||
>x2 : Symbol(x2, Decl(spreadsAndContextualTupleTypes.ts, 12, 5)) | ||
|
||
const x3: [...string[], '!'] = [...t3, '!']; | ||
>x3 : Symbol(x3, Decl(spreadsAndContextualTupleTypes.ts, 13, 5)) | ||
>t3 : Symbol(t3, Decl(spreadsAndContextualTupleTypes.ts, 3, 5)) | ||
|
||
// Repro from #52684 | ||
|
||
const staticPath1Level = ["home"] as const; | ||
>staticPath1Level : Symbol(staticPath1Level, Decl(spreadsAndContextualTupleTypes.ts, 17, 5)) | ||
>const : Symbol(const) | ||
|
||
const staticPath2Level = ["home", "user"] as const; | ||
>staticPath2Level : Symbol(staticPath2Level, Decl(spreadsAndContextualTupleTypes.ts, 18, 5)) | ||
>const : Symbol(const) | ||
|
||
const staticPath3Level = ["home", "user", "downloads"] as const; | ||
>staticPath3Level : Symbol(staticPath3Level, Decl(spreadsAndContextualTupleTypes.ts, 19, 5)) | ||
>const : Symbol(const) | ||
|
||
const randomID = 'id' as string; | ||
>randomID : Symbol(randomID, Decl(spreadsAndContextualTupleTypes.ts, 21, 5)) | ||
|
||
declare function foo<const T>(path: T): T; | ||
>foo : Symbol(foo, Decl(spreadsAndContextualTupleTypes.ts, 21, 32)) | ||
>T : Symbol(T, Decl(spreadsAndContextualTupleTypes.ts, 23, 21)) | ||
>path : Symbol(path, Decl(spreadsAndContextualTupleTypes.ts, 23, 30)) | ||
>T : Symbol(T, Decl(spreadsAndContextualTupleTypes.ts, 23, 21)) | ||
>T : Symbol(T, Decl(spreadsAndContextualTupleTypes.ts, 23, 21)) | ||
|
||
const a1 = foo([...staticPath1Level, randomID, 'doc.pdf']); | ||
>a1 : Symbol(a1, Decl(spreadsAndContextualTupleTypes.ts, 25, 5)) | ||
>foo : Symbol(foo, Decl(spreadsAndContextualTupleTypes.ts, 21, 32)) | ||
>staticPath1Level : Symbol(staticPath1Level, Decl(spreadsAndContextualTupleTypes.ts, 17, 5)) | ||
>randomID : Symbol(randomID, Decl(spreadsAndContextualTupleTypes.ts, 21, 5)) | ||
|
||
const a2 = foo([...staticPath2Level, randomID, 'doc.pdf']); | ||
>a2 : Symbol(a2, Decl(spreadsAndContextualTupleTypes.ts, 26, 5)) | ||
>foo : Symbol(foo, Decl(spreadsAndContextualTupleTypes.ts, 21, 32)) | ||
>staticPath2Level : Symbol(staticPath2Level, Decl(spreadsAndContextualTupleTypes.ts, 18, 5)) | ||
>randomID : Symbol(randomID, Decl(spreadsAndContextualTupleTypes.ts, 21, 5)) | ||
|
||
const a3 = foo([...staticPath3Level, randomID, 'doc.pdf']); | ||
>a3 : Symbol(a3, Decl(spreadsAndContextualTupleTypes.ts, 27, 5)) | ||
>foo : Symbol(foo, Decl(spreadsAndContextualTupleTypes.ts, 21, 32)) | ||
>staticPath3Level : Symbol(staticPath3Level, Decl(spreadsAndContextualTupleTypes.ts, 19, 5)) | ||
>randomID : Symbol(randomID, Decl(spreadsAndContextualTupleTypes.ts, 21, 5)) | ||
|
||
const b1 = foo([...staticPath1Level, randomID, 'folder', 'doc.pdf']); | ||
>b1 : Symbol(b1, Decl(spreadsAndContextualTupleTypes.ts, 29, 5)) | ||
>foo : Symbol(foo, Decl(spreadsAndContextualTupleTypes.ts, 21, 32)) | ||
>staticPath1Level : Symbol(staticPath1Level, Decl(spreadsAndContextualTupleTypes.ts, 17, 5)) | ||
>randomID : Symbol(randomID, Decl(spreadsAndContextualTupleTypes.ts, 21, 5)) | ||
|
||
const b2 = foo([...staticPath2Level, randomID, 'folder', 'doc.pdf']); | ||
>b2 : Symbol(b2, Decl(spreadsAndContextualTupleTypes.ts, 30, 5)) | ||
>foo : Symbol(foo, Decl(spreadsAndContextualTupleTypes.ts, 21, 32)) | ||
>staticPath2Level : Symbol(staticPath2Level, Decl(spreadsAndContextualTupleTypes.ts, 18, 5)) | ||
>randomID : Symbol(randomID, Decl(spreadsAndContextualTupleTypes.ts, 21, 5)) | ||
|
||
const b3 = foo([...staticPath3Level, randomID, 'folder', 'doc.pdf']); | ||
>b3 : Symbol(b3, Decl(spreadsAndContextualTupleTypes.ts, 31, 5)) | ||
>foo : Symbol(foo, Decl(spreadsAndContextualTupleTypes.ts, 21, 32)) | ||
>staticPath3Level : Symbol(staticPath3Level, Decl(spreadsAndContextualTupleTypes.ts, 19, 5)) | ||
>randomID : Symbol(randomID, Decl(spreadsAndContextualTupleTypes.ts, 21, 5)) | ||
|
||
const c1 = foo([...staticPath1Level, randomID, 'folder', 'subfolder', 'doc.pdf']); | ||
>c1 : Symbol(c1, Decl(spreadsAndContextualTupleTypes.ts, 33, 5)) | ||
>foo : Symbol(foo, Decl(spreadsAndContextualTupleTypes.ts, 21, 32)) | ||
>staticPath1Level : Symbol(staticPath1Level, Decl(spreadsAndContextualTupleTypes.ts, 17, 5)) | ||
>randomID : Symbol(randomID, Decl(spreadsAndContextualTupleTypes.ts, 21, 5)) | ||
|
||
const c2 = foo([...staticPath2Level, randomID, 'folder', 'subfolder', 'doc.pdf']); | ||
>c2 : Symbol(c2, Decl(spreadsAndContextualTupleTypes.ts, 34, 5)) | ||
>foo : Symbol(foo, Decl(spreadsAndContextualTupleTypes.ts, 21, 32)) | ||
>staticPath2Level : Symbol(staticPath2Level, Decl(spreadsAndContextualTupleTypes.ts, 18, 5)) | ||
>randomID : Symbol(randomID, Decl(spreadsAndContextualTupleTypes.ts, 21, 5)) | ||
|
||
const c3 = foo([...staticPath3Level, randomID, 'folder', 'subfolder', 'doc.pdf']); | ||
>c3 : Symbol(c3, Decl(spreadsAndContextualTupleTypes.ts, 35, 5)) | ||
>foo : Symbol(foo, Decl(spreadsAndContextualTupleTypes.ts, 21, 32)) | ||
>staticPath3Level : Symbol(staticPath3Level, Decl(spreadsAndContextualTupleTypes.ts, 19, 5)) | ||
>randomID : Symbol(randomID, Decl(spreadsAndContextualTupleTypes.ts, 21, 5)) | ||
|
||
const d1 = foo([...staticPath1Level, randomID, 'folder', 'subfolder', 'another-subfolder', 'doc.pdf']); | ||
>d1 : Symbol(d1, Decl(spreadsAndContextualTupleTypes.ts, 37, 5)) | ||
>foo : Symbol(foo, Decl(spreadsAndContextualTupleTypes.ts, 21, 32)) | ||
>staticPath1Level : Symbol(staticPath1Level, Decl(spreadsAndContextualTupleTypes.ts, 17, 5)) | ||
>randomID : Symbol(randomID, Decl(spreadsAndContextualTupleTypes.ts, 21, 5)) | ||
|
||
const d2 = foo([...staticPath2Level, randomID, 'folder', 'subfolder', 'another-subfolder', 'doc.pdf']); | ||
>d2 : Symbol(d2, Decl(spreadsAndContextualTupleTypes.ts, 38, 5)) | ||
>foo : Symbol(foo, Decl(spreadsAndContextualTupleTypes.ts, 21, 32)) | ||
>staticPath2Level : Symbol(staticPath2Level, Decl(spreadsAndContextualTupleTypes.ts, 18, 5)) | ||
>randomID : Symbol(randomID, Decl(spreadsAndContextualTupleTypes.ts, 21, 5)) | ||
|
||
const d3 = foo([...staticPath3Level, randomID, 'folder', 'subfolder', 'another-subfolder', 'doc.pdf']); | ||
>d3 : Symbol(d3, Decl(spreadsAndContextualTupleTypes.ts, 39, 5)) | ||
>foo : Symbol(foo, Decl(spreadsAndContextualTupleTypes.ts, 21, 32)) | ||
>staticPath3Level : Symbol(staticPath3Level, Decl(spreadsAndContextualTupleTypes.ts, 19, 5)) | ||
>randomID : Symbol(randomID, Decl(spreadsAndContextualTupleTypes.ts, 21, 5)) | ||
|
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 know that it's not exactly 100% related to this case - but I was touching this function just recently here. It would be cool if that improvement (if correct) could also go into 5.0 - both would make for a nice "batch" of improvements for contextual types for element expressions. 😉