0

I created a parent class in TypeScript, this class was then extended by the child.

class x {
  s: number;
  c: string;
  constructor(s, c) {
    this.s = s;
    this.c = c;
  }

}

class y extends x {
  g: string;
  constructor(s, c, g) {
    super(s, c);
    this.g = g;
  }
  display() {
    document.write("" + this.s + " " + this.c + " " + this.g);
  }
}
let bp = new y("f");
bp.display();

However, when I pass the string f to the child object, no error is caused, even though the first parameter in its constructor is of type number. Why is that?

Heretic Monkey
  • 10,498
  • 6
  • 45
  • 102
Marwa
  • 321
  • 1
  • 9
  • Type your parameters for your methods. – MyCah Sep 19 '19 at 20:32
  • 1
    You haven't defined the types of the `s` and `c` arguments in the constructor of `y`, so they default to `any`. Also, don't use `document.write`. Ever. – Heretic Monkey Sep 19 '19 at 20:32
  • @HereticMonkey Why shouldn't I use doucment.write? – Marwa Sep 19 '19 at 20:33
  • 1
    [Why is document.write considered a “bad practice”?](https://stackoverflow.com/q/802854/215552) – Heretic Monkey Sep 19 '19 at 20:34
  • @HereticMonkey Also i set the data type in the constructor and I am still not getting errors – Marwa Sep 19 '19 at 20:39
  • 3
    I see errors when I place [this code in the TypeScript Playground](https://www.typescriptlang.org/play/index.html#code/MYGwhgzhAEAe0G8BQ1oQFzQHYFcC2ARgKYBOA3CtMJhAC4kCWWA5hasAPZZ0k7C0cSACgy5CpADRV0PJswCUiSqloALBhAB0MALxo2qaGo2bg0PcAMBfJEhtJQkGAE9oRWLSJYAJjHjJUZhp6OQNObno+AWFRfGISKWpZFikg5IUlQzQcAAdSEUT5AxV1LWZzaFZKG1RvDRzwZyFFAMNvDmB8L1pNAHdGTyEAIiHoAGojUu1x6FHRieMtMwm5mcXNBWs7JBAiWmgCHIqsIl7oJqGAMyGipEPNOogGsCaioA)... – Heretic Monkey Sep 19 '19 at 20:41

0 Answers0