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.

Convert named tuple into object literal type.

See original GitHub issue

Search Terms

refactor

Suggestion

Provide a refactor that convert named tuple into object literal type.

Use Cases

Now we have named tuple. It’s common to write a tuple to pass multiple value. As time goes on, you may add many other fields into tuple. You cannot take a subsequence in the middle or the end of the tuple. If we could convert that into an object, That would be useful.

Examples

type Tuple = [number, number]

function aggregate(items: number[]): Tuple {
  // ...
  return [min, max]
}

const [ min, max ] = aggregate(items)

Growth to

type Tuple = [number, number, number, number, number, number]

function aggregate(items: number[]): Tuple {
  // ...
  return [min, max, avg, mid, first, last]
}

const [ min, max, avg, mid, first, last ] = aggregate(items)

If we want first and last only


const [ , , , , first, last ] = aggregate(items) // too many '.'

const result = aggregate(items);
const first = result[4] // magic number
const last = result[5]  // magic number

If we have the refactor

// add names into tuple
type Tuple = [min: number, max: number, avg: number, mid: number, first: number, last: number]

// apply refactor
type Tuple = {min: number, max: number, avg: number, mid: number, first: number, last: number}

function aggregate(items: number[]): Tuple {
  // ...
  return {min, max, avg, mid, first, last}
}

const { first, last } = aggregate(items)

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. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:23
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

29reactions
Rycochetcommented, Nov 2, 2021

Another use-case is the Parameters<fn> utility call - it returns a named tuple, but occasionally I want to change a long list of arguments (for a library so out of my control) into an object for a wrapper - if I could just do a single extra wrapper for TupleToObject<Parameters<fn>> then this would simplify the typings slightly, and ensure that they’re always correct if the arguments change (while currently they might change, but the types might not change enough to trigger a compiler warning).

10reactions
harrysolovaycommented, Nov 24, 2020

A good solution might depend on an intrinsic utility type for tuple label access.

type X = [first: string, second: number];
type Label0 = Label<X, 0>; // "first"
type Label0 = Label<X, 1>; // "second"

One could perform the mapping like so:

type XAsRec = {
  [I in Extract<keyof X, number> as Label<X, I>]: X[I];
} // {first: string; second: number}
Read more comments on GitHub >

github_iconTop Results From Across the Web

transform named tuple to object - typescript - Stack Overflow
Given that the type [a: string, b: number] and [c: string, d: number] are the same type, there's no principled way to convert...
Read more >
Write Pythonic and Clean Code With namedtuple - Real Python
Converting namedtuple Instances Into Dictionaries​​ _asdict() on a named tuple, you get a new dict object that maps field names to their corresponding...
Read more >
Real use cases for named tuples in TypeScript - LogRocket Blog
With tuples, we can easily construct special kinds of arrays, where elements are of fixed types with respect to an index or position....
Read more >
Crazy, Powerful TypeScript Tuple Types - Instil Software
A tuple object has a fixed size, and the type of each element is known ... the TypeScript code has been converted to...
Read more >
How can I convert a Python Named tuple to a dictionary?
How can I convert a Python Named tuple to a dictionary? - Namedtuple class is defined in the collections module. It returns a...
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