microsoft / microsoft/TypeScript

Convert named tuple into object literal type.

Open
#41,594 9 comments 46 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Domain: LS: Refactorings Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the TypeScript language-service refactor infrastructure and existing refactor tests, then compare their conventions with the named-tuple examples in this issue. Done means a refactor can convert a named tuple type into an equivalent object literal type and supports the described property-based usage, with tests covering the transformation.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
developer-experience
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.