Using TypeScript super()

前端 未结 1 654
不知归路
不知归路 2020-12-30 23:17

I am trying to extend a class in TypeScript. I keep receiving this error on compile: \'Supplied parameters do not match any signature of call target.\' I have tried referenc

相关标签:
1条回答
  • 2020-12-31 00:01

    The super call must supply all parameters for base class. The constructor is not inherited. Commented out artist because I guess it is not needed when doing like this.

    class StreetArtist extends Artist {
      constructor(
        name: string,
        age: number,
        style: string,
        location: string,
        public medium: string,
        public famous: boolean,
        public arrested: boolean,
        /*public art: Artist*/
      ){
        super(name, age, style, location);
        console.log(`instantiated ${this.name}. Are they famous? ${famous}. Are they locked up? ${arrested}`);
      }
    }
    

    Or if you intended the art parameter to populate base properties, but in that case I guess there isn't really a need for using public on art parameter as the properties would be inherited and it would only store duplicate data.

    class StreetArtist extends Artist {
      constructor(
        public medium: string,
        public famous: boolean,
        public arrested: boolean,
        /*public */art: Artist
      ){
        super(art.name, art.age, art.style, art.location);
        console.log(`instantiated ${this.name}. Are they famous? ${famous}. Are they locked up? ${arrested}`);
      }
    }
    
    0 讨论(0)
提交回复
热议问题