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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--allowJs and generated protocol buffers #9509

Closed
tamird opened this issue Jul 4, 2016 · 7 comments
Closed

--allowJs and generated protocol buffers #9509

tamird opened this issue Jul 4, 2016 · 7 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@tamird
Copy link

tamird commented Jul 4, 2016

Currently, Google's protocol buffer compiler at https://github.com/google/protobuf/tree/master/js generates code that looks something like:

/**
 * @fileoverview
 * @enhanceable
 * @public
 */
// GENERATED CODE -- DO NOT EDIT!

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

goog.exportSymbol('proto.cockroach.util.hlc.Timestamp', null, global);


/**
 * Generated by JsPbCodeGenerator.
 * @param {Array=} opt_data Optional initial data array, typically from a
 * server response, or constructed directly in Javascript. The array is used
 * in place and becomes part of the constructed object. It is not cloned.
 * If no data is provided, the constructed object will be empty, but still
 * valid.
 * @extends {jspb.Message}
 * @constructor
 */
proto.cockroach.util.hlc.Timestamp = function(opt_data) {
  jspb.Message.initialize(this, opt_data, 0, -1, null, null);
};
goog.inherits(proto.cockroach.util.hlc.Timestamp, jspb.Message);
if (goog.DEBUG && !COMPILED) {
  proto.cockroach.util.hlc.Timestamp.displayName = 'proto.cockroach.util.hlc.Timestamp';
}

...
...
...

goog.object.extend(exports, proto.cockroach.util.hlc);

See also actual generated code.

I'm not really sure what the underlying cause is, but I can't seem to get any use out of the (presumed) types inferred from the JsDoc comments in this file.

By the way, is it possible to examine the result of --allowJs to see what the typescript compiler was able to infer?

@mhegazy
Copy link
Contributor

mhegazy commented Jul 5, 2016

I'm not really sure what's the underlying cause is, but I can't seem to get any use out of the (presumed) types inferred from the JsDoc comments in this file.

the system can not reason about such helper functions, e.g. goog.object.extend or goog.exportSymbol. so do not think you will get much out of it looking at this code.

By the way, is it possible to examine the result of --allowJs to see what the typescript compiler was able to infer?

open the file in VSCode, and hover over identifiers. it should show you what the TS type system thinks of them.

@tamird
Copy link
Author

tamird commented Jul 5, 2016

the system can not reason about such helper functions, e.g. goog.object.extend or goog.exportSymbol. so do not think you will get much out of it looking at this code.

Bummer. How do you feel about adding hooks for "teaching" TS about this class of helper e.g. via a definition file? (perhaps it's already possible to do this?)

open the file in VSCode, and hover over identifiers. it should show you what the TS type system thinks of them.

Is that the only way? Is there no way to do it outside of VSCode?

@mhegazy
Copy link
Contributor

mhegazy commented Jul 5, 2016

How do you feel about adding hooks for "teaching" TS about this class of helper e.g. via a definition file? (perhaps it's already possible to do this?)

sure. what kind of hooks? would be open to proposals.

Is that the only way? Is there no way to do it outside of VSCode?

you can use the compiler API and do it other forms. VSCode/Sublime/VS are just easier.

@evmar
Copy link
Contributor

evmar commented Jul 12, 2016

Within Google we use https://github.com/angular/clutz to generate TypeScript d.ts files from the Closure JS generated by the protocol compiler. Clutz hooks into the Closure compiler API so it understands the generated code well.

CC @rkirov

@tamird
Copy link
Author

tamird commented Jul 13, 2016

@evmar just to clarify, the process is:

  1. use protoc to generate Closure JS
  2. use clutz to generate typings from Closure JS
  3. import Closure JS via clutz-generated typings into TS code
  4. compile everything using Closure

Steps 3 and 4 are what's unclear to me. In particular, we are using SystemJS and plugin-typescript to do all our compilation in the browser. In other words, we really want to avoid having to run Closure on the output of the typescript compiler (because that's not really tractable). Is that possible?

@btgoodwin
Copy link

@evmar FWIW, +1 to this workflow problem.

I'm fighting my way through this as well in developing an ES2015 library that handles google protobufs. I've spent over a week trying to force protobufjs with @types/protobufjs only to have that route fall completely apart from endless packaging issues for how to deploy that library to the browser (tried jspm, webpack, systemjs).

I switched to google-protobuf only to find #1, I can't use import <whatever> = require('./module'); when my target module type is ES2015, so the CommonJS output is a no-go. #2, The Closure one is giving me similar problems as @tamird is showing. And those are the only two output options.

Is there a full, working example of this configuration anywhere in the wild that someone can reference?

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Apr 27, 2017
@mhegazy
Copy link
Contributor

mhegazy commented May 22, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed May 22, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants