Answer the question
In order to leave comments, you need to log in
How to merge an interface with a class if they are in different files?
if they are in the same file, then there is no problem
// a.ts
interface A {
f(m: 'string'): string;
f(m: 'number'): number;
}
class A {
f(m: string): any {
// ...
}
}
const a = new A();
a.f('string'); // returns string
a.f('number'); // returns number
// a.ts
interface A {
f(m: 'string'): string;
f(m: 'number'): number;
}
export default A;
// b.ts
import A from './a'; // Import declaration conflicts with local declaration of 'A'.
class A {
f(m: string): any {
// ...
}
}
class B implements A {
f(m: string): any {
// ...
}
}
const a: A = new B();
a.f('string'); // returns string
a.f('number'); // returns number
Answer the question
In order to leave comments, you need to log in
Take a look at this example, implements
don't be fooled by the presence
https://www.typescriptlang.org/play/#code/JYOwLgpg
... type by the previous type merging their definitions. This is valid if the types are declared in the same module. And when you import from another module, this is an error.
this is true anyway, the variable gets the class by the name of the constructor. It's just that in the first case, interface A disappeared, only the class remained.
Read on the topic https://www.typescriptlang.org/docs/handbook/decla...
In general, in order to avoid confusion, you should not do this in live code, as you wrote above, make different names. And in ts there is no special need for this at all, implements is usually used to remember to implement the necessary methods.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question