Wrong TypeScript type used for fields when name collision exists between imported and nested messages
- Dominant language
- TypeScript
- Stars
- 4.8k
- Forks
- 716
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 10
Description
### 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:**
```bash
npx proto-loader-gen-types --longs=String --enums=String --defaults --oneofs -I . --outDir=. common.proto service.proto
```
**common.proto:**
```protobuf
syntax = "proto3";
package example;
message Resource {
int64 id = 1;
string name = 2;
}
```
**service.proto:**
```protobuf
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:**
```typescript
// example/Service.ts
import type { Resource as _example_Resource } from './Resource';
export interface Service {
resource?: _example_Resource | null; // Uses imported type
}
```
**Actual:**
```typescript
// 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:
```typescript
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
# Python (protoc)
from example import common_pb2 as _common_pb2
class Service(_message.Message):
resource: _common_pb2.Resource # Correct
```
```go
// Go (protoc-gen-go)
type Service struct {
Resource *Resource // Correct
}
```
```typescript
// TypeScript (proto-loader-gen-types)
resource?: _example_Service_Status_Metric_Resource | null; // Wrong
```
Contributor guide
Assessment
This issue has not been assessed yet.