TypeScript-Handbook
TypeScript-Handbook copied to clipboard
Explain where declarations must exist in order for declaration merging to occur
The declaration merging page describes a system that I would expect to work like this:
// node_modules/@types/package/index.d.ts
interface Foo {
example(): void;
}
// main.ts
Foo.prototype.myNewMethod = () => console.log('hi!');
(new Foo()).example();
interface Foo {
myNewMethod(): void;
}
However, this will not compile (or at least it didn't for me with TS 2.5.3) - the compiler will complain that Property 'example' does not exist on type 'Foo'.
In order for the type merging to work, the interface definition in main.ts
needs to be moved into a type declaration file (e.g. main.d.ts
).
I would open a PR for this but I am not sure if it also applies to the other ways that types can be merged (e.g. namespaces, classes, etc.) or even if this is intended behavior.