egret protobuf生成ts文件报错问题

使用egret protobuf生成ts文件报错问题

首先看白鹭官网提供的demo
egret protobuf
按步骤安装完成后
添加一个test.proto文件,内容如下

package testpackage;

message TestMessage {
    required string testName = 1;
    repeated int32 testNum = 2;
}


目录下执行命令:

pb-egret generate

转出的protobuf-bundles.d.ts文件去掉注释如下

type Long = protobuf.Long;

declare namespace testpackage {
    interface ITestMessage {
        testName: string;
        testNum?: (number[]|null);
    }

    class TestMessage implements ITestMessage {
        constructor(properties?: testpackage.ITestMessage);
        public testName: string;
        public testNum: [ 'Array' ].<number>;
        public static create(properties?: testpackage.ITestMessage): testpackage.TestMessage;
        public static encode(message: testpackage.ITestMessage, writer?: protobuf.Writer): protobuf.Writer;
        public static encodeDelimited(message: testpackage.ITestMessage, writer?: protobuf.Writer): protobuf.Writer;
        public static decode(reader: (protobuf.Reader|Uint8Array), length?: number): testpackage.TestMessage;
        public static decodeDelimited(reader: (protobuf.Reader|Uint8Array)): testpackage.TestMessage;
        public static verify(message: [ 'object' ].<string, any>): (string|null);
    }
}

像这种message: [ 'object' ].<string, any>和[ 'Array' ].<number> 都显示有语法问题,后来在
一篇白鹭论坛文章
上看到有人说是node版本太高导致,看了下我的是v12.16.1,把node版本降到v10.21.0解析出来就是正确的message: { [k: string]: any }和number[]

分析下原因:
看了下node_modules@egret\protobuf\src\index.ts中代码,转换成js和ts文件就是下面这两行代码

await shell('pbjs', args);
...
await shell('pbts', ['--main', output, '-o', tempfile]);

找到pbts.js文件,里面callJsdoc函数有如下

 var cmd = "node \"" + require.resolve("jsdoc/jsdoc.js") + "\" -c \"" + path.join(basedir, "lib", "tsd-jsdoc.json") + "\" -q \"module=" + encodeURIComponent(moduleName) + "&comments=" + Boolean(argv.comments) + "\" " + files.map(function(file) { return "\"" + file + "\""; }).join(" ");

果然是通过node命令,猜测下高版本的解释出的类型估计是不兼容现在的写法,关键是网上找了一圈,也没message: [ 'object' ].<string, any>和[ 'Array' ].<number> 这种写法。
but,降版本确实可以正确解析出来!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值