microsoft / microsoft/TypeScript

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

Open
#45,516 1 comment 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

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.

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

No file, test, or entry point is named. Start by reviewing the proposed destructuring-rest annotation syntax and motivating examples; done means the intended type-checking behavior is agreed and covered by compiler tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.