Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Unused code generated with ts-proto and nestJs=true when using google.protobuf.Struct #1199

@00KL

Description

@00KL

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": true

With 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

  1. Create a new empty folder and enter it:

    mkdir grpc-node-unused-code-example
    cd grpc-node-unused-code-example
  2. Create a file named package.json with 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"
      }
    }
  3. Install the dependencies:

    npm install
  4. Create a file named example.proto with the following content:

    syntax = "proto3";
    import "google/protobuf/struct.proto";
    
    service ExampleService {
      rpc ExampleMethod (google.protobuf.Struct) returns (google.protobuf.Struct);
    }
  5. Create a file named tsconfig.json with the following content:

    {
      "compilerOptions": {
        "noUnusedLocals": true,
        "noUnusedParameters": true
      }
    }
  6. 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
  7. Check the generated TypeScript code for google.protobuf.Struct in the generated directory.

You will see code like:

function createBaseValue(): Value {
  return {};
}

export interface MessageFns<T> {}

These code snippets are never used.

Environment

Additional context

This only happens when using ts_proto_opt=nestjs=true and the noUnusedLocals/noUnusedParameters options.
Include logs or more context if needed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions