-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Add ReadonlyArray overload to Array.isArray #28916
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
7 commits
Select commit
Hold shift + click to select a range
ef70c04
Add ReadonlyArray overload for Array.isArray()
ulrichb f02bdcb
Accept remaining baselines
ulrichb 659e901
Merge remote-tracking branch 'origin/master' into IsArrayReadonlyArra…
ulrichb 57fa898
Switch to alternative which uses conditional types instead of an over…
ulrichb 93e902d
Use parameters instead of casting of test types
ulrichb 5e54de0
Add additional negative tests
ulrichb 88cd932
Fix leftover test type cast
ulrichb 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
tests/cases/compiler/isArray.ts(21,33): error TS2339: Property 'push' does not exist on type 'readonly string[]'. | ||
tests/cases/compiler/isArray.ts(22,33): error TS2339: Property 'push' does not exist on type 'readonly string[]'. | ||
tests/cases/compiler/isArray.ts(28,38): error TS2345: Argument of type '10' is not assignable to parameter of type 'string'. | ||
tests/cases/compiler/isArray.ts(36,33): error TS2339: Property 'push' does not exist on type 'readonly string[] | number[]'. | ||
Property 'push' does not exist on type 'readonly string[]'. | ||
tests/cases/compiler/isArray.ts(40,33): error TS2339: Property 'push' does not exist on type 'readonly string[] | number[]'. | ||
Property 'push' does not exist on type 'readonly string[]'. | ||
tests/cases/compiler/isArray.ts(44,38): error TS2345: Argument of type '10' is not assignable to parameter of type 'string'. | ||
tests/cases/compiler/isArray.ts(50,33): error TS2339: Property 'push' does not exist on type 'MyReadOnlyArray<string>'. | ||
tests/cases/compiler/isArray.ts(51,41): error TS2345: Argument of type '10' is not assignable to parameter of type 'string'. | ||
tests/cases/compiler/isArray.ts(61,33): error TS2339: Property 'push' does not exist on type 'readonly T[]'. | ||
tests/cases/compiler/isArray.ts(70,33): error TS2339: Property 'push' does not exist on type 'readonly [T]'. | ||
tests/cases/compiler/isArray.ts(76,38): error TS2345: Argument of type '10' is not assignable to parameter of type 'string'. | ||
tests/cases/compiler/isArray.ts(83,15): error TS2345: Argument of type '"str"' is not assignable to parameter of type 'string[]'. | ||
|
||
|
||
==== tests/cases/compiler/isArray.ts (12 errors) ==== | ||
interface MyArray<T> extends Array<T> { manifest: any; } | ||
interface MyReadOnlyArray<T> extends ReadonlyArray<T> { manifest: any; } | ||
|
||
function fn1(arg: string | string[]) { | ||
if (Array.isArray(arg)) arg.push(""); // Should OK | ||
} | ||
|
||
function fn2(arg: unknown) { | ||
if (Array.isArray(arg)) arg.push(""); // Should OK | ||
} | ||
|
||
function fn3(arg: object) { | ||
if (Array.isArray(arg)) arg.push(""); // Should OK | ||
} | ||
|
||
function fn4(arg: {}) { | ||
if (Array.isArray(arg)) arg.push(""); // Should OK | ||
} | ||
|
||
function fn5(arg: string | ReadonlyArray<string>) { | ||
if (Array.isArray(arg)) arg.push(10); // Should FAIL | ||
~~~~ | ||
!!! error TS2339: Property 'push' does not exist on type 'readonly string[]'. | ||
if (Array.isArray(arg)) arg.push(""); // Should FAIL | ||
~~~~ | ||
!!! error TS2339: Property 'push' does not exist on type 'readonly string[]'. | ||
if (Array.isArray(arg)) arg.indexOf(""); // Should OK | ||
if (!Array.isArray(arg)) arg.toUpperCase(); // Should OK | ||
} | ||
|
||
function fn6(arg: string | string[]) { | ||
if (Array.isArray(arg)) arg.push(10); // Should FAIL | ||
~~ | ||
!!! error TS2345: Argument of type '10' is not assignable to parameter of type 'string'. | ||
} | ||
|
||
function fn7(arg: boolean | number[] | string[], stringAndNumber: string & number) { | ||
if (Array.isArray(arg)) arg.push(stringAndNumber); // Should OK | ||
} | ||
|
||
function fn8(arg: string | number[] | readonly string[]) { | ||
if (Array.isArray(arg)) arg.push(10); // Should FAIL | ||
~~~~ | ||
!!! error TS2339: Property 'push' does not exist on type 'readonly string[] | number[]'. | ||
!!! error TS2339: Property 'push' does not exist on type 'readonly string[]'. | ||
} | ||
|
||
function fn9(arg: string | number[] | readonly string[]) { | ||
if (Array.isArray(arg)) arg.push(10); // Should FAIL | ||
~~~~ | ||
!!! error TS2339: Property 'push' does not exist on type 'readonly string[] | number[]'. | ||
!!! error TS2339: Property 'push' does not exist on type 'readonly string[]'. | ||
} | ||
|
||
function fn10(arg: string | MyArray<string>) { | ||
if (Array.isArray(arg)) arg.push(10); // Should FAIL | ||
~~ | ||
!!! error TS2345: Argument of type '10' is not assignable to parameter of type 'string'. | ||
if (Array.isArray(arg)) arg.push(""); // Should OK | ||
if (Array.isArray(arg)) arg.manifest; // Should OK | ||
} | ||
|
||
function fn11(arg: string | MyReadOnlyArray<string>) { | ||
if (Array.isArray(arg)) arg.push(""); // Should FAIL | ||
~~~~ | ||
!!! error TS2339: Property 'push' does not exist on type 'MyReadOnlyArray<string>'. | ||
if (Array.isArray(arg)) arg.indexOf(10); // Should FAIL | ||
~~ | ||
!!! error TS2345: Argument of type '10' is not assignable to parameter of type 'string'. | ||
if (Array.isArray(arg)) arg.indexOf(""); // Should OK | ||
if (Array.isArray(arg)) arg.manifest; // Should OK | ||
} | ||
|
||
function fn12<T>(arg: T | T[], t: T) { | ||
if (Array.isArray(arg)) arg.push(t); // Should OK | ||
} | ||
|
||
function fn13<T>(arg: T | ReadonlyArray<T>, t: T) { | ||
if (Array.isArray(arg)) arg.push(t); // Should fail | ||
~~~~ | ||
!!! error TS2339: Property 'push' does not exist on type 'readonly T[]'. | ||
if (Array.isArray(arg)) arg.indexOf(t); // OK | ||
} | ||
|
||
function fn14<T>(arg: T | [T], t: T) { | ||
if (Array.isArray(arg)) arg.push(t); // Should OK | ||
} | ||
|
||
function fn15<T>(arg: T | readonly [T], t: T) { | ||
if (Array.isArray(arg)) arg.push(t); // Should fail | ||
~~~~ | ||
!!! error TS2339: Property 'push' does not exist on type 'readonly [T]'. | ||
if (Array.isArray(arg)) arg.indexOf(t); // Should OK | ||
} | ||
|
||
function fn16<T extends string | string[]>(arg: T) { | ||
if (Array.isArray(arg)) arg.push("10"); // Should OK | ||
if (Array.isArray(arg)) arg.push(10); // Should fail | ||
~~ | ||
!!! error TS2345: Argument of type '10' is not assignable to parameter of type 'string'. | ||
} | ||
|
||
function fn17() { | ||
const s: Array<string | string[]> = []; | ||
const arrs = s.filter(Array.isArray); | ||
arrs.push(["one"]); // Should OK | ||
arrs.push("str"); // Should fail | ||
~~~~~ | ||
!!! error TS2345: Argument of type '"str"' is not assignable to parameter of type 'string[]'. | ||
} | ||
|
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 |
---|---|---|
@@ -1,19 +1,180 @@ | ||
//// [isArray.ts] | ||
var maybeArray: number | number[]; | ||
interface MyArray<T> extends Array<T> { manifest: any; } | ||
interface MyReadOnlyArray<T> extends ReadonlyArray<T> { manifest: any; } | ||
|
||
function fn1(arg: string | string[]) { | ||
if (Array.isArray(arg)) arg.push(""); // Should OK | ||
} | ||
|
||
if (Array.isArray(maybeArray)) { | ||
maybeArray.length; // OK | ||
function fn2(arg: unknown) { | ||
if (Array.isArray(arg)) arg.push(""); // Should OK | ||
} | ||
else { | ||
maybeArray.toFixed(); // OK | ||
} | ||
|
||
function fn3(arg: object) { | ||
if (Array.isArray(arg)) arg.push(""); // Should OK | ||
} | ||
|
||
function fn4(arg: {}) { | ||
if (Array.isArray(arg)) arg.push(""); // Should OK | ||
} | ||
|
||
function fn5(arg: string | ReadonlyArray<string>) { | ||
if (Array.isArray(arg)) arg.push(10); // Should FAIL | ||
if (Array.isArray(arg)) arg.push(""); // Should FAIL | ||
if (Array.isArray(arg)) arg.indexOf(""); // Should OK | ||
if (!Array.isArray(arg)) arg.toUpperCase(); // Should OK | ||
} | ||
|
||
function fn6(arg: string | string[]) { | ||
if (Array.isArray(arg)) arg.push(10); // Should FAIL | ||
} | ||
|
||
function fn7(arg: boolean | number[] | string[], stringAndNumber: string & number) { | ||
if (Array.isArray(arg)) arg.push(stringAndNumber); // Should OK | ||
} | ||
|
||
function fn8(arg: string | number[] | readonly string[]) { | ||
if (Array.isArray(arg)) arg.push(10); // Should FAIL | ||
} | ||
|
||
function fn9(arg: string | number[] | readonly string[]) { | ||
if (Array.isArray(arg)) arg.push(10); // Should FAIL | ||
} | ||
|
||
function fn10(arg: string | MyArray<string>) { | ||
if (Array.isArray(arg)) arg.push(10); // Should FAIL | ||
if (Array.isArray(arg)) arg.push(""); // Should OK | ||
if (Array.isArray(arg)) arg.manifest; // Should OK | ||
} | ||
|
||
function fn11(arg: string | MyReadOnlyArray<string>) { | ||
if (Array.isArray(arg)) arg.push(""); // Should FAIL | ||
if (Array.isArray(arg)) arg.indexOf(10); // Should FAIL | ||
if (Array.isArray(arg)) arg.indexOf(""); // Should OK | ||
if (Array.isArray(arg)) arg.manifest; // Should OK | ||
} | ||
|
||
function fn12<T>(arg: T | T[], t: T) { | ||
if (Array.isArray(arg)) arg.push(t); // Should OK | ||
} | ||
|
||
function fn13<T>(arg: T | ReadonlyArray<T>, t: T) { | ||
if (Array.isArray(arg)) arg.push(t); // Should fail | ||
if (Array.isArray(arg)) arg.indexOf(t); // OK | ||
} | ||
|
||
function fn14<T>(arg: T | [T], t: T) { | ||
if (Array.isArray(arg)) arg.push(t); // Should OK | ||
} | ||
|
||
function fn15<T>(arg: T | readonly [T], t: T) { | ||
if (Array.isArray(arg)) arg.push(t); // Should fail | ||
if (Array.isArray(arg)) arg.indexOf(t); // Should OK | ||
} | ||
|
||
function fn16<T extends string | string[]>(arg: T) { | ||
if (Array.isArray(arg)) arg.push("10"); // Should OK | ||
if (Array.isArray(arg)) arg.push(10); // Should fail | ||
} | ||
|
||
function fn17() { | ||
const s: Array<string | string[]> = []; | ||
const arrs = s.filter(Array.isArray); | ||
arrs.push(["one"]); // Should OK | ||
arrs.push("str"); // Should fail | ||
} | ||
|
||
|
||
//// [isArray.js] | ||
var maybeArray; | ||
if (Array.isArray(maybeArray)) { | ||
maybeArray.length; // OK | ||
function fn1(arg) { | ||
if (Array.isArray(arg)) | ||
arg.push(""); // Should OK | ||
} | ||
function fn2(arg) { | ||
if (Array.isArray(arg)) | ||
arg.push(""); // Should OK | ||
} | ||
function fn3(arg) { | ||
if (Array.isArray(arg)) | ||
arg.push(""); // Should OK | ||
} | ||
function fn4(arg) { | ||
if (Array.isArray(arg)) | ||
arg.push(""); // Should OK | ||
} | ||
function fn5(arg) { | ||
if (Array.isArray(arg)) | ||
arg.push(10); // Should FAIL | ||
if (Array.isArray(arg)) | ||
arg.push(""); // Should FAIL | ||
if (Array.isArray(arg)) | ||
arg.indexOf(""); // Should OK | ||
if (!Array.isArray(arg)) | ||
arg.toUpperCase(); // Should OK | ||
} | ||
function fn6(arg) { | ||
if (Array.isArray(arg)) | ||
arg.push(10); // Should FAIL | ||
} | ||
function fn7(arg, stringAndNumber) { | ||
if (Array.isArray(arg)) | ||
arg.push(stringAndNumber); // Should OK | ||
} | ||
function fn8(arg) { | ||
if (Array.isArray(arg)) | ||
arg.push(10); // Should FAIL | ||
} | ||
function fn9(arg) { | ||
if (Array.isArray(arg)) | ||
arg.push(10); // Should FAIL | ||
} | ||
function fn10(arg) { | ||
if (Array.isArray(arg)) | ||
arg.push(10); // Should FAIL | ||
if (Array.isArray(arg)) | ||
arg.push(""); // Should OK | ||
if (Array.isArray(arg)) | ||
arg.manifest; // Should OK | ||
} | ||
function fn11(arg) { | ||
if (Array.isArray(arg)) | ||
arg.push(""); // Should FAIL | ||
if (Array.isArray(arg)) | ||
arg.indexOf(10); // Should FAIL | ||
if (Array.isArray(arg)) | ||
arg.indexOf(""); // Should OK | ||
if (Array.isArray(arg)) | ||
arg.manifest; // Should OK | ||
} | ||
function fn12(arg, t) { | ||
if (Array.isArray(arg)) | ||
arg.push(t); // Should OK | ||
} | ||
function fn13(arg, t) { | ||
if (Array.isArray(arg)) | ||
arg.push(t); // Should fail | ||
if (Array.isArray(arg)) | ||
arg.indexOf(t); // OK | ||
} | ||
function fn14(arg, t) { | ||
if (Array.isArray(arg)) | ||
arg.push(t); // Should OK | ||
} | ||
function fn15(arg, t) { | ||
if (Array.isArray(arg)) | ||
arg.push(t); // Should fail | ||
if (Array.isArray(arg)) | ||
arg.indexOf(t); // Should OK | ||
} | ||
function fn16(arg) { | ||
if (Array.isArray(arg)) | ||
arg.push("10"); // Should OK | ||
if (Array.isArray(arg)) | ||
arg.push(10); // Should fail | ||
} | ||
else { | ||
maybeArray.toFixed(); // OK | ||
function fn17() { | ||
var s = []; | ||
var arrs = s.filter(Array.isArray); | ||
arrs.push(["one"]); // Should OK | ||
arrs.push("str"); // Should fail | ||
} |
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.