question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript type generation wrong when using multiple files

See original GitHub issue

I’m currently migrating my GRPC service declarations from JS to Typescript and noticed that the code generation is wrong when referencing messages/types from other files.

Given the code below, the following MyServiceServiceClientPb.ts is generated:

...
  methodInfoaddOne = new grpcWeb.AbstractClientBase.MethodInfo(
    MyMessagesFile_pbInteger,
    (request: MyMessagesFile_pbInteger) => {
      return request.serializeBinary();
    },
    MyMessagesFile_pbInteger.deserializeBinary
  );
...

Note the correct type is MyMessagesFile_pb.Integer (the dot is missing)!

This is printed in grpc_generator.cc:713.


Using libprotoc 3.11.1 and grpc-web 1.0.7

MyServiceFile.proto

syntax = "proto3";
import "MyMessagesFile.proto";

service MyService {
  rpc addOne(Integer) returns (Integer);
}

MyMessagesFile.proto

message Integer {
  int32 number = 1;
}

package.json

{
  ...
  "scripts": {
    ...
    "grpc-generate": "mkdir -p src/proto && protoc -I=/path/to/proto/folder /path/to/proto/folder/*.proto --js_out=import_style=commonjs:src/proto --grpc-web_out=import_style=typescript,mode=grpcwebtext:src/proto && for f in src/proto/*.js; do sed -i '1s;^;/* eslint-disable */;' $f; done",
    "grpc-generate-macos": "mkdir -p src/proto && protoc -I=/path/to/proto/folder /path/to/proto/folder/*.proto --js_out=import_style=commonjs:src/proto --grpc-web_out=import_style=typescript,mode=grpcwebtext:src/proto && for f in src/proto/*.js; do sed -i '' '1s;^;/* eslint-disable */;' $f; done",
    ...
  }
  ...
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:8
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
Jdbancommented, Mar 10, 2020

Affecting me too… can you share your sed expression @Jdban?

Sure, it’s not very generic though. It’s just doing an explicit find/replace on the string in all the .ts files.

in our bash script:

# Handles this annoying bug: https://github.com/grpc/grpc-web/issues/693
ORIGINAL=shared_pbFilterRequest
REPLACEMENT=shared_pb.FilterRequest
grep -l -E "$ORIGINAL" **/*.ts  | xargs sed -i.bak "s#$ORIGINAL#$REPLACEMENT#g"
rm -rf js/*.bak   # Our files are in a js folder
0reactions
rieshycommented, Jul 31, 2021

Unfortunately the fix does not seem to cover the “enum” entries defined in the imported proto. The “.” is still missing for them using release 1.2.1

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I split a TypeScript class into multiple files?
I have found many times that partial classes separated across many files are very useful. this is true primarily when you have a...
Read more >
TSConfig Reference - Docs on every TSConfig option
Intro to the TSConfig Reference. A TSConfig file in a directory indicates that the directory is the root of a TypeScript or JavaScript...
Read more >
TypeScript library tips: Rollup your types! | by Martin Hochel
There are various options on the market, most familiar are probably webpack and ... By default, TypeScript wont emit declaration files.
Read more >
Generate GraphQL Types with Apollo Codegen Tutorial
TypeScript GraphQL Code Generator – Generate GraphQL Types with Apollo ... our queries and mutations to tell if we're doing something wrong.
Read more >
TypeScript | WebStorm Documentation - JetBrains
TypeScript files are marked with the TypeScript file ... completion for keywords, labels, variables, parameters, and functions, error and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found