typescripttscdestructuringredeclaration

Ignore multiple results of a destructuring in typescript


I'm using typescript destructuring as follows:

const props = new Map<User, [Name, Age, Location, Gender]>();
props.set(bill, [n, a, l, g]);

// ...

// Want to access location and gender of bill.
const [n, a, l, g] = props.get(bill);
console.log(l + g);

But this violates the noUnusedLocals compiler option, so what I really want is:

const [_, _, l, g] = props.get(bill);

but this violates a redeclaration of a block-scope variable (two variables named _).

What's the best way to deal with this? Perhaps destructuring is just the wrong choice here.


Solution

  • According to the ES6 documentation you can do it this way:

    const [, , l, g] = props.get(bill);
    

    You can find a minimal working example here.