You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+26-31
Original file line number
Diff line number
Diff line change
@@ -466,18 +466,23 @@ Note that the service API is meant for clients. Implementing a server-side endpo
466
466
467
467
### Usage with TypeScript
468
468
469
-
The library ships with its own [type definitions](https://github.com/dcodeIO/protobuf.js/blob/master/index.d.ts) and modern editors like [Visual Studio Code](https://code.visualstudio.com/) should automatically detect and use them for code completion when following this pattern:
469
+
The library ships with its own [type definitions](https://github.com/dcodeIO/protobuf.js/blob/master/index.d.ts) and modern editors like [Visual Studio Code](https://code.visualstudio.com/) will automatically detect and use them for code completion.
470
+
471
+
**Note** that the npm package depends on [@types/node](https://www.npmjs.com/package/@types/node) because of `Buffer` and [@types/long](https://www.npmjs.com/package/@types/long) because of `Long`. If you are not building for node and/or not using long.js and want to exclude their full type definitions manually for whatever reason, there are two stubs available that can be referenced instead of the respective full type definition:
The API shown above works pretty much the same with TypeScript. However, because everything is typed, accessing fields on instances of dynamically generated message classes requires either (1) using bracket-notation (i.e. `message["awesomeField"]`), (2) explicit casts or (3) the use of a [typings file generated for its static counterpart](#pbts-for-typescript).
**Note:** Dynamically generated 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](#pbts-for-typescript) for full typings support.
503
+
#### Using generated static code
499
504
500
-
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:
505
+
If you generated static code to `bundle.js` using the CLI and its type definitions to `bundle.d.ts`, then you can just do:
501
506
502
507
```ts
503
-
import*asrootfrom"./bundle.js";
508
+
import{ AwesomeMessage }from"./bundle.js";
504
509
505
510
// example code
506
-
var AwesomeMessage =root.AwesomeMessage;
507
-
var message =AwesomeMessage.create({ awesomeField: "hello" });
508
-
var buffer =AwesomeMessage.encode(message).finish();
509
-
...
510
-
```
511
-
512
-
**Note** that the npm package depends on [@types/node](https://www.npmjs.com/package/@types/node) because of `Buffer` and [@types/long](https://www.npmjs.com/package/@types/long) because of `Long`.
513
-
514
-
If you are not building for node and/or not using long.js and want to exclude their full type definitions manually for whatever reason, there are two stubs available that can be referenced instead of the respective full type definition:
let message =AwesomeMessage.create({ awesomeField: "hello" });
512
+
let buffer =AwesomeMessage.encode(message).finish();
513
+
let decoded =AweesomeMessage.decode(buffer);
519
514
```
520
515
521
-
#### Experimental decorators
516
+
#### Using decorators
522
517
523
-
**WARNING:** Just introduced, not well tested, probably buggy.
518
+
The library also includes an early implementation of [decorators](https://www.typescriptlang.org/docs/handbook/decorators.html).
524
519
525
-
protobuf.js ships with an initial implementation of decorators, but note that decorators in TypeScript are an experimental feature and are subject to change without notice - plus - you have to enable the feature explicitly with the `experimentalDecorators` option:
520
+
**Note** that this API is rather new in protobuf.js (and probably buggy) and that decorators are an experimental subject-to-change-without-notice feature in TypeScript. Also note that declaration order is important depending on the JS target. For example, `@Field.d(2, AwesomeArrayMessage)` requires that `AwesomeArrayMessage` has been defined earlier when targeting `es5`.
0 commit comments