【问题标题】:How to setup VSCode intellisense for Protobuf-generated JavaScript files?如何为 Protobuf 生成的 JavaScript 文件设置 VSCode 智能感知?
【发布时间】:2020-10-28 02:53:37
【问题描述】:

我的 VSCode 安装通常会在我的 Javascript 项目中提供智能感知提示。但是,我在让 VSCode 显示/注册从另一个 JavaScript 文件导入的消息类型时遇到问题,该文件是通过协议缓冲区编译器 protoc 生成的。

下面是我面临的问题的屏幕截图。根据下面添加的 protobuf 文件,我期望 jeff 对象上的 setId()setName()setSalary() 函数。但是,VSCode 似乎无法识别Schema.Employee 类型。

我可以做些什么来让智能感知与从协议缓冲区生成的类型一起工作?

reprex 使用 VSCode 1.50.1 和 protoc 版本测试libprotoc 3.13.0

我使用以下 protobuf 文件编译了

protoc --js_out=import_style=commonjs,binary:. employees.proto

生成employees_pb.js

employees.proto

syntax = "proto3";

option optimize_for = SPEED;

message Employee {
    int32 id = 1;
    string name = 2;
    float salary = 3;
}

message Employees {
    repeated Employee employees = 1;
} 

index.js:

// import schema from compiled file
const Schema = require("./employees_pb");

//create a new instance of Employee
const jeff = new Schema.Employee();
jeff.setId(1001); // <-- Intellisense does not work here.
jeff.setName("Jeff");
jeff.setSalary(1001);

package.json:

{
  "name": "protobuff",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "google-protobuf": "^3.13.0"
  }
}

【问题讨论】:

    标签: javascript visual-studio-code protocol-buffers


    【解决方案1】:

    在您的employees_pb 文件中,您可以更改最后一行以获得完整的智能感知:

    module.exports = {...goog, ...proto};
    // goog.object.extend(exports, proto);
    

    由于某种原因,exports 扩展不起作用?我在这个话题上打开了这个issue

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-31
      • 2015-08-27
      • 2019-11-11
      • 2015-08-27
      • 2019-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多