Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using TypeScript super()

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); 
like image 428
alex bennett Avatar asked Jun 22 '16 02:06

alex bennett


People also ask

What does super () do?

Definition and Usage The super() function is used to give access to methods and properties of a parent or sibling class. The super() function returns an object that represents the parent class.

What is super () in angular?

super is ES6 syntax that cannot be used outside the method where it's used. Given there is Foo class that extends Bar , super keyword is interpreted as Bar in Foo constructor and static methods and as Bar.

What does super () do in JS?

The super keyword in JavaScript acts as a reference variable to the parent class. It is mainly used when we want to access a variable, method, or constructor in the base class from the derived class.

How do you call a base class constructor in TypeScript?

You can call the base class constructor from the child class by using the super() which will execute the constructor of the base class.


1 Answers

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}`);   } } 
like image 114
mollwe Avatar answered Oct 09 '22 02:10

mollwe