I am facing an issue with destructing parameters and creating a new object of the same type in Typescript.
The following code functions properly:
function customFunc<T extends { attribute: string }>(parameter: T): T {
const { ...rest } = parameter;
return { ...rest };
}
However, this code does not work as expected:
function customFunc<T extends { attribute: string }>(parameter: T): T {
const { attribute, ...rest } = parameter;
return { attribute, ...rest };
}
An error message is displayed:
Type '{ attribute: string; } & Pick<T, Exclude<keyof T, "attribute">>' is not assignable to type 'T'
I would appreciate it if someone could explain what is causing the issue here.