Typescript: Array.isArray refinement loses the array's type

Created on 1 Oct 2019  Â·  4Comments  Â·  Source: microsoft/TypeScript

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

Needs Proposal Suggestion

Most helpful comment

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.

All 4 comments

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);
}

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.

What is the difference between this and #17002?

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jbondc picture jbondc  Â·  3Comments

weswigham picture weswigham  Â·  3Comments

Roam-Cooper picture Roam-Cooper  Â·  3Comments

uber5001 picture uber5001  Â·  3Comments

blendsdk picture blendsdk  Â·  3Comments