Allow a type annotation for indicating the type of a destructured rest assignment
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 30/100
- Issue 类型
- 功能
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- javascript, typescript
- 领域
- compilers
调研方向
未指定文件、测试或入口点。首先审查提议的 destructuring-rest 注解语法和动机示例;当预期的类型检查行为达成一致并由编译器测试覆盖时,即视为完成。
由索引模型根据 Issue 内容生成。
描述
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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
microsoft/TypeScript 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 88/100
microsoft/TypeScript#64322 · 2 条评论 · 1 个 reaction · 已指派 2 人 ·
-
Possible Improvement
难度 2/5 1-3 小时 新手友好度 78/100
microsoft/TypeScript#64278 · 1 条评论 · 1 个 reaction ·
-
Docs
难度 2/5 1-3 小时 新手友好度 70/100
microsoft/TypeScript#64118 · 1 条评论 ·
-
难度 1/5 1 小时以内 新手友好度 88/100
microsoft/TypeScript#64094 ·
-
Docs
难度 2/5 1-3 小时 新手友好度 76/100
microsoft/TypeScript#63959 · 5 条评论 ·
查看 microsoft/TypeScript 的全部 Issue
相似的 Issue
-
Type/Bug
难度 2/5 1-3 小时 新手友好度 78/100
OpenNSW/nsw-srilanka#497 ·
-
难度 1/5 1 小时以内 新手友好度 92/100
milvus-io/birdwatcher#545 ·
-
kind/bug
难度 2/5 1-3 小时 新手友好度 88/100
kubernetes-sigs/prow#953 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 88/100
-
难度 2/5 1-3 小时 新手友好度 88/100
caddyserver/caddy#8046 ·