Proto2TypeScript icon indicating copy to clipboard operation
Proto2TypeScript copied to clipboard

Working example(s)?

Open btgoodwin opened this issue 9 years ago • 5 comments

I've searched for days for a working example of how someone has used this library to work with protocol buffers in TypeScript and have come up with nothing. The example provided in the README.md also indicates a method on ProtoBuf (protobufjs, I assume) that does not presently exist in that library (protoFromFile). I admit I'm still learning all the nuances of TypeScript and project configurations, however it would be great to see the source code of a working example for using this library and protobufs in general.

For my particular use case, I have a library that I'm porting from JS/Angular1 to TS/Angular2 as a ES2015 module, and it has some protocol buffers defined in it. I've tried going through the process of converting the proto files to JSON, and then from JSON to typings definitions as indicated in the README.md. I then installed those definitions (d.ts) into my global typings (typings install --global --save file:<path>/file.d.ts). When I then try to reference the default module name generated by this tool, Proto2TypeScript, in my TypeScript files, I get: Cannot find namespace 'Proto2TypeScript'. I also cannot import that module name, etc. which leaves me wondering what part of this process is actually broken since there is no end-to-end example that I can find.

btgoodwin avatar Nov 08 '16 14:11 btgoodwin

I've been working with this all day and have nothing. When I reference and import the definition typescript file, I end up with dozens of errors from TypeScript indicating problems like "Declaration or statement expected" etc. which indicate to me the file format generated by Proto2Ts is incorrect.

Are there some other constraints to using this library? Like only protobuf 3.x definitions?

btgoodwin avatar Nov 08 '16 19:11 btgoodwin

Hei, I'm sorry to hear that. I don't use this library anymore and I don't exactly know in which state it is with the latest releases of Protobuf, Protobuf.js, and TypeScript. It looks like some work is required.

fungiboletus avatar Nov 09 '16 12:11 fungiboletus

Did you move on to something more effective/up to date?

When you did use the library, was the workflow for this?

  1. Generate the descriptor (d.ts) file
  2. Add it to typings (typings install --global --save file:d.ts)
  3. Add to your TS file: import { SomeExportedInterface } from 'ModuleName' // declared in the d.ts

btgoodwin avatar Nov 09 '16 14:11 btgoodwin

The problem it's having is type extensions. For example:

message SuperType {
    extensions 101 to max;
    enum TYPE {
        option allow_alias = true;
        Type1 = 1;
        // etc.
    }
    required TYPE type = 1;
}

message SubType {
    extend SuperType {
        optional SubType supertype = 101;
    }
   // Some special field for this type
}

I'm poking around in the DUST templates now to see if I can clarify this.

btgoodwin avatar Nov 09 '16 15:11 btgoodwin

I don't use Typescript anymore in my professionnals projects :(

I merge pull requests time to time, but because I am not an user of this tool, I don't really know in which state it is with the recent versions.

When I used the library, it wasn't with ES2015 and typings, so I did most of the things by hand. I don't know whether type extensions are working. You can check the DUST templates yes :-)

fungiboletus avatar Nov 09 '16 18:11 fungiboletus