ipfs / ipfs/protons

Type name conflict when enum is named `Error`

Open
#64 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
38
Forks
25
Avg merge
1h 4m
Merged PRs (30d)
3

Description

Using the following protobuf definition:

```
message HistoryResponse {
enum Error {
ERROR_NONE_UNSPECIFIED = 0;
ERROR_INVALID_CURSOR = 1;
}
optional Error error = 4;
}
```

The TypeScript generated has the following typescript error:

```
src/proto/store_v2beta3.ts:364:21 - error TS2351: This expression is not constructable.
Type 'typeof Error' has no construct signatures.

364 throw new Error('Protocol error: required field "messages" was not found in object')
~~~~~

src/proto/store_v2beta4.ts:373:21 - error TS2351: This expression is not constructable.
Type 'typeof Error' has no construct signatures.

373 throw new Error('Protocol error: required field "messages" was not found in object')

```

This is because typescript thinks that `Error` refers to the enum and not to the native type `Error`:

```
export namespace HistoryResponse {
export enum Error {
ERROR_NONE_UNSPECIFIED = 'ERROR_NONE_UNSPECIFIED',
ERROR_INVALID_CURSOR = 'ERROR_INVALID_CURSOR'
}

enum __ErrorValues {
ERROR_NONE_UNSPECIFIED = 0,
ERROR_INVALID_CURSOR = 1
}

export namespace Error {
export const codec = () => {
return enumeration(__ErrorValues)
}
}

let _codec: Codec

export const codec = (): Codec => {
if (_codec == null) {
_codec = message((obj, writer, opts = {}) => {
if (opts.lengthDelimited !== false) {
writer.fork()
}

if (obj.messages != null) {
for (const value of obj.messages) {
writer.uint32(18)
WakuMessage.codec().encode(value, writer)
}
} else {
throw new Error('Protocol error: required field "messages" was not found in object')
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.