'Add typing to rest operator

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



Solution 1:[1]

You could define an intersection type:

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

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

Playground

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Taxel