As I understand typescript the "code" in the example does not generate nor change any output javascript. It is all declarations that will help intellisense and the typescript compiler find errors, but does nothing to alter the code. The declarations that the example gives are intended for an "ambient" declaration file, i.e. a declaration file that provides typescript definitions for ordinary javascript library so that typescript and intellisense can work with them as if they were typescript files instead of plain javascript. I believe that at a first approximation typescript treats anything it finds of which it has no declaration knowledges as an "any" type.
For the library mentioned you would be better off to use the existing type library (npm install --save-dev @types/d3
). I have found that after installing the library (npm install --save d3
) and the @types/d3
, using import * as d3 from 'd3'
in a .ts file (in angular 5; other situations may other steps) will bring the object and type information in, and everything works satisfactorily. If you open up the types file you will see a completely different set of declarations from the ones given in the typescript documentation example, but I believe they are still all declarations, i.e. no code.
In reply to edits
The file in the example is not a primary typescript file. It is a declarations file describing the "typescript shape" of an ordinary javascript library, i.e. how the typescript compiler and intellisense should pretend the ordinary javascript would look like if it were written in typescript. While you may think the choice of the name D3 was arbitrary, it was not exactly. The .d.ts
file is trying to provide typescript meta information for a library (the d3 library: https://www.npmjs.com/package/d3) which does not itself provide that information (although, as mentioned, the typescript meta information for that particular library is available as an @types package).
If you were writing your own library code and wanted to work inside a namespace as per the validation example, you would use namespace D3
rather than declare namespace D3
, but you would also be writing a .ts
file, not a .d.ts
file. As the section title of the documentation says, that example is working on ambient namespaces, i.e. namespaces for ordinary javascript code the writer of the file does not (typically) provide.
In summary, the goals of the two halves of the documentation are different. In the first half the "validation" code is showing you how to use namespaces within your own typescript code. In the second half, the "d3" code is showing you how you can provide typescript metadata for code that is plain javascript, not typescript.