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
isArray() preserve mutability and element type #48228
Open
jablko
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
jablko:patch-51
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,7 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts( | |
a1(...array2); // Error parameter type is (number|string)[] | ||
~~~~~~ | ||
!!! error TS2552: Cannot find name 'array2'. Did you mean 'Array'? | ||
!!! related TS2728 /.ts/lib.es5.d.ts:1470:13: 'Array' is declared here. | ||
!!! related TS2728 /.ts/lib.es5.d.ts:1471:13: 'Array' is declared here. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please clarify why it has been changed? |
||
a5([1, 2, "string", false, true]); // Error, parameter type is [any, any, [[any]]] | ||
~~~~~~~~ | ||
!!! error TS2322: Type 'string' is not assignable to type '[[any]]'. | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
tests/cases/compiler/isArray.ts(15,9): error TS4104: The type 'readonly string[]' is 'readonly' and cannot be assigned to the mutable type 'string[]'. | ||
tests/cases/compiler/isArray.ts(26,9): error TS2322: Type 'readonly string[]' is not assignable to type 'readonly void[]'. | ||
Type 'string' is not assignable to type 'void'. | ||
tests/cases/compiler/isArray.ts(32,9): error TS2322: Type 'readonly string[]' is not assignable to type 'readonly void[]'. | ||
tests/cases/compiler/isArray.ts(41,9): error TS2322: Type 'any[]' is not assignable to type 'void'. | ||
tests/cases/compiler/isArray.ts(47,9): error TS2322: Type 'any[]' is not assignable to type 'void'. | ||
|
||
|
||
==== tests/cases/compiler/isArray.ts (5 errors) ==== | ||
/// @errors: 2322 4104 | ||
|
||
// https://github.com/microsoft/TypeScript/issues/17002 | ||
// Preserves mutability, false branch removes arrays, mutable or not | ||
|
||
declare const mutable: string | string[]; | ||
if (Array.isArray(mutable)) { | ||
const stillMutable: string[] = mutable; | ||
} else { | ||
const narrowed: string = mutable; | ||
} | ||
|
||
declare const immutable: string | readonly string[]; | ||
if (Array.isArray(immutable)) { | ||
const notMutable: string[] = immutable; // Should fail: readonly string[] isn't assignable to string[] | ||
~~~~~~~~~~ | ||
!!! error TS4104: The type 'readonly string[]' is 'readonly' and cannot be assigned to the mutable type 'string[]'. | ||
} else { | ||
const narrowed: string = immutable; | ||
} | ||
|
||
// https://github.com/microsoft/TypeScript/issues/33700 | ||
// Preserves element or iterated type of wider types | ||
|
||
declare const arrayLike: string | ArrayLike<string>; | ||
if (Array.isArray(arrayLike)) { | ||
const arrayOfElementType: readonly string[] = arrayLike; | ||
const notArrayOfAny: readonly void[] = arrayLike; // Should fail: string isn't assignable to void | ||
~~~~~~~~~~~~~ | ||
!!! error TS2322: Type 'readonly string[]' is not assignable to type 'readonly void[]'. | ||
!!! error TS2322: Type 'string' is not assignable to type 'void'. | ||
} | ||
|
||
declare const iterable: string | Iterable<string>; | ||
if (Array.isArray(iterable)) { | ||
const arrayOfIteratedType: readonly string[] = iterable; | ||
const notArrayOfAny: readonly void[] = iterable; // Should fail: string isn't assignable to void | ||
~~~~~~~~~~~~~ | ||
!!! error TS2322: Type 'readonly string[]' is not assignable to type 'readonly void[]'. | ||
} | ||
|
||
// https://github.com/microsoft/TypeScript/pull/42316#discussion_r823218462 | ||
// any and unknown backward compatibility | ||
|
||
declare const any: any; | ||
if (Array.isArray(any)) { | ||
const mutableArrayOfAny: void[] = any; | ||
const notAny: void = any; // Should fail: any[] isn't assignable to void | ||
~~~~~~ | ||
!!! error TS2322: Type 'any[]' is not assignable to type 'void'. | ||
} | ||
|
||
declare const unknown: unknown; | ||
if (Array.isArray(unknown)) { | ||
const mutableArrayOfAny: void[] = unknown; | ||
const notAny: void = unknown; // Should fail: any[] isn't assignable to void | ||
~~~~~~ | ||
!!! error TS2322: Type 'any[]' is not assignable to type 'void'. | ||
} | ||
|
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 |
---|---|---|
@@ -1,19 +1,81 @@ | ||
//// [isArray.ts] | ||
var maybeArray: number | number[]; | ||
/// @errors: 2322 4104 | ||
|
||
// https://github.com/microsoft/TypeScript/issues/17002 | ||
// Preserves mutability, false branch removes arrays, mutable or not | ||
|
||
if (Array.isArray(maybeArray)) { | ||
maybeArray.length; // OK | ||
declare const mutable: string | string[]; | ||
if (Array.isArray(mutable)) { | ||
const stillMutable: string[] = mutable; | ||
} else { | ||
const narrowed: string = mutable; | ||
} | ||
else { | ||
maybeArray.toFixed(); // OK | ||
} | ||
|
||
declare const immutable: string | readonly string[]; | ||
if (Array.isArray(immutable)) { | ||
const notMutable: string[] = immutable; // Should fail: readonly string[] isn't assignable to string[] | ||
} else { | ||
const narrowed: string = immutable; | ||
} | ||
|
||
// https://github.com/microsoft/TypeScript/issues/33700 | ||
// Preserves element or iterated type of wider types | ||
|
||
declare const arrayLike: string | ArrayLike<string>; | ||
if (Array.isArray(arrayLike)) { | ||
const arrayOfElementType: readonly string[] = arrayLike; | ||
const notArrayOfAny: readonly void[] = arrayLike; // Should fail: string isn't assignable to void | ||
} | ||
|
||
declare const iterable: string | Iterable<string>; | ||
if (Array.isArray(iterable)) { | ||
const arrayOfIteratedType: readonly string[] = iterable; | ||
const notArrayOfAny: readonly void[] = iterable; // Should fail: string isn't assignable to void | ||
} | ||
|
||
// https://github.com/microsoft/TypeScript/pull/42316#discussion_r823218462 | ||
// any and unknown backward compatibility | ||
|
||
declare const any: any; | ||
if (Array.isArray(any)) { | ||
const mutableArrayOfAny: void[] = any; | ||
const notAny: void = any; // Should fail: any[] isn't assignable to void | ||
} | ||
|
||
declare const unknown: unknown; | ||
if (Array.isArray(unknown)) { | ||
const mutableArrayOfAny: void[] = unknown; | ||
const notAny: void = unknown; // Should fail: any[] isn't assignable to void | ||
} | ||
|
||
|
||
//// [isArray.js] | ||
var maybeArray; | ||
if (Array.isArray(maybeArray)) { | ||
maybeArray.length; // OK | ||
/// @errors: 2322 4104 | ||
if (Array.isArray(mutable)) { | ||
const stillMutable = mutable; | ||
} | ||
else { | ||
const narrowed = mutable; | ||
} | ||
if (Array.isArray(immutable)) { | ||
const notMutable = immutable; // Should fail: readonly string[] isn't assignable to string[] | ||
} | ||
else { | ||
maybeArray.toFixed(); // OK | ||
const narrowed = immutable; | ||
} | ||
if (Array.isArray(arrayLike)) { | ||
const arrayOfElementType = arrayLike; | ||
const notArrayOfAny = arrayLike; // Should fail: string isn't assignable to void | ||
} | ||
if (Array.isArray(iterable)) { | ||
const arrayOfIteratedType = iterable; | ||
const notArrayOfAny = iterable; // Should fail: string isn't assignable to void | ||
} | ||
if (Array.isArray(any)) { | ||
const mutableArrayOfAny = any; | ||
const notAny = any; // Should fail: any[] isn't assignable to void | ||
} | ||
if (Array.isArray(unknown)) { | ||
const mutableArrayOfAny = unknown; | ||
const notAny = unknown; // Should fail: any[] isn't assignable to void | ||
} |
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.
Shall it be
unknown
instead, since arg isunknown
?