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

226
Views
Herencia de clase con accesorios comunes: anotaciones de clase genéricas con TypeScript

Necesito ayuda con el siguiente ejemplo. Tengo un componente de clase principal con propiedad de accesorios y algunos componentes secundarios. ¿Cómo debo escribir correctamente las anotaciones para que sea posible heredar objetos de esta manera, incluso en varios niveles? ¿Es posible hacerlo?

No puedo transmitir las características de la tercera y otra clase. La propiedad special aún hace referencia a IButtonProps en lugar de ISpecialButtonProps .

Muchas gracias.

 interface IProps { parent?: any } class Component<T1> { protected props: T1; constructor(props: T1) { this.props = props; } } interface IButtonProps extends IProps { caption?: string } class Button<T1> extends Component<IButtonProps> { constructor(props: T1) { super(props); // props.parent = 'x'; // props.caption = 'y'; } test(): void { console.log(this.props.parent); console.log(this.props.caption); } } interface ISpecialButtonProps extends IButtonProps { special?: string } class SpecialButton<T2> extends Button<ISpecialButtonProps> { constructor(props: T2) { super(props); // this.props.parent = 'a'; // this.props.caption = 'b'; // this.props.special = 'c'; } test(): void { console.log(this.props.caption); console.log(this.props.special); // Property 'special' does not exist on type 'IButtonProps' } } let button1 = new SpecialButton({caption: 'x', special: 'y'}); button1.test();
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

interface IProps { parent?: any; } class Component<T1> { protected props: T1; constructor(props: T1) { this.props = props; } } interface IButtonProps extends IProps { caption?: string; } class Button<T1> extends Component<IButtonProps & T1> { // diff constructor(props: T1) { super(props); // props.parent = 'x'; // props.caption = 'y'; } test(): void { console.log(this.props.parent); console.log(this.props.caption); } } interface ISpecialButtonProps extends IButtonProps { special?: string; } class SpecialButton<T2> extends Button<ISpecialButtonProps & T2> { // diff constructor(props: T2) { super(props); // this.props.parent = 'a'; // this.props.caption = 'b'; // this.props.special = 'c'; } test(): void { console.log(this.props.caption); console.log(this.props.special); // Property 'special' does not exist on type 'IButtonProps' } } const button1 = new SpecialButton({ caption: "x", special: "y" }); button1.test();
about 4 years ago · Santiago Gelvez 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!