JSPM

  • Created
  • Published
  • Downloads 81898
  • Score
    100M100P100Q162745F
  • License MIT

Compile proto files to typescript.

Package Exports

  • protoc-gen-ts

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (protoc-gen-ts) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Protoc Gen Typescript

npm npm bundle size npm

Generates appropriate Protocol Buffer sources from Proto files directly through TypeScript Compiler API.

This plugin generates plain Typescript files that can be used AMD, UMD, CommonJS module systems.

Aim of this protoc plugin is to make usage of protocol buffers easy in Javascript/Typescript by taking modern approaches.

Example

syntax = "proto3";

message Change {
    Kind kind = 1;
    string patch = 2;
}

enum Kind {
    UPDATED = 0;
    DELETED = 1;
}
// Constructed message
const change = new Change({
    kind: Kind.UPDATED,
    patch: "@@ -7,11 +7,15 @@"
});

// Sent over the wire
const bytes: Uint8Array = change.serialize();

const receivedChange: Change = Change.deserialize(bytes);

console.log(receivedChange.kind == Kind.UPDATE) // true
console.log(receivedChange.patch) // "@@ -7,11 +7,15 @@"

Key Differences

This protoc plugin does generate;

  • Fields as getter setters.
  • Enums as enums.
  • Messages within a namespace if the proto has a package directive.

Usage

Without Bazel

npm install -g protoc-gen-ts

protoc -I=sourcedir --ts_out=dist myproto.proto

With Bazel

# Add protoc-gen-ts to dependencies section of your package.json file.
# Then use it like you would use the other bazel compatible npm packages.

load("@npm_protoc_gen_ts//:index.bzl", "ts_proto_library")

ts_proto_library(
    name = "protos",
    deps = [
        ":some_proto_library_target"
    ]
)

# Checkout the examples/bazel directory for an example.

Roadmap

  • Support for repeated non-integer fields
  • Generate appropriate service code that is usable with node grpc package.
  • Support for creating protocol buffer messages directly from their constructors with an object.
  • Support map<TYPE, TYPE> fields.
  • Support for import directive.
  • Interopability with well knowns.

Ideas

  • Support map<TYPE, TYPE> types as ES Map.
  • Make services strongly typed.
  • Support for Promise in rpcs.

Alternatives

Plugin google-protobuf Typescript Declarations gRPC Node gRPC Web ES6 Support Notes
thesayyn/protoc-gen-ts Yes Yes Yes Yes Partial Yes The generated messages are compatible with ever-green browsers.
However, you might need to use third-party packages to use rpcs.
improbable-eng/ts-protoc-gen Yes No Yes No Yes Partial Drawback: You can't bundle generated files with rollup since
they are not >= ES6 compatible.
stephenh/ts-proto No Yes Yes No No Yes There is no support for rpcs.
See: https://github.com/stephenh/ts-proto/issues/2