Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
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];
}

Playground Link

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Currently type of key if keyof ThemeMeta, which mean theme[key] can be any type of available types of the values of ThemeMeta. in our case is boolean or string[]. so type of keyof ThemeMeta[keyof ThemeMeta] is boolean | string

When you then apply it to any of specific properties of themeMeta you got an error, because neither boolean or string[] cannot accept value of type boolean | string

What you can do is:

const theme:Theme = defaultTheme;
let themeMeta:ThemeMeta = defaultThemeMeta;
let key: keyof ThemeMeta;
for(key in defaultThemeMeta) {
    themeMeta = {
        ...themeMeta,
        [key]: theme[key]
    }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!