'TypeScript copy object's properties to create a new object
I want to create a new object of type 'B' from the object of type 'A' that extends 'B'. I've tried to copy properties of type 'B' from the object with type 'A' and that gives me an error.
Could you please help me to resolve this error or explain what I do wrong. Here is my code example:
interface ThemeMeta {
available: boolean;
tags: string[];
}
const defaultThemeMeta: ThemeMeta = {
available: false,
tags: []
};
interface Theme extends ThemeMeta {
name: string;
domainName: string;
lastUpdate: number;
published: boolean;
developer: boolean;
pinned: boolean;
id: number;
}
const defaultTheme:Theme = {
name: 'Default theme name',
domainName: '',
lastUpdate: 0,
published: false,
developer: false,
pinned: false,
id: 0,
available: false,
tags:[]
};
const theme:Theme = defaultTheme,
themeMeta:ThemeMeta = defaultThemeMeta;
let key: keyof ThemeMeta;
for(key in defaultThemeMeta) {
themeMeta[key] = theme[key];
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
