我什么时候需要三斜杠参考? [英] When do I need a triple slash reference?
问题描述
当 Anders Hejlsberg 在以下视频中 35:00 左右谈论外部模块时......
When Anders Hejlsberg is talking about external modules around 35:00 in the following video...
Anders Hejlsberg:TypeScript 简介
...为什么文件 server.ts
需要对 node.d.ts 的三斜杠引用,而 hello.ts
不需要类似的引用server.ts
?
... why does the file server.ts
require a triple slash reference to node.d.ts where as hello.ts
doesn't require a similar reference to server.ts
?
特别是在 hello.ts
中,他提到存在智能感知,并且他可以使用从 server.ts
导出的项目.那么三重斜线引用还能添加什么?
In particular in hello.ts
he mentions that intellisense is present plus he gets to use the exported item from server.ts
. So what more could the triple slash reference add?
推荐答案
在两种情况下需要三斜线引用:
You need a triple slash reference in two scenarios:
- 当您引用 JavaScript 类型定义时,例如节点、jQuery 等的定义,请参阅:https://github.com/DefinitelyTyped/DefinitelyTyped一个>
- 当我们想使用
--out
进行编译时,您可以使用///
引用您的文件.
- When you are referencing JavaScript type definitions e.g. definitions for node, jQuery, etc. for a great collection see : https://github.com/DefinitelyTyped/DefinitelyTyped
- When we want to compile using
--out
you can reference your files using/// <reference
.
使用外部模块时需要import/require
组合,即amd
/commonjs
.如果您不知道这些是什么意思(amd/commonjs 是 javascript 术语,并非特定于打字稿),您不必关心.只需使用 ///
--out
进行编译.
You need a import/require
combo when using external modules i.e. amd
/commonjs
. If you don't know what these mean (amd/commonjs are javascript terms, not specific to typescript) you don't have to care. Just use /// <reference
and compile with --out
.
PS:我有一个关于内部模块与外部模块的视频教程:TypeScript Modules揭秘:内部,AMD 带 RequireJS,CommonJS 带 NodeJS
PS: I have a video tutorial on internal vs. external modules: TypeScript Modules Demystified : Internal, AMD with RequireJS, CommonJS with NodeJS
新项目请使用 tsconfig.json
而不是参考注释:https://basarat.gitbook.io/typescript/project/compilation-context
Please use tsconfig.json
for new projects instead of reference comments : https://basarat.gitbook.io/typescript/project/compilation-context
这篇关于我什么时候需要三斜杠参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!