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

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

Open
4 tasks done
KSXGitHub opened this issue Oct 12, 2018 · 7 comments
Open
4 tasks done

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

KSXGitHub opened this issue Oct 12, 2018 · 7 comments
Labels
Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@KSXGitHub
Copy link
Contributor

KSXGitHub commented Oct 12, 2018

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)
@ghost ghost added the Suggestion An idea for TypeScript label Oct 12, 2018
@weswigham weswigham added Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript In Discussion Not yet reached consensus labels Nov 6, 2018
@aleclarson
Copy link

aleclarson commented Feb 28, 2019

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

Missing search keyword: spread, spreading

@jcalz
Copy link
Contributor

jcalz commented Mar 6, 2019

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

@aleclarson
Copy link

@jcalz I believe the proposed behavior for spreading tuples is:

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

@KSXGitHub Can you add this to the OP please?

@KSXGitHub
Copy link
Contributor Author

KSXGitHub commented Mar 6, 2019

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

@KSXGitHub Can you add this to the OP please?

I will!

@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' }

@KSXGitHub
Copy link
Contributor Author

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?

@jcalz
Copy link
Contributor

jcalz commented May 1, 2020

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

@jakebailey
Copy link
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
Labels
Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants