I have below code. I have two types one is MyType the other is MyTypeDto. And I'd like to clone the instance of MyType to MyTypeDto but has 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 solve the issue here?
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]; // error here
});