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

112
Views
TypeScript copia las propiedades del objeto para crear un nuevo objeto

Quiero crear un nuevo objeto de tipo 'B' a partir del objeto de tipo 'A' que se extiende a 'B'. He intentado copiar propiedades de tipo 'B' del objeto de tipo 'A' y me da error.

¿Podría ayudarme a resolver este error o explicarme qué hago mal? Aquí está mi ejemplo de código:

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

Enlace del patio de recreo

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

0

Actualmente, el tipo de key es keyof ThemeMeta , lo que significa que theme[key] puede ser cualquier tipo de tipo disponible de los valores de ThemeMeta . en nuestro caso es boolean o string[] . entonces el tipo de keyof ThemeMeta[keyof ThemeMeta] es boolean | string

Cuando lo aplica a cualquiera de las propiedades específicas de themeMeta, obtiene un error, porque ni boolean ni string[] no pueden aceptar el valor de tipo boolean | string

Lo que puedes hacer es:

 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!