Typescript更正多个文件的用法
我有以下文件结构:Typescript更正多个文件的用法,typescript,Typescript,我有以下文件结构: project | | -- src | | | |--util | | | |--StringUtils.ts | |--Constants.ts | | -- test | | -- catalog | | -- issue | |
project
|
| -- src
| |
| |--util
| |
| |--StringUtils.ts
| |--Constants.ts
|
| -- test
|
| -- catalog
|
| -- issue
|
| -- myTest.ts
以及文件的内容:StringUtils.ts:
module Util {
export class StringUtils {
static format(formatString:String, ...replacements:string[]):String {
return formatString.replace(/{(\d+)}/g, function (match, number) {
return typeof replacements[number] != 'undefined'
? replacements[number]
: match;
})
}
}
}
export class StringUtils {
static format(formatString:String, ...replacements:string[]):String {
return formatString.replace(/{(\d+)}/g, function (match, number) {
return typeof replacements[number] != 'undefined'
? replacements[number]
: match;
})
}
}
myTest.ts:
import imported = require("../../../src/util/StringUtils");
exports.testSomething = function(test) {
var testOutput:String = imported.Util.StringUtils.format("{0}, this is a test", "Mofo");
test.ok(true, "this assertion should pass");
test.done();
};
import {StringUtils} from "../../../src/util/StringUtils";
exports.testSomething = function(test) {
var testOutput:String = StringUtils.format("{0}, this is a test", "Mofo");
test.ok(true, "this assertion should pass");
test.done();
};
当与nodeunit一起运行时,我得到:
TypeError:无法读取未定义的属性“StringUtils”
如何正确引用该文件?您可以忽略模块,而使用ES6的导入语句。您的代码应该如下所示 StringUtils.ts:
module Util {
export class StringUtils {
static format(formatString:String, ...replacements:string[]):String {
return formatString.replace(/{(\d+)}/g, function (match, number) {
return typeof replacements[number] != 'undefined'
? replacements[number]
: match;
})
}
}
}
export class StringUtils {
static format(formatString:String, ...replacements:string[]):String {
return formatString.replace(/{(\d+)}/g, function (match, number) {
return typeof replacements[number] != 'undefined'
? replacements[number]
: match;
})
}
}
myTest.ts:
import imported = require("../../../src/util/StringUtils");
exports.testSomething = function(test) {
var testOutput:String = imported.Util.StringUtils.format("{0}, this is a test", "Mofo");
test.ok(true, "this assertion should pass");
test.done();
};
import {StringUtils} from "../../../src/util/StringUtils";
exports.testSomething = function(test) {
var testOutput:String = StringUtils.format("{0}, this is a test", "Mofo");
test.ok(true, "this assertion should pass");
test.done();
};
编辑
实际上,对模块和名称空间存在误解。在Typescript的新版本中,被称为模块的现在是名称空间
。从文档中:
关于术语的说明:在TypeScript中需要注意的是
1.5,术语已更改。“内部模块”现在是“名称空间”。“外部模块”现在只是“模块”,用于对齐
使用ECMAScript 2015的术语(即模块X{
相当于现在首选的命名空间X{)
建议使用ES6的方法,其中每个文件本身都是一个模块,但如果仍要使用名称空间来分隔文件中的代码,则需要按照之前定义的模块定义名称空间:
namespace Util {
export class StringUtils {
.....
然后将其导入到任何您想与古典音乐一起使用的地方:
/// <reference path="path.to.file.ts"/>
//
尝试从“./../src/util/StringUtils”
导入ES6样式的
导入{StringUtils};可能需要从StringUtils.ts中删除模块util
,并且只保留有效的类StringUtilsOK(与模块删除一起)。将其作为答案发布查看我的答案的更新