在 TypeScript 文件中导入 AMD JS 库
Import an AMD JS library in a TypeScript file
我坚持使用正确的方法将 AMD JavaScript 库 (https://github.com/dcodeIO/bytebuffer.js) 导入 TypeScript 文件。
我找到了它的——不是最新的——类型定义(https://github.com/SINTEF-9012/Proto2TypeScript/commit/0889dccbf6048f116551a73e77d75dd83553cfe6),但实际上我无法找到使用它的方法并让 RequireJS 加载库。
这是我正在使用的代码:
/// <amd-dependency path="Scripts/bytebuffer" />
var ByteBuffer = require( 'Scripts/bytebuffer' );
import protocols = require( 'protocols' );
export class Pippo
{
readPayload( payload: ArrayBuffer, ECType: string ): any
{
var ECStruct = new protocols.ECStruct( ECType );
var bb = new ByteBuffer()
.writeIString( "Hello world!" )
.flip();
console.log( bb.readIString() + " from bytebuffer.js" );
}
}
两个模块protocols和bytebuffer加载正确,但实际上我看不到实例成员bb 在 Visual Studio 中。如果我把行
/// <reference path="scripts/typings/bytebuffer/bytebuffer.d.ts" />
和评论
//var ByteBuffer = require('Scripts/bytebuffer');
当然我可以看到 bb 的方法和属性,但是模块没有在运行时加载。
有没有办法让 ByteBuffer.js 由 RequireJS 加载并有可能在 VS 中看到它的成员?
谢谢
声明外部模块接口的语法是:
declare module 'amd/module/name' {
// module definition, probably with:
exports = thingToExport;
}
你的情况应该是:
declare module 'Scripts/bytebuffer' {
exports = ByteBuffer;
}
将其放在 bytebuffer.d.ts 文件之后!!! 另请参阅 handbook 中的 "Writing .d.ts files"。
在我的例子中,/// <amd-dependency>
和 /// <reference>
内容也是多余的 - 您可能也想尝试一下,以简化您的代码。
我坚持使用正确的方法将 AMD JavaScript 库 (https://github.com/dcodeIO/bytebuffer.js) 导入 TypeScript 文件。
我找到了它的——不是最新的——类型定义(https://github.com/SINTEF-9012/Proto2TypeScript/commit/0889dccbf6048f116551a73e77d75dd83553cfe6),但实际上我无法找到使用它的方法并让 RequireJS 加载库。
这是我正在使用的代码:
/// <amd-dependency path="Scripts/bytebuffer" />
var ByteBuffer = require( 'Scripts/bytebuffer' );
import protocols = require( 'protocols' );
export class Pippo
{
readPayload( payload: ArrayBuffer, ECType: string ): any
{
var ECStruct = new protocols.ECStruct( ECType );
var bb = new ByteBuffer()
.writeIString( "Hello world!" )
.flip();
console.log( bb.readIString() + " from bytebuffer.js" );
}
}
两个模块protocols和bytebuffer加载正确,但实际上我看不到实例成员bb 在 Visual Studio 中。如果我把行
/// <reference path="scripts/typings/bytebuffer/bytebuffer.d.ts" />
和评论 //var ByteBuffer = require('Scripts/bytebuffer'); 当然我可以看到 bb 的方法和属性,但是模块没有在运行时加载。
有没有办法让 ByteBuffer.js 由 RequireJS 加载并有可能在 VS 中看到它的成员?
谢谢
声明外部模块接口的语法是:
declare module 'amd/module/name' {
// module definition, probably with:
exports = thingToExport;
}
你的情况应该是:
declare module 'Scripts/bytebuffer' {
exports = ByteBuffer;
}
将其放在 bytebuffer.d.ts 文件之后!!! 另请参阅 handbook 中的 "Writing .d.ts files"。
在我的例子中,/// <amd-dependency>
和 /// <reference>
内容也是多余的 - 您可能也想尝试一下,以简化您的代码。