Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
Class inheritance with common props - generic class annotations with TypeScript

I need help with following example. I have main class Component with props property and some child components. How should I correctly write annotations so that it is possible to inherit objects in this way - even at several levels? Is it even possible to do?

I cannot pass on the characteristics from the third and other class. The special property still refers to IButtonProps instead of ISpecialButtonProps.

Many thanks.

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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda