Skip to content

Destructuring with type annotations #7576

@osi-oswald

Description

@osi-oswald

Using destructuring in functions signatures is a relief, but when combining it with type declarations there is a lot of duplication necessary:

function foo(
  {paramA, paramB, paramC, paramD = null}
  :{paramA: number, paramB: string, paramC: boolean, paramD?: Object}
) {}

function bar(
  [paramA, paramB, paramC, paramD]
  :[number, string, boolean, Object]
) {}

Would it make sense to introduce the following syntax to reduce duplication of code?

function foo(
  {paramA as number, paramB as string, paramC as boolean, paramD? as Object}
) {}

function bar(
  [paramA as number, paramB as string, paramC as boolean, paramD as Object]
) {}

Originally I wanted to suggest to use the colon ":" instead of the "as", but since the colon is used for renaming destructured parametes, the "as" is the only option I guess.

With this syntax change the following would be possible too I guess:

let {paramA as number, paramB as string, paramC as boolean, paramD as Object} = anyObject;
let {paramA: a as number, paramB: b as string, paramC: c as boolean, paramD: d as Object} = anyObject;
let [paramA as number, paramB as string, paramC as boolean, paramD as Object] = anyArray;

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions