46

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 referencing the artist.name property in the super call as super(name) but is not working.

Any ideas and explanations you may have will be greatly appreciated. Thanks - Alex.

class Artist {
  constructor(
    public name: string,
    public age: number,
    public style: string,
    public location: string
  ){
    console.log(`instantiated ${name}, whom is ${age} old, from ${location}, and heavily regarded in the ${style} community`);
  }
}

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

interface Human {
  name: string,
  age: number
}

function getArtist(artist: Human){
  console.log(artist.name)
}

let Banksy = new Artist(
  "Banksy",
   40,
  "Politcal Graffitti",
  "England / Wolrd"
)

getArtist(Banksy);
0

1 Answer 1

68

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}`);
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

If you add pubic to each of constructors arguments this argument will be assigned in child as well as parent
I did intend to populate the base class with art: Artist. The second solution worked seamlessly. Thank you very much.
Happy to be able to help. @mortezaT you are right it meant to have the first four arguments without public. I don't know what will happen if you cast StreetArtist to Artist and access name for example, will they be the same? It hides base property right?
Worth noting on second solution. You are basically creating two artist objects with same information. Coming from c# world it seems a bit off for me, but it works :) For typescript maybe I would have used an interface for the art argument instead and use an object initializer as { name: 'aoue', ... } that follows the interface.
@CodyBugsteina as far i know it is. Looks like there is a petition to make it implicit more or less like the Java Specification. github.com/Microsoft/TypeScript/issues/5910
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.