GithubHelp home page GithubHelp logo

Comments (3)

timostamm avatar timostamm commented on June 2, 2024 1

Hey Azuka, the initializer does not accept string values for enum. I'd expect a type error in your call:

const list = new ProjectServiceListRequest({sort: 'PROJECT_SORT_JOB_NUMBER'});
                                                  ^ TS2322: Type string is not assignable to type ProjectSort

v1.7.2 and earlier let the string value through if the type error is ignored, and they use it as is for JSON, without a runtime check.

It is correct that the behavior for this usage is broken with v1.8.0, which adds an assertion. But I'm afraid it never was supported behavior. It may have worked in this specific situation, but only by accident, and you will run into undefined behavior in other places when ignoring the type error when constructing the message.

We'll try to find a better solution for this use case, but going through proto3.getEnumType is the correct solution for now. Alternatively, you could also use field info (see #738).

from protobuf-es.

Azuka avatar Azuka commented on June 2, 2024

@timostamm thanks. I didn't realize that was incorrect behavior.

I've been using the enum string values in drop downs (because they're easier to read and troubleshoot than numbers).

from protobuf-es.

timostamm avatar timostamm commented on June 2, 2024

Yes, numeric enums are not ideal for this case. But you can use the names of the generated enum. For example, with this Protobuf enum (source):

enum PrefixEnum {
  PREFIX_ENUM_ZERO = 0;
  PREFIX_ENUM_ONE = 1;
}

The generated TypeScript enum is:

export enum PrefixEnum {
  ZERO = 0,
  ONE = 1,
}

TypeScript enums allow lookup from string or from number, and we can get a type for the string names with keyof typeof:

const names: keyof typeof PrefixEnum = ["ZERO", "ONE"];

There are several ways to get the string values for the array, but getEnumType is quite simple:

const names = proto3.getEnumType(PrefixEnum).values
  .map(v => v.localName) as (keyof typeof PrefixEnum)[];

You can use these names to populate a drop-down.

Then to set the value on the Protobuf message, you can convert the string name to the numerical value using the enum:

const selectedName = names[0];
myMessage.enumField = PrefixEnum[selectedName];

from protobuf-es.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.