Skip to content

Commit

Permalink
Docs: Added more information on typings usage, see #744
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Apr 4, 2017
1 parent 602065e commit 1c4d9d7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -458,14 +458,16 @@ protobuf.load("awesome.proto", function(err, root) {
console.log(`message = ${JSON.stringify(message)}`);

let buffer = AwesomeMessage.encode(message).finish();
console.log(`buffer = ${Array.prototype.slice.call(buffer)}`);
console.log(`buffer = ${Array.prototype.toString.call(buffer)}`);

let decoded = AwesomeMessage.decode(buffer);
console.log(`decoded = ${JSON.stringify(decoded)}`);
});
```
If you generated static code using the CLI to `bundle.js` and its type definitions to `bundle.d.ts`, then you can do:
**Note:** Dynamically generated runtime message classes cannot be typed, technically, so you must either access its fields using `message["awesomeField"]` notation or you can utilize [typings of its static counterpart](https://github.com/dcodeIO/protobuf.js/tree/master/cli#pbts) for full typings support.
If you generated static code to `bundle.js` using the CLI and its type definitions to `bundle.d.ts` instead, then you can just do:
```ts
import * as root from "./bundle.js";
Expand All @@ -477,13 +479,10 @@ var buffer = AwesomeMessage.encode(message).finish();
...
```
**Note:** By default, the npm package ships with long.js including its typings and node typing as optional dependencies. However, where long.js and/or node Buffers are not required, there are two stubs available that can be referenced instead of the full type definitions:
**Note:** When using [long.js](https://github.com/dcodeIO/long.js), make sure to add [@types/long](https://www.npmjs.com/package/@types/long) to your project's dependencies. Likewise, when building for node, make sure to add [@types/node](https://www.npmjs.com/package/@types/node). Alternatively, if you are not using one of these, there are two stubs available that can be referenced instead of the respective full type definition:
```ts
/// <reference path="./node_modules/protobufjs/stub-long.d.ts" />
```
```ts
/// <reference path="./node_modules/protobufjs/stub-node.d.ts" />
```
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@
"@protobufjs/utf8": "^1.1.0"
},
"optionalDependencies": {
"long": "^3.2.0",
"@types/long": "^3.0.31",
"@types/node": "7.0.12"
"long": "^3.2.0"
},
"devDependencies": {
"@types/long": "^3.0.31",
"@types/node": "7.0.12",
"benchmark": "^2.1.4",
"browserify": "^14.1.0",
"browserify-wrap": "^1.0.2",
Expand All @@ -77,7 +77,7 @@
"gulp": "^3.9.1",
"gulp-header": "^1.8.8",
"gulp-if": "^2.0.1",
"gulp-sourcemaps": "^2.5.0",
"gulp-sourcemaps": "^2.5.1",
"gulp-uglify": "^2.1.2",
"istanbul": "^0.4.5",
"jaguarjs-jsdoc": "dcodeIO/jaguarjs-jsdoc",
Expand Down

0 comments on commit 1c4d9d7

Please sign in to comment.