Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Why instances are compatible even if their class constructors are not compatible?

$
0
0

In the following example, the constructors are not compatible but their instances are compatible. Why?

class Person {  name: string;  age: number;  constructor(name: string, age: number) {    this.name = name;    this.age = age;  }}class Employee {  name: string;  age: number;  salary: number;  constructor(name: string, age: number, salary: number) {    this.name = name;    this.age = age;    this.salary = salary;  }}const pCtor: typeof Person = Employee; // error, constructors are not compatibleconst p: Person = new Employee("", 0, 0); // but the instances *are* compatible, how is that possible?

Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>