Skip to content

Destructuring variable declarations with types #5034

Closed
@schungx

Description

@schungx

There doesn't seem to be a way to specify type information on destructuring variable declarations with types. For example:

function someFunc(data: any) {
    const { x:string, y:number, z:boolean } = data;
}

This will be interpreted, as per ES6 rules, as the field data.x being assigned to a variable named string and so on.

The alternative syntax discussed before in another issue:

function someFunc(data: any) {
    const { x::string, y::number, z::boolean } = data;
}

did not make it into the current version.

Currently, you need to use initializers to fudge the typing info:

function someFunc(data: any) {
    const { x="", y=0, z=false } = data;
}

but then it becomes weird if you want a different default value:

function someFunc(data: any) {
    const { x=null as string, y=null as number, z=null as boolean } = data;
}

Can we put back a way to declare the types of destructuring pieces? This is very useful especially when integrating with legacy code, where there is limited type information upstream.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions