Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

TS declarations files are missing enums #403

Closed
serchavalos opened this issue Dec 3, 2018 · 2 comments 路 Fixed by #404
Closed

TS declarations files are missing enums #403

serchavalos opened this issue Dec 3, 2018 · 2 comments 路 Fixed by #404

Comments

@serchavalos
Copy link

Hello 馃憢!

I noticed that Enum types are missing in the generated .d.ts files. This is an example

syntax = "proto3";

enum Examples {
  CASE_1 = 0;
  CASE_2 = 1;
  CASE_3 = 2;
  CASE_4 = 3;
  CASE_5 = 4;
}

This file will generated the JS but not the declaration file

/**
 * @fileoverview
 * @enhanceable
 * @suppress {messageConventions} JS Compiler reports an error if a variable or
 *     field starts with 'MSG_' and isn't a translatable message.
 * @public
 */
// GENERATED CODE -- DO NOT EDIT!

var jspb = require('google-protobuf');
var goog = jspb;
var global = Function('return this')();

goog.exportSymbol('proto.Examples', null, global);
/**
 * @enum {number}
 */
proto.Examples = {
  CASE_1: 0,
  CASE_2: 1,
  CASE_3: 2,
  CASE_4: 3,
  CASE_5: 4
};

goog.object.extend(exports, proto);

I made sure to run the command with the argument --grpc-web_out=import_style=commonjs+dts

Thanks in advance

@rogchap
Copy link
Contributor

rogchap commented Dec 3, 2018

Enum types are generated if they are nested (which was fixed in #385), but not at the top level.

@rogchap
Copy link
Contributor

rogchap commented Dec 3, 2018

So after a bit more digging I see that TypeScript definitions are only generated for Message types that are declared in a service method; hence Messages that are not used in a gRPC service (that are not nested) or top level Enum never get generated.

The main function in question is GetAllMessages(): https://github.com/grpc/grpc-web/blob/master/javascript/net/grpc/web/grpc_generator.cc#L344

I can understand why it might not be required to generate Messages that are not used (why else would they be there for?), but Enums are different.

A more complete example of the issue:

syntax = "proto3";

// This enum will not be defined in TypeScript
enum Status {
  UNKNOWN = 0;
  HAPPY = 1;
  SAD = 2;
}

service Greeter {
  // Defined in TypeScript
  rpc SayHello (HelloRequest) returns (HelloReply);
}

// Defined in TypeScript
message HelloRequest {

 // Defined in TypeScript, even though it's not used, but still nested in a Message that is
  enum NestedStatus {
    UNKOWN = 0;
    GOOD = 1;
    BAD = 2;
  }

  string name = 1;
}

// Defined in TypeScript
message HelloReply {
  string message = 1;
  Status status = 2;
}

// NOT Defined in TypeScript
message AnotherMessage {
  string msg = 1;
}

What harm is there to define all messages and enums, even if they are not used in the service definition?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants