@@ -14,14 +14,14 @@ $('#foo');
1414jQuery (' #foo' );
1515```
1616
17- 但是在 ts 中,编译器并不知道 ` $ ` 或 ` jQuery ` 是什么东西[ <sup >` 1 ` </sup >] ( ../ examples/declaration-files/1 -jquery) :
17+ 但是在 ts 中,编译器并不知道 ` $ ` 或 ` jQuery ` 是什么东西[ <sup >1 </sup >] ( https://github.com/xcatliu/typescript-tutorial/tree/master/ examples/declaration-files/01 -jquery) :
1818
1919``` ts
2020jQuery (' #foo' );
2121// ERROR: Cannot find name 'jQuery'.
2222```
2323
24- 这时,我们需要使用 ` declare var ` 来定义它的类型:
24+ 这时,我们需要使用 ` declare var ` 来定义它的类型[ < sup >2</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/02-declare-var ) :
2525
2626``` ts
2727declare var jQuery: (selector : string ) => any ;
@@ -39,7 +39,7 @@ jQuery('#foo');
3939
4040## 什么是声明文件
4141
42- 通常我们会把声明语句放到一个单独的文件(` jQuery.d.ts ` )中,这就是声明文件:
42+ 通常我们会把声明语句放到一个单独的文件(` jQuery.d.ts ` )中,这就是声明文件[ < sup >3</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/03-jquery-d-ts ) :
4343
4444``` ts
4545// src/jQuery.d.ts
@@ -121,7 +121,7 @@ npm install @types/jquery --save-dev
121121
122122##### ` declare var `
123123
124- 在所有的声明语句中,` declare var ` 是最简单的,如之前所学,它能够用来定义一个全局变量的类型。与其类似的,还有 ` declare let ` 和 ` declare const ` ,使用 ` let ` 与使用 ` var ` 没有什么区别,而使用 ` const ` 定义时,表示此时的全局变量是一个常量,不允许再去修改它的值了:
124+ 在所有的声明语句中,` declare var ` 是最简单的,如之前所学,它能够用来定义一个全局变量的类型。与其类似的,还有 ` declare let ` 和 ` declare const ` ,使用 ` let ` 与使用 ` var ` 没有什么区别,而使用 ` const ` 定义时,表示此时的全局变量是一个常量,不允许再去修改它的值了[ < sup >4</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/04-declare-const-jquery ) :
125125
126126``` ts
127127declare let jQuery: (selector : string ) => any ;
@@ -146,7 +146,7 @@ jQuery = function(selector) {
146146
147147一般来说,全局变量都是禁止修改的常量,所以大部分情况都应该使用 ` const ` 而不是 ` var ` 或 ` let ` 。
148148
149- 需要注意的是,声明语句中只能定义类型,切勿在声明语句中定义具体的值:
149+ 需要注意的是,声明语句中只能定义类型,切勿在声明语句中定义具体的值[ < sup >5</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/05-declare-jquery-value ) :
150150
151151``` ts
152152declare const jQuery = function (selector ) {
@@ -165,7 +165,7 @@ declare function jQuery(selector: string): any;
165165jQuery (' #foo' );
166166```
167167
168- 在函数类型的声明语句中,函数重载也是支持的:
168+ 在函数类型的声明语句中,函数重载也是支持的[ < sup >6</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/06-declare-function ) :
169169
170170``` ts
171171declare function jQuery(selector : string ): any ;
@@ -179,7 +179,7 @@ jQuery(function() {
179179
180180#### ` declare class `
181181
182- 当全局变量是一个类的时候,我们用 ` declare class ` 来定义它的类型:
182+ 当全局变量是一个类的时候,我们用 ` declare class ` 来定义它的类型[ < sup >7</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/07-declare-class ) :
183183
184184``` ts
185185declare class Animal {
@@ -206,7 +206,7 @@ declare class Animal {
206206
207207#### ` declare enum `
208208
209- 使用 ` declare enum ` 定义的枚举类型也称作外部枚举(Ambient Enums),举例如下:
209+ 使用 ` declare enum ` 定义的枚举类型也称作外部枚举(Ambient Enums),举例如下[ < sup >8</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/08-declare-enum ) :
210210
211211``` ts
212212declare enum Directions {
@@ -247,7 +247,7 @@ declare namespace jQuery {
247247jQuery .ajax (' /api/get_something' );
248248```
249249
250- 注意,在 ` declare namespace ` 内部,我们直接使用 ` function ajax ` 来声明函数,而不是使用 ` declare function ajax ` 。类似的,也可以使用 ` const ` , ` class ` , ` enum ` 等语句:
250+ 注意,在 ` declare namespace ` 内部,我们直接使用 ` function ajax ` 来声明函数,而不是使用 ` declare function ajax ` 。类似的,也可以使用 ` const ` , ` class ` , ` enum ` 等语句[ < sup >9</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/09-declare-namespace ) :
251251
252252``` ts
253253declare namespace jQuery {
@@ -278,7 +278,7 @@ e.blur(jQuery.EventType.CustomClick);
278278
279279##### 嵌套的命名空间
280280
281- 如果对象拥有深层的层级,则需要用嵌套的 ` namespace ` 来声明深层的属性的类型:
281+ 如果对象拥有深层的层级,则需要用嵌套的 ` namespace ` 来声明深层的属性的类型[ < sup >10</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/10-declare-namespace-nesting ) :
282282
283283``` ts
284284declare namespace jQuery {
@@ -298,7 +298,7 @@ jQuery.fn.extend({
298298});
299299```
300300
301- 假如 ` jQuery ` 下仅有 ` fn ` 这一个属性(没有 ` ajax ` 等其他属性或方法),则可以不需要嵌套 ` namespace ` :
301+ 假如 ` jQuery ` 下仅有 ` fn ` 这一个属性(没有 ` ajax ` 等其他属性或方法),则可以不需要嵌套 ` namespace ` [ < sup >11</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/11-declare-namespace-dot ) :
302302
303303``` ts
304304declare namespace jQuery .fn {
@@ -316,7 +316,7 @@ jQuery.fn.extend({
316316
317317#### ` interface ` 和 ` type `
318318
319- 除了全局变量之外,可能有一些类型我们也希望能暴露出来。在类型声明文件中,我们可以直接使用 ` interface ` 或 ` type ` 来声明一个全局的类型:
319+ 除了全局变量之外,可能有一些类型我们也希望能暴露出来。在类型声明文件中,我们可以直接使用 ` interface ` 或 ` type ` 来声明一个全局的类型[ < sup >12</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/12-interface ) :
320320
321321``` ts
322322// src/jQuery.d.ts
@@ -348,7 +348,7 @@ jQuery.ajax('/api/post_something', settings);
348348
349349##### 防止命名冲突
350350
351- 暴露在最外层的 ` interface ` 或 ` type ` 会作为全局类型作用于整个项目中,我们应该尽可能的减少全局变量或全局类型的数量。故应该将他们放到 ` namespace ` 下:
351+ 暴露在最外层的 ` interface ` 或 ` type ` 会作为全局类型作用于整个项目中,我们应该尽可能的减少全局变量或全局类型的数量。故应该将他们放到 ` namespace ` 下[ < sup >13</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/13-avoid-name-conflict ) :
352352
353353``` ts
354354// src/jQuery.d.ts
@@ -378,7 +378,7 @@ jQuery.ajax('/api/post_something', settings);
378378
379379#### 声明合并
380380
381- 假如 jQuery 既是一个函数,可以直接被调用 ` jQuery('#foo') ` ,又是一个对象,拥有子属性 ` jQuery.ajax() ` (事实确实如此),那么我们可以组合多个声明语句,它们会不冲突的合并起来:
381+ 假如 jQuery 既是一个函数,可以直接被调用 ` jQuery('#foo') ` ,又是一个对象,拥有子属性 ` jQuery.ajax() ` (事实确实如此),那么我们可以组合多个声明语句,它们会不冲突的合并起来[ < sup >14</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/14-declaration-merging ) :
382382
383383``` ts
384384declare function jQuery(selector : string ): any ;
@@ -443,7 +443,7 @@ jQuery.ajax('/api/get_something');
443443
444444npm 包的声明文件与全局变量的声明文件有很大区别。在 npm 包的声明文件中,使用 ` declare ` 不再会声明一个全局变量,而只会在当前文件中声明一个局部变量。只有在声明文件中使用 ` export ` 导出,然后在使用方 ` import ` 导入后,才会应用到这些类型声明。
445445
446- ` export ` 的语法与普通的 ts 中的语法类似,区别仅在于声明文件中禁止定义具体的值:
446+ ` export ` 的语法与普通的 ts 中的语法类似,区别仅在于声明文件中禁止定义具体的值[ < sup >15</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/15-export ) :
447447
448448``` ts
449449// types/foo/index.d.ts
@@ -485,7 +485,7 @@ let options: Options = {
485485
486486##### 混用 ` declare ` 和 ` export `
487487
488- 我们也可以使用 ` declare ` 先声明多个变量,最后再用 ` export ` 一次性导出。上例的声明文件可以等价的改写为:
488+ 我们也可以使用 ` declare ` 先声明多个变量,最后再用 ` export ` 一次性导出。上例的声明文件可以等价的改写为[ < sup >16</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/16-declare-and-export ) :
489489
490490``` ts
491491// types/foo/index.d.ts
@@ -513,7 +513,7 @@ export { name, getName, Animal, Directions, Options };
513513
514514#### ` export namespace `
515515
516- 与 ` declare namespace ` 类似,` export namespace ` 用来导出一个拥有子属性的对象:
516+ 与 ` declare namespace ` 类似,` export namespace ` 用来导出一个拥有子属性的对象[ < sup >17</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/17-export-namespace ) :
517517
518518``` ts
519519// types/foo/index.d.ts
@@ -539,7 +539,7 @@ foo.bar.baz();
539539
540540在 ES6 模块系统中,使用 ` export default ` 可以导出一个默认值,使用方可以用 ` import foo from 'foo' ` 而不是 ` import { foo } from 'foo' ` 来导入这个默认值。
541541
542- 在类型声明文件中,` export default ` 用来导出默认值的类型:
542+ 在类型声明文件中,` export default ` 用来导出默认值的类型[ < sup >18</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/18-export-default ) :
543543
544544``` ts
545545// types/foo/index.d.ts
@@ -555,7 +555,7 @@ import foo from 'foo';
555555foo ();
556556```
557557
558- 注意,只有 ` function ` 、` class ` 和 ` interface ` 可以直接默认导出,其他的变量需要先定义出来,再默认导出:
558+ 注意,只有 ` function ` 、` class ` 和 ` interface ` 可以直接默认导出,其他的变量需要先定义出来,再默认导出[ < sup >19</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/19-export-default-enum-error ) :
559559
560560``` ts
561561// types/foo/index.d.ts
@@ -584,7 +584,7 @@ declare enum Directions {
584584export default Directions ;
585585```
586586
587- 针对这种默认导出,我们一般会将导出语句放在整个声明文件的最前面:
587+ 针对这种默认导出,我们一般会将导出语句放在整个声明文件的最前面[ < sup >20</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/20-export-default-enum ) :
588588
589589``` ts
590590// types/foo/index.d.ts
@@ -637,7 +637,7 @@ import foo = require('foo');
637637import bar = foo .bar ;
638638```
639639
640- 对于这种使用 commonjs 规范的库,假如要为它写类型声明文件的话,就需要使用到 ` export = ` 这种语法了:
640+ 对于这种使用 commonjs 规范的库,假如要为它写类型声明文件的话,就需要使用到 ` export = ` 这种语法了[ < sup >21</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/21-export-equal ) :
641641
642642``` ts
643643// types/foo/index.d.ts
@@ -662,7 +662,7 @@ declare namespace foo {
662662
663663#### ` export as namespace `
664664
665- 一般使用 ` export as namespace ` 时,都是先有了 npm 包的声明文件,再基于它添加一条 ` export as namespace ` 语句,即可将声明好的一个变量声明为全局变量,举例如下:
665+ 一般使用 ` export as namespace ` 时,都是先有了 npm 包的声明文件,再基于它添加一条 ` export as namespace ` 语句,即可将声明好的一个变量声明为全局变量,举例如下[ < sup >22</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/22-export-as-namespace ) :
666666
667667``` ts
668668// types/foo/index.d.ts
@@ -692,7 +692,7 @@ declare namespace foo {
692692
693693### 直接扩展全局变量
694694
695- 有的第三方库扩展了一个全局变量,可是此全局变量的类型却没有相应的更新过来,就会导致 ts 编译错误,此时就需要扩展全局变量的类型。比如扩展 ` String ` 类型:
695+ 有的第三方库扩展了一个全局变量,可是此全局变量的类型却没有相应的更新过来,就会导致 ts 编译错误,此时就需要扩展全局变量的类型。比如扩展 ` String ` 类型[ < sup >23</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/23-merge-global-interface ) :
696696
697697``` ts
698698interface String {
@@ -704,7 +704,7 @@ interface String {
704704
705705通过声明合并,使用 ` interface String ` 即可给 ` String ` 添加属性或方法。
706706
707- 也可以使用 ` declare namespace ` 给已有的命名空间添加类型声明:
707+ 也可以使用 ` declare namespace ` 给已有的命名空间添加类型声明[ < sup >24</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/24-merge-global-namespace ) :
708708
709709``` ts
710710// types/jquery-plugin/index.d.ts
@@ -734,7 +734,7 @@ jQuery.foo({
734734
735735#### ` declare global `
736736
737- 使用 ` declare global ` 可以在 npm 包或者 UMD 库的声明文件中扩展全局变量的类型:
737+ 使用 ` declare global ` 可以在 npm 包或者 UMD 库的声明文件中扩展全局变量的类型[ < sup >25</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/25-declare-global ) :
738738
739739``` ts
740740// types/foo/index.d.ts
@@ -762,7 +762,7 @@ export {};
762762
763763#### ` declare module `
764764
765- 如果是需要扩展原有模块的话,需要在类型声明文件中先引用原有模块,再使用 ` declare module ` 扩展原有模块:
765+ 如果是需要扩展原有模块的话,需要在类型声明文件中先引用原有模块,再使用 ` declare module ` 扩展原有模块[ < sup >26</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/26-declare-module ) :
766766
767767``` ts
768768// types/moment-plugin/index.d.ts
@@ -783,7 +783,7 @@ import 'moment-plugin';
783783moment .foo ();
784784```
785785
786- ` declare module ` 也可用于在一个文件中一次性声明多个模块的类型:
786+ ` declare module ` 也可用于在一个文件中一次性声明多个模块的类型[ < sup >27</ sup > ] ( https://github.com/xcatliu/typescript-tutorial/tree/master/examples/declaration-files/27-multiple-declare-module ) :
787787
788788``` ts
789789// types/foo-bar.d.ts
0 commit comments