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.from does not accept Iterable<T>|ArrayLike<T> argument #20432

Closed
Jessidhia opened this issue Dec 4, 2017 · 2 comments · Fixed by #20467
Closed

Array.from does not accept Iterable<T>|ArrayLike<T> argument #20432

Jessidhia opened this issue Dec 4, 2017 · 2 comments · Fixed by #20467
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue Help Wanted You can do this

Comments

@Jessidhia
Copy link

TypeScript Version: 2.6.2

Code

function takeN<T> (n: number, input: Iterable<T>|ArrayLike<T>): [T[], T[]] {
  const array = Array.from(input)
  return [array.splice(0, n), array]
}

function takeNv2<T> (n: number, input: Iterable<T>|ArrayLike<T>): [T[], T[]] {
  const array = isIterable(input) ? Array.from(input) : Array.from(input)
  return [array.splice(0, n), array]

  function isIterable(arg: any): arg is Iterable<any> {
    return Symbol.iterator in arg
  }
}

Playground link

Expected behavior:

takeN to work

Actual behavior:

takeN raises an error on Array.from, because TypeScript does not accept that the input matches more than one overload.

This is either a compiler bug, or the declaration for Array.from needs an overload that accepts either.

@Jessidhia Jessidhia changed the title Array.from Array.from does not accept Iterable<T>|ArrayLike<T> argument Dec 4, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Dec 4, 2017

The root cause is #14107.
We can work around this by adding an overload for Array.from to accept ArrayLike<T> | Iterable<T> instead of just Iterable<T>.

A similar bug report was #20215 for Date constructor.

@mhegazy mhegazy added Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this labels Dec 4, 2017
@mhegazy mhegazy added this to the Community milestone Dec 4, 2017
@DanielRosenwasser DanielRosenwasser added the Fixed A PR has been merged for this issue label Dec 8, 2017
@DanielRosenwasser DanielRosenwasser modified the milestones: Community, TypeScript 2.7 Dec 8, 2017
@DanielRosenwasser
Copy link
Member

Thanks @Kovensky!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants