GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Allow ArrayLike as a spread source · Issue #7596 · microsoft/TypeScript

Allow ArrayLike as a spread source #7596

New issue
Copy link
New issue
Copy link
Closed
Closed
Allow ArrayLike as a spread source#7596
Copy link
Labels
ES6Relates to the ES6 SpecRelates to the ES6 SpecIn DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript

Description

@iby
Ian Bytchek (iby)
opened on Mar 20, 2016
Issue body actions

When trying to pass caller arguments to another function with ...arguments the compiler complains with Type 'IArguments' is not an array type. Version 1.8.9.

Reacted by AA, Aaron Resnick, Code Scratcher, Emil Johansson, Cory Kleiser and Jan Molak

Activity

Next
  1. jwbay commented on Mar 20, 2016

    @jwbay
    jwbay
    on Mar 20, 2016
    Contributor
    More actions

    The arguments object is not an array type (though it resembles one), so the compiler error seems correct. Accept the arguments with a rest operator and they'll get converted to an array.

    function foo(...args) {
       bar(...args);
    }
    Reacted by Ian Bytchek, jeffreymorlan and Code Scratcher
  2. iby commented on Mar 20, 2016

    @iby
    Ian Bytchek (iby)
    on Mar 20, 2016
    Author
    More actions

    Now when you say it, I remember looking it up a dozen times now… Still, that functionality would be natural, especially given that apply can be used directly with arguments.

  3. Mohamed Hegazy (mhegazy)
    added
    QuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code
    on Mar 28, 2016
  4. Mohamed Hegazy (mhegazy)
    closed this as completedon Mar 28, 2016
  5. iby commented on Mar 28, 2016

    @iby
    Ian Bytchek (iby)
    on Mar 28, 2016
    Author
    More actions

    Mohamed Hegazy (@mhegazy) I wasn't asking a question. This works in javascript, typescript fails to compile. arguments is not an array, it's a fact, but it should be compatible with the spread operator.

    Reacted by Code Scratcher and Cory Kleiser
  6. mhegazy commented on Mar 28, 2016

    @mhegazy
    Mohamed Hegazy (mhegazy)
    on Mar 28, 2016
    Contributor
    More actions

    it is not an array. quoting MDN:

    The arguments object is an Array-like object corresponding to the arguments passed to a function.

    it goes on to say:

    The arguments object is not an Array. It is similar to an Array, but does not have any Array properties except length. For example, it does not have the pop method. However it can be converted to a real Array

    so,

    function f() {
      arguments.push(0);          // Runtime error: Uncaught TypeError: arguments.push is not a function(…)
      arguments.indexOf(0);       // Runtime error: Uncaught TypeError: arguments.indexOf is not a 
      Array.isArray(arguments);   // false
    }
    f(1,2);
    Reacted by Cory Kleiser
  7. iby commented on Mar 29, 2016

    @iby
    Ian Bytchek (iby)
    on Mar 29, 2016
    Author
    More actions

    We're having a miscommunication. I am not saying it's an array, I'm stating the opposite, that it is not array, but it should be compatible with the spread operator.

    function foo(){
        console.log(...arguments);
    }
    
    foo('bar', 'baz');

    This throws a compile error, but produces a working code. Naturally arguments would be expected to work with the spread operator.

  8. Mohamed Hegazy (mhegazy)
    added
    SuggestionAn idea for TypeScriptAn idea for TypeScript
    In DiscussionNot yet reached consensusNot yet reached consensus
    and removed
    QuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code
    on Mar 29, 2016
  9. mhegazy commented on Mar 29, 2016

    @mhegazy
    Mohamed Hegazy (mhegazy)
    on Mar 29, 2016
    Contributor
    More actions

    i see.

    well. some of the transformation on a spread uses slice. which does not exist. in theory we can allow ArrayLike, and call Array.prototype.slice.apply(v) instead of v.slice

    Reacted by Ian Bytchek, Richard Simpson, Pavel Birukov and Jan Molak
  10. mhegazy commented on Mar 29, 2016

    @mhegazy
    Mohamed Hegazy (mhegazy)
    on Mar 29, 2016
    Contributor
    More actions

    updating the title.

  11. Mohamed Hegazy (mhegazy)
    changed the title [-]Type 'IArguments' is not an array type[/-] [+]Allow ArayLike as a spread soruce[/+] on Mar 29, 2016
  12. Ian Bytchek (iby)
    changed the title [-]Allow ArayLike as a spread soruce[/-] [+]Allow ArayLike as a spread source[/+] on Mar 29, 2016
  13. kitsonk commented on Mar 30, 2016

    @kitsonk
    Kitson Kelly (kitsonk)
    on Mar 30, 2016
    Contributor
    More actions

    It isn't valid ES6 syntax:

    'use strict';
    
    function bar() {
      console.log(arguments);
    }
    
    function foo() {
      bar.call(this, ...arguments);
    }
    
    foo('bar', 'qat');

    It fails in Firefox (but not in Chrome or Edge) and so would have to be transformed even when targeting ES6 with little or no value. It was the "magic" behaviours of arguments was a big part of the value of the spread/rest operator in the first place.

  14. 17 remaining items

  15. mhegazy commented on May 11, 2016

    @mhegazy
    Mohamed Hegazy (mhegazy)
    on May 11, 2016
    Contributor
    More actions

    oh. did not think of that. duh.. back to the drawing board then :)

  16. Mohamed Hegazy (mhegazy)
    added
    In DiscussionNot yet reached consensusNot yet reached consensus
    and removed
    Accepting PRs
    Effort: Easy
    on May 11, 2016
  17. Mohamed Hegazy (mhegazy)
    removed this from the This milestone has been deleted milestone on May 11, 2016
  18. pablobirukov commented on Jan 3, 2017

    @pablobirukov
    Pavel Birukov (pablobirukov)
    on Jan 3, 2017
    More actions

    [...document.querySelectorAll(".foo")] triggers Type 'NodeListOf<Element>' is not an array type (with target: "es5").
    But I think it would be nice to have it successfully transpiled to Array.prototype.slice.call(document.querySelectorAll(...)) since NodeListOf<Element> is iterable

  19. mhegazy commented on Jan 3, 2017

    @mhegazy
    Mohamed Hegazy (mhegazy)
    on Jan 3, 2017
    Contributor
    More actions

    This should be addressed by #12346

  20. aaron-resnick commented on Dec 29, 2017

    @aaron-resnick
    Aaron Resnick (aaron-resnick)
    on Dec 29, 2017
    More actions

    I believe #12346 did not address this, as I'm still seeing the same error in TS 2.6.2

  21. kitsonk commented on Dec 29, 2017

    @kitsonk
    Kitson Kelly (kitsonk)
    on Dec 29, 2017
    Contributor
    More actions

    aaronresnick as mentioned in the PR text, when targeting ES3/ES5 you have to use --downlevelIteration. Are you using that?

    Reacted by Cory Kleiser and ueli k
    Reacted by Cory Kleiser
  22. vitaly-t commented on Feb 26, 2018

    @vitaly-t
    Code Scratcher (vitaly-t)
    on Feb 26, 2018
    More actions

    So, to clarify, as of this day it still doesn't work, and the reason being - it doesn't work in FireFox.

    In the meantime, all our Node.js code in JavaScript uses arguments for spread operator without any issues, but the TypeScript can't/won't do it regardless, for compatibility with FireFox.

    Shouldn't support for spread of array-like objects become the default at this point, or at least default when building for the server-side?

    Reacted by Cory Kleiser, Mike Murkovic and Sushruth Sastry
  23. Daniel Rosenwasser (DanielRosenwasser)
    changed the title [-]Allow ArayLike as a spread source[/-] [+]Allow ArrayLike as a spread source[/+] on Jun 18, 2018
  24. fregante commented on May 24, 2019

    @fregante
    fregante
    on May 24, 2019
    More actions

    The spread operator only works on Iterables and ArrayLikes aren't Iterables.

    // from lib.es5.d.ts
    interface ArrayLike<T> {
        readonly length: number;
        readonly [n: number]: T;
    }

    Try this anywhere:

    [...{length: 0}];
    // TypeError: ({length:0}) is not iterable

    arguments however is both Iterable and ArrayLike, and it already works correctly in TypeScript:

    [...arguments]

    This issue should be closed as resolved or invalid.

    Reacted by Trotyl Yu
  25. sushruth commented on Mar 12, 2021

    @sushruth
    Sushruth Sastry (sushruth)
    on Mar 12, 2021
    More actions

    Valid -

    I have now realized that ReadonlyArray<> is a better tool for this job than ArrayLike<>. Text below is invalid.

    Invalid -

    One problem I had that led me to this issue was not being able to type spread arguments as ArrayLike like so -

    function getProps<K extends ArrayLike<keyof User>>(...props: K) {
      // above line gives this error at `...props: K`  - A rest parameter must be of an array type.(2370)
    }

    However there is a tiny workaround for this -

    function getProps2<K extends ArrayLike<keyof User>>(...props: K[number][]) {
      // No error above!
    }

    I hope this helps some.

  26. RyanCavanaugh commented on Jan 25, 2024

    @RyanCavanaugh
    Ryan Cavanaugh (RyanCavanaugh)
    on Jan 25, 2024
    Member
    More actions

    This works now

  27. Ryan Cavanaugh (RyanCavanaugh)
    closed this as completedon Jan 25, 2024
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

    ES6Relates to the ES6 SpecRelates to the ES6 SpecIn DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.