Lin Du
Lin Du

Reputation: 102257

how to add type annotation to a destructed object literal with rest operator?

I destruct an object literal and use spread/rest operator.

How to add type annotation to the rest part?

I try to do this, but tsc throw an error.

const { x, y, ...rest }: {x: number, y: number, ...rest: any} = { x: 1, y: 2, z: 3, q: 4 };

Upvotes: 2

Views: 375

Answers (1)

Mμ.
Mμ.

Reputation: 8542

For the ...rest part you can do the following:

const { x, y, ...rest }: { x: number, y: number, [key: string]: number } = { x: 1, y: 2, z: 3, q: 4 };

It basically means that it accepts any other keys as a string with number as the value.

Upvotes: 1

Related Questions