typescript - 如何使用 Protobuf 生成的类作为 TypeScript 的类型?
问题描述
抱歉,如果这是基本的,但我是 TypeScript 的新手并且想知道......假设我生成了一个.js
名为的 Protobuf 文件
request_pb.js
而且我想在传递一个值时将生成的类之一合并为 TypeScript 类型,例如类似于...
const req = require('../build/gen/request_pb');
/**
* check if incoming request is valid
* @param data
*/
function validateRequest(data: req.Request) { // TODO: how to include type information?
return data.hasStartDateEt() && data.hasEndDateEt();
}
我目前正在
“找不到命名空间‘req’”
这到底是什么意思,我该怎么办?
解决方案
我使用将 TypeScript 定义文件生成到相同的构建目录中
$ protoc --proto_path=schemas --js_out=import_style=commonjs,binary:build/gen --ts_out=build/gen schemas/*.proto schemas/enums/*.proto
和ts-protoc-gen
包(https://www.npmjs.com/package/ts-protoc-gen)。
然后我可以使用导入类型
import account_statement_req from '../build/gen/account_statement_request_pb';
推荐阅读
- linux - 如何根据需要将所有文件合并为一个文件
- c# - 如何使用 Linq 在某些操作中聚合两个 List?
- r - 在 Window 10 上安装 nloptr 版本 1.2.1:错误 #include "nlopt.h"
- vb.net - 遍历富文本框中的每个单词
- ios - 表单/表格中的 SwiftUI 多个 NavigationLink - 条目保持突出显示
- python - 如何使用 PostgreSQL 和 %sql 魔术在 Jupiter 中选择带有空格的列名?
- java - 如何更改椭圆的行和列的颜色以响应鼠标悬停在其中一个椭圆上?
- php - android live设备上的离子和电容器的CORS问题
- swift - 我想将字符串值从 API 转换为自定义可编码模型 SWIFT
- mysql - 减去来自不一定相同的两个不同选择的记录的一列的值