-
Notifications
You must be signed in to change notification settings - Fork 13.4k
isArray() preserve mutability and element type #48228
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
Open
jablko
wants to merge
3
commits into
microsoft:main
Choose a base branch
from
jablko:patch-51
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
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 2 commits
Commits
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
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
| 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 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,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.
Uh oh!
There was an error while loading. Please reload this page.