The interpreter mentions that the humanProps
is expected to be of type {humanProps: IHumanProps}
.
How can I properly set the type for the spread operation so that humanPros
has the correct type IHumanProps
?
Here's an example:
interface IName {
name: string
}
interface IHumanProps {
hobby: string,
age: number
}
interface IHuman extends IName, IHumanProps {}
const human: IHuman = {
name: 'Linda',
hobby: 'reading',
age: 99
}
const { name, ...humanProps }: { name: string, humanProps: IHumanProps } = human;
console.log(name);
console.log(humanProps);
How can I destructure an object using the spread operator and specify types? I couldn't find much information on this particular scenario online.
[Edit]:
I encountered an error where VSCode (or possibly the linter) was indicating that the deconstructed humanProps
did not match the expected type IHumanProps
, instead suggesting a type of
{humanProps: {humanProps: IHumanProps}}
, which was incorrect. This might have been an issue with VSCode/ESLint.
Thank you all for the quick responses!