I hive 2 classes:
export interface Vec2I {
x: number;
y: number;
}
export class Vec2 implements Vec2I {
constructor(public x: number = 0, public y: number = 0) {}
public set(x: Vec2I): void;
public set(x: number, y: number): void;
public set(x: Vec2I | number, y?: number) {
if (typeof x === "number") {
this.x = x;
if (y !== undefined) {
this.y = y;
}
} else {
this.x = x.x;
this.y = x.y;
}
}
}
and
import { Vec2, Vec2I } from "./Vec2";
export interface Vec3I extends Vec2I {
z: number;
}
export class Vec3 extends Vec2 implements Vec3I {
constructor(x: number = 0, y: number = 0, public z: number = 0) {
super(x, y);
}
public set(x: Vec3I): void;
public set(x: number, y: number, z: number): void;
public set(x: Vec3I | number, y?: number, z?: number) {
super.set(x, y);
if (typeof x === "number") {
if (z !== undefined) {
this.z = z;
}
} else {
this.z = x.z;
}
}
}
and the error I see when I try to overload a method in inherited class is:
Property 'set' in type 'Vec3' is not assignable to the same property in base type 'Vec2'.
Type '{ (x: Vec3I): void; (x: number, y: number, z: number): void; }' is not assignable to type '{ (x: Vec2I): void; (x: number, y: number): void; }'.
Types of parameters 'x' and 'x' are incompatible.
Type 'number' is not assignable to type 'Vec3I'.
I don't understand the problem. Vec3I extends from Vec2I, so in should be ok to substitute in method parameters. If I comment overloading, everything works:

