Skip to content

Improve Array.from(tuple) and [...tuple] #27859

Open

Description

@KSXGitHub

Suggestion

Array.from(tuple) and [...tuple] should preserve individual types that made up tuple.

Proposal

Array.from (Simple)

Add this overload to Array.from:

interface ArrayConstructor {
  from<T extends any[]> (array: T): T
}

demo 1

Caveats:

  • The above definition preserves everything including unrelated properties that do not belong to Array.prototype whilst actual Array.from discards them. (for instance, if input has foo: 'bar', output array will also has foo: 'bar').

Array.from (Complete)

Fix above caveats.

interface ArrayConstructor {
  from<T extends any[]> (array: T): CloneArray<T>
}

type CloneArray<T extends any[]> = {
  [i in number & keyof T]: T[i]
} & {
  length: T['length']
} & any[]

demo 2

Spread operator

declare const tuple: [0, 1, 2] & { foo: 'bar' }

// $ExpectType [string, string, 0, 1, 2, string, string]
const clone = ['a', 'b', ...tuple, 'c', 'd']

Note that typeof clone does not contain { foo: 'bar' }.

Use Cases

  • Clone a tuple without losing type information.

Examples

Clone a tuple

const a: [0, 1, 2] = [0, 1, 2]

// $ExpectType [0, 1, 2]
const b = Array.from(a)

Clone a generic tuple

function clone<T extends any[]> (a: T): T {
  return Array.from(a)
}

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript / JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. new expression-level syntax)

Activity

  1. ghost added
    SuggestionAn idea for TypeScript
    on Oct 12, 2018
  2. aleclarson commented on Feb 28, 2019

    @aleclarson

    Bump. The current behavior doesn't make any sense to me.

    Missing search keyword: spread, spreading

  3. jcalz commented on Mar 6, 2019

    @jcalz
    Contributor

    I'm not seeing how this proposal addresses [...tuple]. I only see Array.from(tuple) discussed. What am I missing?

  4. aleclarson commented on Mar 6, 2019

    @aleclarson

    Joe Calzaretta (@jcalz) I believe the proposed behavior for spreading tuples is:

    [...tuple] should return typeof tuple instead of tuple[number][].

    Khải (@KSXGitHub) Can you add this to the OP please?

  5. KSXGitHub commented on Mar 6, 2019

    @KSXGitHub
    ContributorAuthor

    I'm not seeing how this proposal addresses [...tuple]. I only see Array.from(tuple) discussed. What am I missing?

    Khải (@KSXGitHub) Can you add this to the OP please?

    I will!

    Joe Calzaretta (@jcalz) I believe the proposed behavior for spreading tuples is:

    [...tuple] should return typeof tuple instead of tuple[number][].

    No, not typeof tuple. You did not consider the possibility that tuple may have non-array properties, for example:

    declare const tuple: [0, 1, 2] & { foo: 'bar' } // typeof tuple → [0, 1, 2] & { foo: 'bar' }
    const clone = [...tuple] // should be [0, 1, 2] and without { foo: 'bar' }
  6. KSXGitHub commented on Mar 6, 2019

    @KSXGitHub
    ContributorAuthor

    By adding "Spread operator" section (as suggested by #27859 (comment) and #27859 (comment)), this issue is no longer within the boundary of lib.d.ts but requires changes from TypeScript engine itself. Should I move the section to a new issue or should I keep it here?

  7. jcalz commented on May 1, 2020

    @jcalz
    Contributor

    It looks like #36861 handles tuple spread... but only on pure tuple types like [0, 1, 2] and not those intersected with objects like [0, 1, 2] & { foo: 'bar' } in the example. Cross-referencing anyway:

    declare const a: [0, 1, 2];
    const b = [-2, -1, ...a, 3, 4, 5] as const;
    // const b: readonly [-2, -1, 0, 1, 2, 3, 4, 5]
    
    declare const c: [0, 1, 2] & { foo: "bar" };
    const d = [-2, -1, ...c, 3, 4, 5] as const;
    // const d: readonly (0 | 3 | 1 | 2 | -2 | -1 | 4 | 5)[]

    Playground link

  8. jakebailey commented on Jul 9, 2023

    @jakebailey
    Member

    Looking at old issues, interestingly as of TS 4.2, the above example is now:

    declare const a: [0, 1, 2];
    const b = [-2, -1, ...a, 3, 4, 5] as const;
    //    ^?
    // const b: readonly [-2, -1, 0, 1, 2, 3, 4, 5]
    
    declare const c: [0, 1, 2] & { foo: "bar" };
    const d = [-2, -1, ...c, 3, 4, 5] as const;
    //    ^?
    // const d: readonly [-2, -1, ...(0 | 1 | 2), 3, 4, 5]

    Playground Link

    So, it gets the two ends correct.

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

    Domain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptIn DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions