I'm facing an issue with the code snippet below. I have two types, MyType
and MyTypeDto
. My goal is to clone an instance of MyType
into MyTypeDto
, but I encounter a compile error on the assignment line:
Type 'string | boolean | Date' is not assignable to type 'undefined'. Type 'string' is not assignable to type 'undefined'.
How can I resolve this error?
interface MyType {
str: string;
bool: boolean;
d: Date;
...
}
interface MyTypeDto {
str?: string;
bool?: boolean;
d?: Date;
name?: string;
...
}
const t: MyType = {
str: "",
bool: false,
d: new Date(),
};
const keys = Object.keys(t);
const dto: MyTypeDto = {};
keys.forEach((key: string) => {
dto[key as keyof MyTypeDto] = t[key as keyof MyType]; // encountering error here
});