Convert named tuple into object literal type.
See original GitHub issueSearch 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:
- Created 3 years ago
- Reactions:23
- Comments:7 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 forTupleToObject<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).A good solution might depend on an intrinsic utility type for tuple label access.
One could perform the mapping like so: