0.1.3 • Published 2 years ago
proto-to-ts-type v0.1.3
proto-to-ts-type
This library will generate TypeScript types from .proto files.
This does not generate other features like binary serializing/deserializing, toJson/fromJson or other stuff like that.
how to use
- install package
yarn add --dev proto-to-ts-type- execute
protoccommand
- without option
protoc \
--plugin="./node_modules/.bin/protoc-gen-ts-type" \
--ts-type_out=. \
proto.proto- with options
For example, If you'd like to generate json_name defined in protofiles as TS type fields and enum as string, execute the following command.
Each the pair of key and value is supposed to be delimited by equal sign =, and comma , for each parameters.
protoc \
--plugin="./node_modules/.bin/protoc-gen-ts-type" \
--ts-type_out=. \
--ts-type_opt=useJsonName=true,enumValueAsString=true \
proto.protoAvailable option
Check in src/option.ts.
| key | value | |
|---|---|---|
| useJsonName | true/false | json_name in a proto file would be used. |
| enumValueAsString | true/false | Enum value would be same as member names instead of numbers, which starts with 0 |
| useTypeGuardForOneOf | true/false | If true, add type guard functions for oneof operation, or optional properties. |
The mapping rules of proto to TypeScript types
| proto | TypeScript |
|---|---|
| double | number |
| float | number |
| int32 | number |
| uint32 | number |
| sint32 | number |
| fixed32 | number |
| sfixed | number |
| int64 | bigint |
| uint64 | bigint |
| sint64 | bigint |
| fixed64 | bigint |
| sfixed64 | bigint |
| string | string |
| bool | boolean |
| group | type_name of FieldDescriptorProto |
| enum | type_name of FieldDescriptorProto |
| message | type_name of FieldDescriptorProto |