thesayyn / protoc-gen-ts

Compile protocol buffer messages to TypeScript.
MIT License
360 stars 74 forks source link

Repeated enum results in type error. #171

Closed jereklas closed 1 year ago

jereklas commented 2 years ago

Repeated enums result in typescript errors on the generated files. The following proto file:

syntax = "proto3";

enum E {
    One = 0;
    Two = 1;
}

message Test {
    repeated E Enums = 1;
}

Results in the following generated TS:

    get Enums() {
        return pb_1.Message.getFieldWithDefault(this, 1, E.One) as E[];
    }

Which contains the following error:

Conversion of type 'import("/path").E' to type 'E[]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.ts(2352)

The underlying implementation of it appears to be working fine? it's just typed incorrectly.

martyphee commented 2 years ago

I seem to be getting hit by this issue also.

Is there any workaround for it?

src/company/protobuf/policies/v2/policy.ts:2922:20 - error TS2352: Conversion of type 'import("develop/company/protobuf/grpc_tools_node_protoc_ts/dist/src/company/protobuf/policies/v2/policy").company.protobuf.policies.v2.MotorClassOfUse' to type 'MotorClassOfUse[]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.

2922             return pb_1.Message.getFieldWithDefault(this, 1, MotorClassOfUse.MOTOR_CLASS_OF_USE_UNSPECIFIED) as MotorClassOfUse[];
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/company/protobuf/policies/v2/policy.ts:2928:20 - error TS2352: Conversion of type 'import("develop/company/protobuf/grpc_tools_node_protoc_ts/dist/src/company/protobuf/policies/v2/policy").company.protobuf.policies.v2.MotorPeril' to type 'MotorPeril[]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.

2928             return pb_1.Message.getFieldWithDefault(this, 2, MotorPeril.MOTOR_PERIL_UNSPECIFIED) as MotorPeril[];
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/company/protobuf/policies/v2/policy.ts:3052:20 - error TS2352: Conversion of type 'import("develop/company/protobuf/grpc_tools_node_protoc_ts/dist/src/company/protobuf/policies/v2/policy").company.protobuf.policies.v2.MotorClassOfUse' to type 'MotorClassOfUse[]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.

3052             return pb_1.Message.getFieldWithDefault(this, 1, MotorClassOfUse.MOTOR_CLASS_OF_USE_UNSPECIFIED) as MotorClassOfUse[];
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
thesayyn commented 2 years ago

Is there any workaround for it?

Not yet. Though this is trivial to fix. I just didn't have the time to work on it myself.

martyphee commented 2 years ago

If you can point me to the general area I can take a look. Not really a TS developer. Trying to create a POC using our protobuf library in a nextjs project. The protobuf library is quite large. We have scala, .net and python libraries generated. I was trying to add TS.

martyphee commented 2 years ago

In other news. I changed the code above and everything worked when calling our gRPC service. :-)

jereklas commented 2 years ago

My solution was to change the proto definition to not use an enum. When this gets fixed we’ll reconsider the enum solution

tsawada commented 1 year ago

@thesayyn Thank you so much for merging so quickly! It'd be great if you could push a new release to NPM... I'm now aware of an easy way to use the latest HEAD.

Thanks in advance.

thesayyn commented 1 year ago

@thesayyn Thank you so much for merging so quickly! It'd be great if you could push a new release to NPM... I'm now aware of an easy way to use the latest HEAD.

Thanks in advance.

https://www.npmjs.com/package/protoc-gen-ts/v/0.8.6-rc1