Typescript: Destructure of function parameters - Refactoring

Created on 9 Jun 2018  路  7Comments  路  Source: microsoft/TypeScript

const calllback = event=> {
   let {target} = event;
   // code with used target
}

to

const calllback = ({target})=> {
   // code with used target
}

Same in the opposite direction and with a more complex and deep destructure and function content.

Refactorings Needs Proposal Suggestion

Most helpful comment

@Shinigami92 Of course (May optionally be const {target} = event).
Sorry for the mistake.

All 7 comments

But shouldn't it be event.target?

const calllback = event => {
   const target = event.target;
   // code with used target
}

to

const calllback = ({target}) => {
   // code with used target
}

@Shinigami92 Of course (May optionally be const {target} = event).
Sorry for the mistake.

There's a big difference between your two examples though. In the first one target is a constant, but not in the second one. Destructured variables in function parameters can be reassigned.

@jcready Oh, I do not know why it seemed to me that it was different.
This somewhat limits this proposed refactoring function. : |

Actually, you can do it somehow? Or are there any standard plans for this?

Hi folks.
There's a similar refactor https://github.com/microsoft/TypeScript/pull/39832 that do not convert at the original position but append new line below

@Kingwl What about function parameter destructure? And it looks like it works in one direction.

Well, It's good to take a try.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Zlatkovsky picture Zlatkovsky  路  3Comments

siddjain picture siddjain  路  3Comments

kyasbal-1994 picture kyasbal-1994  路  3Comments

dlaberge picture dlaberge  路  3Comments

manekinekko picture manekinekko  路  3Comments