环境准备
npm install ts-proto- protoc环境直接在github.com/protocolbuf… 下载并配置环境变量
编写proto文件
syntax = "proto3";
package person;
message Person {
string name = 1;
uint64 age = 2;
}
生成代码
mac: protoc --plugin=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_out=. ./person.proto
windows: protoc --plugin=protoc-gen-ts_proto=.\node_modules\.bin\protoc-gen-ts_proto.cmd --ts_proto_out=. ./person.proto
./person.proto 换成你对应的proto文件的地址
可以看到proto文件对应的目录下生成了一个ts文件,import即可使用,比如:
const bytes = Simple.encode({ name: ..., age: ..., ... }).finish();
const simple = Simple.decode(bytes);
const { name, age } = simple;