Reputation: 2031
I have a Typescript declaration file that looks like the following:
/// <reference types="node" />
declare namespace foo {
interface bar {
}
}
declare const foo: foo.bar;
export = foo;
This compiles just fine with TS 2.0/2.2. However, if the namespace contains any class
whatsoever - e.g. changing bar
to a class, adding another class bam
, etc. - Typescript throws an error TS2300: Duplicate identifier 'foo'.
, for the two declare lines. The goal of the code as written is to take advantage of Declaration Merging in Typescript, and when foo
contains only interface
s, the code works as expected (type
s seem fine to include in foo
, too). Why does declaration merging fail if foo
contains any class
es?
Upvotes: 1
Views: 1434
Reputation: 15589
This is because class
is concrete. namespace
behaves differently when it contains only types or it contains code.
When it contains code, it will also emit value. i.e. namespace x
would becomes var x
.
When it does not contain code, no code will be emitted.
That's why when it contains class, it will emit var foo
thus conflict with your const foo
.
https://www.typescriptlang.org/docs/handbook/declaration-merging.html
Upvotes: 2