@@ -48,21 +48,21 @@ The compiler will try to find a `.ts`, `.tsx`, and then a `.d.ts` with the appro
4848If a specific file could not be found, then the compiler will look for an * ambient module declaration* .
4949Recall that these need to be declared in a ` .d.ts ` file.
5050
51- * ` myModules.d.ts `
51+ ##### myModules.d.ts
5252
53- ``` ts
54- // In a .d.ts file or .ts file that is not a module:
55- declare module " SomeModule" {
56- export function fn(): string ;
57- }
58- ```
53+ ``` ts
54+ // In a .d.ts file or .ts file that is not a module:
55+ declare module " SomeModule" {
56+ export function fn(): string ;
57+ }
58+ ```
5959
60- * ` myOtherModule.ts `
60+ ##### myOtherModule.ts
6161
62- ` ` ` ts
63- /// <reference path="myModules.d.ts" />
64- import * as m from "SomeModule";
65- ` ` `
62+ ``` ts
63+ /// <reference path = " myModules.d.ts" />
64+ import * as m from " SomeModule" ;
65+ ```
6666
6767The reference tag here allows us to locate the declaration file that contains the declaration for the ambient module.
6868This is how the ` node.d.ts ` file that several of the TypeScript samples use is consumed.
@@ -71,24 +71,24 @@ This is how the `node.d.ts` file that several of the TypeScript samples use is c
7171
7272If you're converting a program from namespaces to modules, it can be easy to end up with a file that looks like this:
7373
74- * ` shapes.ts `
74+ ##### shapes.ts
7575
76- ` ` ` ts
77- export namespace Shapes {
78- export class Triangle { /* ... */ }
79- export class Square { /* ... */ }
80- }
81- ` ` `
76+ ``` ts
77+ export namespace Shapes {
78+ export class Triangle { /* ... */ }
79+ export class Square { /* ... */ }
80+ }
81+ ```
8282
8383The top-level module here ` Shapes ` wraps up ` Triangle ` and ` Square ` for no reason.
8484This is confusing and annoying for consumers of your module:
8585
86- * ` shapeConsumer.ts `
86+ ##### shapeConsumer.ts
8787
88- ` ` ` ts
89- import * as shapes from "./shapes";
90- let t = new shapes.Shapes.Triangle(); // shapes.Shapes?
91- ` ` `
88+ ``` ts
89+ import * as shapes from " ./shapes" ;
90+ let t = new shapes .Shapes .Triangle (); // shapes.Shapes?
91+ ```
9292
9393A key feature of modules in TypeScript is that two different modules will never contribute names to the same scope.
9494Because the consumer of a module decides what name to assign it, there's no need to proactively wrap up the exported symbols in a namespace.
@@ -98,19 +98,19 @@ Because the module file itself is already a logical grouping, and its top-level
9898
9999Here's a revised example:
100100
101- * ` shapes.ts `
101+ ##### shapes.ts
102102
103- ` ` ` ts
104- export class Triangle { /* ... */ }
105- export class Square { /* ... */ }
106- ` ` `
103+ ``` ts
104+ export class Triangle { /* ... */ }
105+ export class Square { /* ... */ }
106+ ```
107107
108- * ` shapeConsumer.ts `
108+ ##### shapeConsumer.ts
109109
110- ` ` ` ts
111- import * as shapes from "./shapes";
112- let t = new shapes.Triangle();
113- ` ` `
110+ ``` ts
111+ import * as shapes from " ./shapes" ;
112+ let t = new shapes .Triangle ();
113+ ```
114114
115115## Trade-offs of Modules
116116
0 commit comments