-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Apply a contextual type to empty arrays #51882
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 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
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
44 changes: 44 additions & 0 deletions
44
tests/baselines/reference/arrayContextualTyping.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,44 @@ | ||
tests/cases/compiler/arrayContextualTyping.ts(9,9): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'. | ||
tests/cases/compiler/arrayContextualTyping.ts(14,11): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'. | ||
|
||
|
||
==== tests/cases/compiler/arrayContextualTyping.ts (2 errors) ==== | ||
// Setup | ||
declare class MyCustomArray extends Array<number> { | ||
isCustom: true; | ||
} | ||
|
||
// MVP: An empty array contextually typed by an array type gets that array's type | ||
const m1 = [] satisfies number[]; | ||
m1.push(0); // Should be OK | ||
m1.push(""); // Should error | ||
~~ | ||
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'. | ||
|
||
// Works in object fields? | ||
const m2 = { a: [] satisfies string[] }; | ||
m2.a.push(""); // Should be OK | ||
m2.a.push(0); // Should error | ||
~ | ||
!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'. | ||
|
||
// If the contextual type is a union, keep the array-supertyping parts of the union | ||
const m3 = [] satisfies number[] | ArrayLike<string>; | ||
const m3t: number[] | string[] = m3; | ||
|
||
// Keep only the array parts | ||
const m4 = [] satisfies MyCustomArray | string[]; | ||
const m4t: string[] = m4; | ||
|
||
// Should OK | ||
const m5: string[] | number[] = [] satisfies string[] | number[]; | ||
// Should OK | ||
type Obj = { a: string[] } | { a: number[] }; | ||
const m6: Obj = { a: [] } satisfies Obj; | ||
|
||
// Should all OK | ||
type DiscrObj = { a: string[], kind: "strings" } | { a: number[], kind: "numbers" }; | ||
const m7: DiscrObj = { a: [], kind: "numbers"}; | ||
const m8: DiscrObj = { a: [], kind: "numbers"} satisfies DiscrObj; | ||
const m9: DiscrObj = { a: [], kind: "strings"} satisfies DiscrObj; | ||
|
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,59 @@ | ||
//// [arrayContextualTyping.ts] | ||
// Setup | ||
declare class MyCustomArray extends Array<number> { | ||
isCustom: true; | ||
} | ||
|
||
// MVP: An empty array contextually typed by an array type gets that array's type | ||
const m1 = [] satisfies number[]; | ||
m1.push(0); // Should be OK | ||
m1.push(""); // Should error | ||
|
||
// Works in object fields? | ||
const m2 = { a: [] satisfies string[] }; | ||
m2.a.push(""); // Should be OK | ||
m2.a.push(0); // Should error | ||
|
||
// If the contextual type is a union, keep the array-supertyping parts of the union | ||
const m3 = [] satisfies number[] | ArrayLike<string>; | ||
const m3t: number[] | string[] = m3; | ||
|
||
// Keep only the array parts | ||
const m4 = [] satisfies MyCustomArray | string[]; | ||
const m4t: string[] = m4; | ||
|
||
// Should OK | ||
const m5: string[] | number[] = [] satisfies string[] | number[]; | ||
// Should OK | ||
type Obj = { a: string[] } | { a: number[] }; | ||
const m6: Obj = { a: [] } satisfies Obj; | ||
|
||
// Should all OK | ||
type DiscrObj = { a: string[], kind: "strings" } | { a: number[], kind: "numbers" }; | ||
const m7: DiscrObj = { a: [], kind: "numbers"}; | ||
const m8: DiscrObj = { a: [], kind: "numbers"} satisfies DiscrObj; | ||
const m9: DiscrObj = { a: [], kind: "strings"} satisfies DiscrObj; | ||
|
||
|
||
//// [arrayContextualTyping.js] | ||
"use strict"; | ||
// MVP: An empty array contextually typed by an array type gets that array's type | ||
var m1 = []; | ||
m1.push(0); // Should be OK | ||
m1.push(""); // Should error | ||
// Works in object fields? | ||
var m2 = { a: [] }; | ||
m2.a.push(""); // Should be OK | ||
m2.a.push(0); // Should error | ||
// If the contextual type is a union, keep the array-supertyping parts of the union | ||
var m3 = []; | ||
var m3t = m3; | ||
// Keep only the array parts | ||
var m4 = []; | ||
var m4t = m4; | ||
// Should OK | ||
var m5 = []; | ||
var m6 = { a: [] }; | ||
var m7 = { a: [], kind: "numbers" }; | ||
var m8 = { a: [], kind: "numbers" }; | ||
var m9 = { a: [], kind: "strings" }; |
105 changes: 105 additions & 0 deletions
105
tests/baselines/reference/arrayContextualTyping.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,105 @@ | ||
=== tests/cases/compiler/arrayContextualTyping.ts === | ||
// Setup | ||
declare class MyCustomArray extends Array<number> { | ||
>MyCustomArray : Symbol(MyCustomArray, Decl(arrayContextualTyping.ts, 0, 0)) | ||
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
|
||
isCustom: true; | ||
>isCustom : Symbol(MyCustomArray.isCustom, Decl(arrayContextualTyping.ts, 1, 51)) | ||
} | ||
|
||
// MVP: An empty array contextually typed by an array type gets that array's type | ||
const m1 = [] satisfies number[]; | ||
>m1 : Symbol(m1, Decl(arrayContextualTyping.ts, 6, 5)) | ||
|
||
m1.push(0); // Should be OK | ||
>m1.push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --)) | ||
>m1 : Symbol(m1, Decl(arrayContextualTyping.ts, 6, 5)) | ||
>push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --)) | ||
|
||
m1.push(""); // Should error | ||
>m1.push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --)) | ||
>m1 : Symbol(m1, Decl(arrayContextualTyping.ts, 6, 5)) | ||
>push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --)) | ||
|
||
// Works in object fields? | ||
const m2 = { a: [] satisfies string[] }; | ||
>m2 : Symbol(m2, Decl(arrayContextualTyping.ts, 11, 5)) | ||
>a : Symbol(a, Decl(arrayContextualTyping.ts, 11, 12)) | ||
|
||
m2.a.push(""); // Should be OK | ||
>m2.a.push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --)) | ||
>m2.a : Symbol(a, Decl(arrayContextualTyping.ts, 11, 12)) | ||
>m2 : Symbol(m2, Decl(arrayContextualTyping.ts, 11, 5)) | ||
>a : Symbol(a, Decl(arrayContextualTyping.ts, 11, 12)) | ||
>push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --)) | ||
|
||
m2.a.push(0); // Should error | ||
>m2.a.push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --)) | ||
>m2.a : Symbol(a, Decl(arrayContextualTyping.ts, 11, 12)) | ||
>m2 : Symbol(m2, Decl(arrayContextualTyping.ts, 11, 5)) | ||
>a : Symbol(a, Decl(arrayContextualTyping.ts, 11, 12)) | ||
>push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --)) | ||
|
||
// If the contextual type is a union, keep the array-supertyping parts of the union | ||
const m3 = [] satisfies number[] | ArrayLike<string>; | ||
>m3 : Symbol(m3, Decl(arrayContextualTyping.ts, 16, 5)) | ||
>ArrayLike : Symbol(ArrayLike, Decl(lib.es5.d.ts, --, --)) | ||
|
||
const m3t: number[] | string[] = m3; | ||
>m3t : Symbol(m3t, Decl(arrayContextualTyping.ts, 17, 5)) | ||
>m3 : Symbol(m3, Decl(arrayContextualTyping.ts, 16, 5)) | ||
|
||
// Keep only the array parts | ||
const m4 = [] satisfies MyCustomArray | string[]; | ||
>m4 : Symbol(m4, Decl(arrayContextualTyping.ts, 20, 5)) | ||
>MyCustomArray : Symbol(MyCustomArray, Decl(arrayContextualTyping.ts, 0, 0)) | ||
|
||
const m4t: string[] = m4; | ||
>m4t : Symbol(m4t, Decl(arrayContextualTyping.ts, 21, 5)) | ||
>m4 : Symbol(m4, Decl(arrayContextualTyping.ts, 20, 5)) | ||
|
||
// Should OK | ||
const m5: string[] | number[] = [] satisfies string[] | number[]; | ||
>m5 : Symbol(m5, Decl(arrayContextualTyping.ts, 24, 5)) | ||
|
||
// Should OK | ||
type Obj = { a: string[] } | { a: number[] }; | ||
>Obj : Symbol(Obj, Decl(arrayContextualTyping.ts, 24, 65)) | ||
>a : Symbol(a, Decl(arrayContextualTyping.ts, 26, 12)) | ||
>a : Symbol(a, Decl(arrayContextualTyping.ts, 26, 30)) | ||
|
||
const m6: Obj = { a: [] } satisfies Obj; | ||
>m6 : Symbol(m6, Decl(arrayContextualTyping.ts, 27, 5)) | ||
>Obj : Symbol(Obj, Decl(arrayContextualTyping.ts, 24, 65)) | ||
>a : Symbol(a, Decl(arrayContextualTyping.ts, 27, 17)) | ||
>Obj : Symbol(Obj, Decl(arrayContextualTyping.ts, 24, 65)) | ||
|
||
// Should all OK | ||
type DiscrObj = { a: string[], kind: "strings" } | { a: number[], kind: "numbers" }; | ||
>DiscrObj : Symbol(DiscrObj, Decl(arrayContextualTyping.ts, 27, 40)) | ||
>a : Symbol(a, Decl(arrayContextualTyping.ts, 30, 17)) | ||
>kind : Symbol(kind, Decl(arrayContextualTyping.ts, 30, 30)) | ||
>a : Symbol(a, Decl(arrayContextualTyping.ts, 30, 52)) | ||
>kind : Symbol(kind, Decl(arrayContextualTyping.ts, 30, 65)) | ||
|
||
const m7: DiscrObj = { a: [], kind: "numbers"}; | ||
>m7 : Symbol(m7, Decl(arrayContextualTyping.ts, 31, 5)) | ||
>DiscrObj : Symbol(DiscrObj, Decl(arrayContextualTyping.ts, 27, 40)) | ||
>a : Symbol(a, Decl(arrayContextualTyping.ts, 31, 22)) | ||
>kind : Symbol(kind, Decl(arrayContextualTyping.ts, 31, 29)) | ||
|
||
const m8: DiscrObj = { a: [], kind: "numbers"} satisfies DiscrObj; | ||
>m8 : Symbol(m8, Decl(arrayContextualTyping.ts, 32, 5)) | ||
>DiscrObj : Symbol(DiscrObj, Decl(arrayContextualTyping.ts, 27, 40)) | ||
>a : Symbol(a, Decl(arrayContextualTyping.ts, 32, 22)) | ||
>kind : Symbol(kind, Decl(arrayContextualTyping.ts, 32, 29)) | ||
>DiscrObj : Symbol(DiscrObj, Decl(arrayContextualTyping.ts, 27, 40)) | ||
|
||
const m9: DiscrObj = { a: [], kind: "strings"} satisfies DiscrObj; | ||
>m9 : Symbol(m9, Decl(arrayContextualTyping.ts, 33, 5)) | ||
>DiscrObj : Symbol(DiscrObj, Decl(arrayContextualTyping.ts, 27, 40)) | ||
>a : Symbol(a, Decl(arrayContextualTyping.ts, 33, 22)) | ||
>kind : Symbol(kind, Decl(arrayContextualTyping.ts, 33, 29)) | ||
>DiscrObj : Symbol(DiscrObj, Decl(arrayContextualTyping.ts, 27, 40)) | ||
|
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.
@RyanCavanaugh Could you please clarify what is the purpose of this
isCustom
property? Looking at this, it seems to me like it is a condition to run a different set of tests, if so it does not seem like a good practice to have conditional tests.