-
Notifications
You must be signed in to change notification settings - Fork 381
Description
Problem description
When generating TypeScript code from a .proto file that uses google.protobuf.Struct and the option ts_proto_opt=nestjs=true, the generated code includes unused functions and interfaces.
This unused code causes TypeScript type-checking errors in NestJS projects when the following options are enabled in tsconfig.json:
"noUnusedLocals": true,
"noUnusedParameters": trueWith these options, TypeScript will report errors for unused code, such as:
ERROR in ./libs/device-communication/src/generated/google/protobuf/struct.ts:104:10
TS6133: 'createBaseValue' is declared but its value is never read.
102 | };
103 |
> 104 | function createBaseValue(): Value {
| ^^^^^^^^^^^^^^^
105 | return {};
106 | }
107 |
ERROR in ./libs/device-communication/src/generated/google/protobuf/struct.ts:195:28
TS6133: 'T' is declared but its value is never read.
193 | } as any;
194 |
> 195 | export interface MessageFns<T> {}
| ^^^
196 |
197 | export interface StructWrapperFns {
198 | wrap(object: { [key: string]: any } | undefined): Struct;
Found 2 errors in 5260 ms.
Reproduction steps
-
Create a new empty folder and enter it:
mkdir grpc-node-unused-code-example cd grpc-node-unused-code-example -
Create a file named
package.jsonwith the following content:{ "name": "grpc-node-unused-code-example", "version": "1.0.0", "private": true, "description": "Minimal reproduction for unused code generated by ts-proto with nestJs=true and google.protobuf.Struct", "scripts": { "generate": "protoc --plugin=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_out=./generated --ts_proto_opt=nestJs=true,unrecognizedEnum=false,env=node -I=. example.proto" }, "devDependencies": { "ts-proto": "^2.7.2", "typescript": "^5.8.3" } } -
Install the dependencies:
npm install
-
Create a file named
example.protowith the following content:syntax = "proto3"; import "google/protobuf/struct.proto"; service ExampleService { rpc ExampleMethod (google.protobuf.Struct) returns (google.protobuf.Struct); }
-
Create a file named
tsconfig.jsonwith the following content:{ "compilerOptions": { "noUnusedLocals": true, "noUnusedParameters": true } } -
Run the code generation command (either as a script or raw command):
- Using npm script:
npm run generate
- Or directly:
npx protoc --plugin=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_out=./generated --ts_proto_opt=nestJs=true,unrecognizedEnum=false,env=node -I=. example.proto
- Using npm script:
-
Check the generated TypeScript code for
google.protobuf.Structin thegenerateddirectory.
You will see code like:
function createBaseValue(): Value {
return {};
}
export interface MessageFns<T> {}These code snippets are never used.
Environment
- OS name, version and architecture:
- Node version:
- Node installation method:
- Compiler version:
- Package name and version:
Additional context
This only happens when using ts_proto_opt=nestjs=true and the noUnusedLocals/noUnusedParameters options.
Include logs or more context if needed.