Skip to content

Wrong TypeScript type used for fields when name collision exists between imported and nested messages #3025

@somerobmitchell

Description

@somerobmitchell

Problem description

proto-loader-gen-types fails to disambiguate between imported top-level types and nested messages with the same name. When this collision occurs, it incorrectly uses the nested type, omits the required import, and generates types that don't match the protobuf structure. This causes type errors and breaks IDE autocomplete for affected fields.

Reproduction steps

Command:

npx proto-loader-gen-types --longs=String --enums=String --defaults --oneofs -I . --outDir=. common.proto service.proto

common.proto:

syntax = "proto3";
package example;

message Resource {
    int64 id = 1;
    string name = 2;
}

service.proto:

syntax = "proto3";
package example;
import "common.proto";

message Service {
    Resource resource = 1;  // Should reference Resource from common.proto
    
    message Status {
        message Metric {
            message Resource {  // Name collision
                string name = 1;
                int32 value = 2;
            }
        }
    }
}

Expected:

// example/Service.ts
import type { Resource as _example_Resource } from './Resource';

export interface Service {
  resource?: _example_Resource | null;  // Uses imported type
}

Actual:

// example/Service.ts
// Missing import for Resource

export interface Service {
  resource?: _example_Service_Status_Metric_Resource | null;  // Uses wrong nested type
}

Workaround - Manually add the import and fix the type:

import type { Resource as _example_Resource } from './Resource';

export interface Service {
  resource?: _example_Resource | null;  // Fixed
}

Environment

Reproduced with @grpc/proto-loader@0.7.12 and @grpc/proto-loader@0.8.0 on Ubuntu 22.04 on WSL2 (x86_64) with multiple 16+ Node versions. Node is installed using nvm

Additional context

Python and Go generators seem to handle this correctly:

# Python (protoc)
from example import common_pb2 as _common_pb2
class Service(_message.Message):
    resource: _common_pb2.Resource  # Correct
// Go (protoc-gen-go)
type Service struct {
    Resource *Resource  // Correct
}
// TypeScript (proto-loader-gen-types)
resource?: _example_Service_Status_Metric_Resource | null;  // Wrong

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions