Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 13.8k
A second attempt at readonly array support persisting through isArray#42316
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,12 @@ | ||
| tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOf.ts(7,20): error TS2339: Property 'global' does not exist on type 'never'. | ||
| The intersection 'I & RegExp' was reduced to 'never' because property 'global' has conflicting types in some constituents. | ||
| tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOf.ts(31,11): error TS2339: Property 'onChanges' does not exist on type 'C | (Validator & Partial<OnChanges>)'. | ||
| Property 'onChanges' does not exist on type 'C'. | ||
| tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOf.ts(32,11): error TS2339: Property 'onChanges' does not exist on type 'C | (Validator & Partial<OnChanges>)'. | ||
| Property 'onChanges' does not exist on type 'C'. | ||
| ==== tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOf.ts (1 errors) ==== | ||
| ==== tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOf.ts (3 errors) ==== | ||
| interface I { global: string; } | ||
| var result!: I; | ||
| var result2!: I; | ||
| @@ -37,7 +41,13 @@ tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOf.ts(7,20) | ||
| } | ||
| v // Validator & Partial<OnChanges> via subtype reduction | ||
| if (v.onChanges) { | ||
| ~~~~~~~~~ | ||
| !!! error TS2339: Property 'onChanges' does not exist on type 'C | (Validator & Partial<OnChanges>)'. | ||
Contributor 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. this looks like a regression. I reported this case back then, because the effect of | ||
| !!! error TS2339: Property 'onChanges' does not exist on type 'C'. | ||
| v.onChanges({}); | ||
| ~~~~~~~~~ | ||
| !!! error TS2339: Property 'onChanges' does not exist on type 'C | (Validator & Partial<OnChanges>)'. | ||
| !!! error TS2339: Property 'onChanges' does not exist on type 'C'. | ||
| } | ||
| } | ||
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.
This is a safer type, but it can't help but break anybody who was previously able to mutate an array or use its contents after checking it with isArray. I think it's too breaky for the benefit.
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.
Nathan Shively-Sanders (@sandersn) You mean anybody who was previously able to mutate a mutable array after checking it with isArray? I tested this change locally and confirmed it doesn't break that: Mutable arrays remain mutable. The type predicate narrows the type to the intersection of
typeof arg & readonly unknown[], which is only immutable ifargwas already immutable.Would you consider reopening this PR?
⏯️ Playground link
Workbench repro.
🧑💻 Code
🙁 Actual behavior
No errors: You're free to mutate an immutable array.
🙂 Expected behavior
With this PR:
Mutating an immutable array is an error.
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.
What I mean is that if somebody writes this today, it's not an error:
unknownnarrowing is an important scenario for Array.isArray, and this change would add errors that were not there before and are not necessarily correct.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.
Nathan Shively-Sanders (@sandersn) I should've realized what you meant, thanks for your patience.
Instead of replacing the existing signature, would adding overloads for
ArrayLike<T>andIterable<T>work, without affecting the current behavior ofanyandunknown? I've opened #48228 with that modification to this PR.