microsoft / microsoft/TypeScript

Convert named tuple into object literal type.

未关闭
#41,594 9 条评论 46 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Awaiting More Feedback Domain: LS: Refactorings Suggestion
主要语言
Go
星标
111k
派生
14.4k
平均合并
1 天 19 小时
30 天内合并 PR
117

描述

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.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先查看 TypeScript language-service 的重构基础设施和现有重构测试,然后将其中的约定与此 issue 中的 named tuple 示例进行比较。完成的标准是:重构可以将 named tuple 类型转换为等价的 object literal 类型,并支持所描述的基于属性的用法,同时有覆盖该转换的测试。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
developer-experience
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
32/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。