Skip to content

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

Open

Description

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

Activity

  1. MartinJohns commented on Oct 1, 2019

    @MartinJohns
    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);
    }
    
  2. jcalz commented on Oct 1, 2019

    @jcalz
    Contributor

    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.

  3. RyanCavanaugh commented on Oct 9, 2019

    @RyanCavanaugh
    Member

    What is the difference between this and #17002?

  4. RyanCavanaugh commented on Oct 9, 2019

    @RyanCavanaugh
    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.

  5. jablko commented on Mar 11, 2022

    @jablko
    Contributor

    Ryan Cavanaugh (@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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions