Within my ts-node project, I am converting TypeScript from gRPC proto files, where certain properties are denoted as optional.
However, the resulting TS interfaces end up with ALL properties being marked as optional. Additionally, an extra "_" prefixed property is created for the one that should actually be optional?
What I really need is a way to easily determine in my TS code whether a field is optional or not (to avoid unnecessary undefined checks), but navigating through this generated code proves challenging.
Is there a solution to correct this issue or perhaps customize the code generation process?
(Proto version 3)
message GetStatusResponse {
OperationMode mode = 1;
optional string transactionId = 2;
SystemState state = 3;
string systemName = 4;
}
// Original file: proto/autofuel.proto
import type { OperationMode as _autofuel_control_OperationMode, OperationMode__Output as _autofuel_control_OperationMode__Output } from '../../autofuel/control/OperationMode';
import type { SystemState as _autofuel_control_SystemState, SystemState__Output as _autofuel_control_SystemState__Output } from '../../autofuel/control/SystemState';
export interface GetStatusResponse {
'mode'?: (_autofuel_control_OperationMode);
'transactionId'?: (string);
'state'?: (_autofuel_control_SystemState);
'systemName'?: (string);
'_transactionId'?: "transactionId";
}
export interface GetStatusResponse__Output {
'mode'?: (_autofuel_control_OperationMode__Output);
'transactionId'?: (string);
'state'?: (_autofuel_control_SystemState__Output);
'systemName'?: (string);
}
I had hoped for something resembling:
export interface GetStatusResponse {
mode: _autofuel_control_OperationMode;
transactionId: string;
state: _autofuel_control_SystemState;
systemName: string;
}
The command I utilize for generation:
proto-loader-gen-types --grpcLib=@grpc/grpc-js --outDir=proto/generated/ proto/*.proto
My package.json
:
{
"name": "machine-ui-gateway",
"version": "0.0.1",
"main": "main.ts",
"license": "MIT",
"dependencies": {
"@grpc/grpc-js": "^1.8.0",
"@grpc/proto-loader": "^0.7.4",
"async-mqtt": "^2.6.3",
"dotenv": "^16.0.3",
"express": "^4.18.2"
},
"devDependencies": {
"@types/express": "^4.17.14",
"nodemon": "^2.0.20",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
},
"scripts": {
"build": "tsc",
"start": "nodemon main.ts",
"proto:gen": "proto-loader-gen-types --grpcLib=@grpc/grpc-js --outDir=proto/generated/ proto/*.proto"
}
}