Allow a type annotation for indicating the type of a destructured rest assignment

未关闭
#45,516 1 条评论 7 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
30/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
领域
compilers

调研方向

未指定文件、测试或入口点。首先审查提议的 destructuring-rest 注解语法和动机示例;当预期的类型检查行为达成一致并由编译器测试覆盖时,即视为完成。

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

描述

Awaiting More Feedback Suggestion

Suggestion

I offer allow to assign type to rest in destructuring assignment.

🔍 Search Terms

destructuring assignment, desctructuring, rest

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

const { e, ...rest }: { e: number, ...rest: ICalculatedNumbers } = test;

For better understanding please check Use Cases

📃 Motivating Example

I wanted to assign new calculated value to destructured assigment to field that moved out to new variable by destructuring, but I can't do it because new type exists without field and I'm not able to assign new type to desctructuring assignment.

Provided example in Use Cases

💻 Use Cases

Current behavior in TypeScript
export interface INumbersE {
    c: number;
    d: number;
}

export interface INumbers {
    a?: number;
    b?: number;
    c?: number;
    d?: number;
    e?: INumbersE;
}

export interface ICalculatedNumbers {
    a?: number;
    b?: number;
    c?: number;
    d?: number;
    e?: number;
}

const calcTheNumber = (numbers: ICalculatedNumbers) => {
    // another logic goes there
    console.log(numbers);
}

const test: INumbers = {
    a: 1,
    b: 2,
    c: 3,
    d: 4,
    e: {
        c: 2,
        d: 3
    }
};


const { e, ...rest }: { e: number, ...rest: ICalculatedNumbers } = test; // Property 'rest' is missing in type 'INumbers' but required in type '{ e: number; rest: ICalculatedNumbers; }'

if (e && e.c + e.d === 5) { // Property 'd' does not exist on type 'number' || Property 'c' does not exist on type 'number'
    rest.e = 5; // Property 'e' does not exist on type '{ a: number; b: number; c: number; d: number; }'
}

calcTheNumber(rest); // Type '{ rest: ICalculatedNumbers; }' has no properties in common with type 'ICalculatedNumbers'.
Running in pure JS right as expected behavior and getting expected object with e: 5
const test = {
    a: 1,
    b: 2,
    c: 3,
    d: 4,
    e: {
        c: 2,
        d: 3
    }
};

const calcTheNumber = (numbers) => {
    console.log(numbers); // { a: 1, b: 2, c: 3, d: 4, e: 5 }
}

const { e, ...rest } = test;

if (e && e.c + e.d === 5) {
    rest.e = 5;
}

calcTheNumber(rest);

If cover errors with //@ts-ignore, it'll run as expected, but I'm not ok with that.

主要语言
Go
星标
111k
派生
14.4k
平均合并
1 天 19 小时
30 天内合并 PR
117

贡献指南

打开贡献指南

从这里开始

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

microsoft/TypeScript 的其他 Issue

查看 microsoft/TypeScript 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

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