Interfaces of the same name within a module will be merged:
interface Foo {
x: number;
}
interface Foo {
y: string;
}
let g = {} as Foo;
g.x; // OK
g.y; // OK
A class declaration creates both a constructor function as well as type declaration, which essentially means that all classes can be used as interfaces.
class Bar {
y: number;
}
interface IBaz extends Bar { } // includes y: number
class CBaz implements Bar {
y: number = 5;
}
Therefore, having a class and an interface with the same name is equivalent to having two interfaces with the same name, and you will get merge conflicts if both instances of the interface re-declare the same members with different types.
Strangely enough, Typescript will allow for this:
export interface Foo {
readonly x: number;
}
export class Foo {
readonly x: number = 3;
}
but it won't allow get x() { return 3; }
even though both of those generate as readonly x: number
, so I can only imagine that the type checker considers them as different during merging even though they are semantically the same (this is why you can extend the interface and specify the readonly property as a getter function).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…