question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tuple iteration and merging

See original GitHub issue

Edit: Remove indexing part Edit 2: Replace tuple mapping syntax with normal mapped types

Search Terms

Concatentate and merge tuples’ entries.

Suggestion

  1. I would like to be able to extract and spread in positions other than the last part of a tuple.
  2. I would like to merge a tuple into an intersection or concatenation of its members.

Use Cases

  • Converting a tuple to its intersection, such as with Object.assign’s assignee.
  • Typing Array.prototype.concat correctly for tuples.

Syntax

The syntax comes in a few new forms:

  • [...A, B] appends B to the tuple A. Similarly, [...infer A, any] extends B ? A : [] drops the last item of the tuple and [...any[], infer A] extends B ? A : never extracts the first.
  • {... ...T} for an n-ary merge and [... ...T] an n-ary concatenation.

Examples

Here’s the types for each method I mentioned above.

interface Array<T> {
    concat<U extends any[]>(...others: U): [...this, ... ...{[I in keyof U]: (
        U[I] extends any[] ? N : U[I] extends ArrayLike<infer R> ? R[] : [U[I]]
    )}];
}

interface ObjectConstructor {
    assign(target: {... ...typeof sources}, ...sources: object[]): typeof target;
}

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)

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:23
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
lieenecommented, Nov 13, 2019

@weswigham this should fit your need and it enforces object and they are here

export type MergO<U extends object> = (U extends object ? (k: U) => void : never) extends (k: infer T) => void ? (T extends object ? T : object) : object;
export type UnionTupleType<A extends any[]> = A extends { [n: number]: infer T } ? T extends object ? T : never : never;
export type MergTupleType<A extends any[]> = MergO<UnionTupleType<A>>;
6reactions
karol-majewskicommented, Jan 22, 2019

Ignoring tuples for a second, I want a way to easily convert any union to an intersection

@weswigham check this out:

/**
 * @author https://stackoverflow.com/users/2887218/jcalz
 * @see https://stackoverflow.com/a/50375286/10325032
 */
type UnionToIntersection<Union> =
  (Union extends any
    ? (argument: Union) => void
    : never
  ) extends (argument: infer Intersection) => void
      ? Intersection
      : never;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Python Program to Merge tuple list by overlapping mid tuple
Given two lists that contain tuples as elements, the task is to write a Python program to accommodate tuples from the second list...
Read more >
How do I iterate over the tuples of the items of two or more lists ...
The zip function "returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or...
Read more >
5 Easy Ways to Iterate Through a Python List or Tuple
Python list comprehension is a simple, concise way of iterating through lists (or tuples) to access or alter values. The result of list...
Read more >
How we can iterate through a Python list of tuples?
Easiest way is to employ two nested for loops. Outer loop fetches each tuple and inner loop traverses each item from the tuple....
Read more >
Collections and Data Structures - Julia Documentation
Advance the iterator to obtain the next element. If no elements remain, nothing should be returned. Otherwise, a 2-tuple of the next element...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found