Skip to content
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

Array.isArray refinement loses the array's type #33700

Open
lll000111 opened this issue Oct 1, 2019 · 5 comments · May be fixed by #48228 or #49855
Open

Array.isArray refinement loses the array's type #33700

lll000111 opened this issue Oct 1, 2019 · 5 comments · May be fixed by #48228 or #49855
Labels
Fix Available A PR has been opened for this issue Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript

Comments

@lll000111
Copy link

lll000111 commented Oct 1, 2019

I realize there are a number of related issues. However, some where about ReadonlyArray, some didn't see directly related at all even though it was pointed there.

Also, this is a much smaller example, just a single line of code really:

Playground link

I know people will point to this ir that implementation or design decision detail — honestly guys, you got it REVERSED. The tool should serve the purpose, not the other way around!

That the array loses all it's type information in such a simple example is A BUG.

And it is possible to do much better: See here! I'm only pointing this out because at times the responses sound like "We tried everything but it is just not possible" when the competition shows that it is actually quite possible.

Related(?): #17002

@MartinJohns
Copy link
Contributor

There simply is no type-information available in the Array.isArray function for the compiler to keep the type, because it currently accepts any as an argument. Changing that would be a heavy breaking change that is not going to happen.

You can easily workaround this by providing your own version with more specific types, so there's type information available for the compiler, for example:

function isArray<T>(arg: Iterable<T>): arg is T[] {
    return Array.isArray(arg);
}

@jcalz
Copy link
Contributor

jcalz commented Oct 1, 2019

Exact duplicate of #32497, which was locked due to violations of the code of conduct. Let's please remember that disagreements should be handled constructively and respectfully.

@RyanCavanaugh RyanCavanaugh added Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript labels Oct 9, 2019
@RyanCavanaugh
Copy link
Member

What is the difference between this and #17002?

@RyanCavanaugh
Copy link
Member

Just to reiterate, the last thing we need is brow-beating about how THIS IS A BUG and YOU ARE SCREWING UP. We're trying to make a good product and we want the right things to happen; just 💩ing on our efforts doesn't make us more likely to take your issue seriously.

@jablko
Copy link
Contributor

jablko commented Mar 11, 2022

@RyanCavanaugh They're very similar. #17002 only mentions readonly T[] (preserving mutability and element type), whereas this one calls out the issue of still-wider types Iterable<T>. Maybe there's a non-generic solution to #17002, à la #42316, because e.g. the intersection readonly T[] & readonly unknown[] has typed properties length, concat, join, slice, and 14 more, whereas Iterable<T> & readonly unknown[] only has typed [Symbol.iterator] --- length, concat, join, slice, and 14 only exist on the readonly unknown[] component. A generic parameter is needed to instead make it Iterable<T> & readonly T[].

Well, just readonly T[] because that's a subtype of Iterable<T>? https://github.com/microsoft/TypeScript/blob/93c3a30edc7cc4be94bbeb3074a6a678ffd45d3b/src/compiler/checker.ts#L25061

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fix Available A PR has been opened for this issue Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript
Projects
None yet
6 participants