Answers:
从TypeScript 1.8版开始,您可以import像在ES6中一样使用简单的语句:
import { ZipCodeValidator } from "./ZipCodeValidator";
let myValidator = new ZipCodeValidator();
https://www.typescriptlang.org/docs/handbook/modules.html
旧答案:从TypeScript 1.5版开始,您可以使用tsconfig.json:http : //www.typescriptlang.org/docs/handbook/tsconfig-json.html
它完全消除了注释样式引用的需要。
较旧的答案:
您需要在当前文件的顶部引用该文件。
您可以这样做:
/// <reference path="../typings/jquery.d.ts"/>
/// <reference path="components/someclass.ts"/>
class Foo { }
等等
这些路径是相对于当前文件的。
你的例子:
/// <reference path="moo.ts"/>
class bar extends moo.foo
{
}
reference编译后不会生成任何加载该js的js代码。它仅用于编译器。我没有TypeScript和AMD的经验,因为我只是捆绑了生成的js文件,因此我需要的一切都在那里。但是,我看到了您的需求,您可以在此处阅读有关此内容的更多信息:typescriptlang.org/Content/…在第75页(第9章)。值得一读的是整个规范,与其他语言相比,它相对较短。
--all在主.ts文件中将标志与tsc一起使用。编译器根据您的参考标记找出所有依赖关系,并为整个应用程序生成一个输出.js文件:tsc --out app.js main.ts
es2015(或esnext)模块,指定的文件扩展名import的语句是强制性的(除非你做这个)。TypeScript不会为您添加扩展名。另外,目前无法输出.mjs,因此扩展名必须为.js。
Typescript区分两种不同的模块:内部模块用于内部构造代码。在编译时,您必须使用引用路径将内部模块引入作用域:
/// <reference path='moo.ts'/>
class bar extends moo.foo {
}
另一方面,外部模块用于引用要在运行时使用CommonJS或AMD加载的外部源文件。对于您的情况,要使用外部模块加载,您必须执行以下操作:
o
export class foo {
test: number;
}
应用程序
import moo = module('moo');
class bar extends moo.foo {
test2: number;
}
注意将代码嵌入作用域的不同方法。对于外部模块,必须使用module包含模块定义的源文件的名称。如果要使用AMD模块,则必须按以下方式调用编译器:
tsc --module amd app.ts
然后将其编译为
var __extends = this.__extends || function (d, b) {
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
}
define(["require", "exports", 'moo'], function(require, exports, __moo__) {
var moo = __moo__;
var bar = (function (_super) {
__extends(bar, _super);
function bar() {
_super.apply(this, arguments);
}
return bar;
})(moo.foo);
})
如果您使用的是AMD模块,则其他答案将在TypeScript 1.0(撰写本文时为最新版本)中不起作用。
您可以使用不同的方法,具体取决于您希望从每个.ts文件中导出多少个内容。
脚
export class Foo {}
export interface IFoo {}
巴特
import fooModule = require("Foo");
var foo1 = new fooModule.Foo();
var foo2: fooModule.IFoo = {};
脚
class Foo
{}
export = Foo;
巴特
import Foo = require("Foo");
var foo = new Foo();
如果您希望使用模块并将其编译为单个JavaScript文件,则可以执行以下操作:
tsc -out _compiled/main.js Main.ts
维护
///<reference path='AnotherNamespace/ClassOne.ts'/>
///<reference path='AnotherNamespace/ClassTwo.ts'/>
module MyNamespace
{
import ClassOne = AnotherNamespace.ClassOne;
import ClassTwo = AnotherNamespace.ClassTwo;
export class Main
{
private _classOne:ClassOne;
private _classTwo:ClassTwo;
constructor()
{
this._classOne = new ClassOne();
this._classTwo = new ClassTwo();
}
}
}
ClassOne.ts
///<reference path='CommonComponent.ts'/>
module AnotherNamespace
{
export class ClassOne
{
private _component:CommonComponent;
constructor()
{
this._component = new CommonComponent();
}
}
}
CommonComponent.ts
module AnotherNamespace
{
export class CommonComponent
{
constructor()
{
}
}
}
您可以在此处了解更多信息:http : //www.codebelt.com/typescript/javascript-namespacing-with-typescript-internal-modules/
我会避免现在使用,/// <reference path='moo.ts'/>而是将外部文件库用于不包含定义文件的外部库。
的 reference path编辑器解决了错误,但它并没有真正意味着文件需从国外进口。因此,如果您使用的是gulp工作流程或JSPM,则它们可能会尝试分别编译每个文件,而不是tsc -out一个文件。
从打字稿1.5开始
只需在文件级别(根作用域)添加要导出的前缀
图书馆
{
export class AClass(){} // exported i.e. will be available for import
export valueZero = 0; // will be available for import
}
您也可以稍后在文件末尾添加要导出的内容
{
class AClass(){} // not exported yet
valueZero = 0; // not exported yet
valueOne = 1; // not exported (and will not in this example)
export {AClass, valueZero} // pick the one you want to export
}
甚至混合在一起
{
class AClass(){} // not exported yet
export valueZero = 0; // will be available for import
export {AClass} // add AClass to the export list
}
对于导入,您有2个选项,首先您再次选择想要的内容(一个接一个)
anotherFile.ts
{
import {AClass} from "./aLib.ts"; // you import only AClass
var test = new AClass();
}
或整个出口
{
import * as lib from "./aLib.ts"; // you import all the exported values within a "lib" object
var test = new lib.AClass();
}
关于出口的注意事项:出口两次相同的值将引发错误{export valueZero = 0; 导出{valueZero}; // valueZero已经导出...}
import {AClass} from "./aLib.ts";
从TypeScript开始,1.8+您可以使用简单的简单import语句,例如:
import { ClassName } from '../relative/path/to/file';
或通配符版本:
import * as YourName from 'global-or-relative';
了解更多:https : //www.typescriptlang.org/docs/handbook/modules.html
Visual Studio中的快速简易过程
将扩展名为.ts的文件从解决方案窗口拖放到编辑器,它将生成内联参考代码,如..
/// <reference path="../../components/someclass.ts"/>