Add typing to rest operator

Viewed 44

Take this example with a rest operator :

type Foo = { foo1: string, foo2: number };

const { yeah, ...foo }: { yeah: string, [k: string]: any } = { yeah: '', foo1: '', foo2: 0 };

How can I specify the typing of my foo object better than with {[k: string]: any} and using my Foo type ?

Playground

1 Answers

You could define an intersection type:

type Foo = { foo1: string, foo2: number };

const { yeah, ...foo }: { yeah: string} & Foo = { yeah: '', foo1: '', foo2: 0 };

Playground

Related