protobufjs / bytebuffer.js

A fast and complete ByteBuffer implementation using either ArrayBuffers in the browser or Buffers under node.js.
http://dcode.io
Apache License 2.0
723 stars 155 forks source link

ByteBuffer.js used from TypeScript #85

Open courteous opened 6 years ago

courteous commented 6 years ago

I am trying to use the ByteBuffer.js in typescript. I have downloaded the typescript definitions i.e. the index.d.ts from

here

and i did the same for the Long.js it all went well except 2 warnings

when i try to execute the following code

var bb = new ByteBuffer()
.writeIString("Hello world!")
.flip();
console.log( "base 64 encoding of the buffer: " + bb.toBase64() );

var bb = ByteBuffer.allocate(2, false, false);

I am getting the following two warnings:

WARNING in ./ts/app.ts
18:9-28 "export 'allocate' (imported as 'ByteBuffer') was not found in '../definitions/ByteBuffer'
 @ ./ts/app.ts
 @ multi ./ts/app.ts ./ts/app.ts

WARNING in ./definitions/ByteBuffer.js
Module not found: Error: Can't resolve 'long' in '/home/tito/Projects/external/definitions'
 @ ./definitions/ByteBuffer.js 30:35-50
 @ ./ts/app.ts
 @ multi ./ts/app.ts ./ts/app.ts

if I comment those lines below then the second warning about the long is gone i guess this is the same behaviour explained in the ticket number (#47 opened on May 8, 2015 by jcalfee)

    /* CommonJS */ else if (typeof require === 'function' && typeof module === "object" && module && module["exports"])
        module['exports'] = (function() {
            var Long; try { Long = require("long"); } catch (e) {}
            return factory(Long);
        })();

However i do not know what to do with the first warring why is typescript sayng that "export 'allocate' was not found. I guess this have to do with the fact that ByteBuffer.js is ready to be consumed as CommonJS and AMD module but somehow i am missing the es6 modules exports, but i might be wrong. Any ideas about this?

dcodeIO commented 6 years ago

Usually you'd simply add long and bytebuffer like so:

npm install long bytebuffer

and use the definitions in development:

npm install --save-dev @types/long @types/bytebuffer

Afterwards, something like import * as ByteBuffer from "bytebuffer" should just work. Note that these definitions have been created by a 3rd party.

courteous commented 6 years ago

Hello Daniel, many thanks for the quick response. Indeed i forgot to do "npm install long" . Many thanks for the quick feedback.