Typescript - Declaration Merging for Namespace with Classes -


i have typescript declaration file looks following:

/// <reference types="node" />  declare namespace foo {     interface bar {      } }  declare const foo: foo.bar; export = foo; 

this compiles fine ts 2.0/2.2. however, if namespace contains class whatsoever - e.g. changing bar class, adding class bam, etc. - typescript throws error ts2300: duplicate identifier 'foo'., 2 declare lines. goal of code written take advantage of declaration merging in typescript, , when foo contains interfaces, code works expected (types seem fine include in foo, too). why declaration merging fail if foo contains classes?

this because class concrete. namespace behaves differently when contains types or contains code.

when contains code, emit value. i.e. namespace x becomes var x.

when not contain code, no code emitted.

that's why when contains class, emit var foo conflict const foo.

https://www.typescriptlang.org/docs/handbook/declaration-merging.html

here demo: http://www.typescriptlang.org/play/index.html#src=namespace%20foo%20%7b%0d%0a%20%20interface%20x%20%7b%20%7d%0d%0a%7d%0d%0a%0d%0anamespace%20boo%20%7b%0d%0a%20%20class%20y%20%7b%20%7d%0d%0a%7d


Comments