8000 Array.from does not accept Iterable<T>|ArrayLike<T> argument · Issue #20432 · microsoft/TypeScript · GitHub 8000
[go: up one dir, main page]

Skip to content
Array.from does not accept Iterable<T>|ArrayLike<T> argument #20432
@Jessidhia

Description

@Jessidhia

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptFixedA PR has been merged for this issueHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0