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

Long not imported in generated TypeScript static code #940

Closed
cusher opened this issue Nov 14, 2017 · 6 comments
Closed

Long not imported in generated TypeScript static code #940

cusher opened this issue Nov 14, 2017 · 6 comments
Labels

Comments

@cusher
Copy link

cusher commented Nov 14, 2017

protobuf.js version: 6.8.0

After generating static code with pbjs and pbts:

node_modules/protobufjs/cli/bin/pbjs -t static-module -w commonjs -o types.js --keep-case
node_modules/protobufjs/cli/bin/pbts -o types.d.ts types.js

I import from the generated typescript file:

//Inside my typescript file:
import {namespacewithtypes} from "../types";

Upon compilation of my TypeScript file, I get errors about not being able to find Long, e.g:

ERROR in /mydevpath/types.d.ts (5846,52): Cannot find name 'Long'.

The errors go away if I modify types.d.ts to add an import near the top:

import {Long} from "protobufjs";

However, adding that line every time one of the proto files is modified won't be practical in the long term.

Sorry if this isn't a bug and I'm just misunderstanding something.

@calvellido
Copy link

calvellido commented Nov 20, 2017

I think that it will depend on the way you are building your application, and the way you are using Protobuf.js and/or Long.js, but you shouldn't need to add the types on each d.ts file generated out from a proto descriptor.

In fact, by adding the type info to your d.ts you are just hiding the problem, but probably the Long is not being processed anyway, though the compiler will be happy for the time being.

I use Protobuf.js in a TypeScript env, using static generated code, which is bundled later by Webpack (specifically, this is an Angular project). By reading the Browserify integration section, you can read that if int64 support is required, explicitly require the long module somewhere in your project as it will be excluded otherwise.

The main thing to have in mind is that Protobuf.js will try to do a require (this is done through inquire, which I'm not sure why is necessary though) for Long. If this process can't be completed for whatever reason regarding your environment, you have to specify yourself.

If you have a general proto management TS file maybe you could add these lines there.

import * as protobuf from 'protobufjs/minimal';
import * as Long from 'long';
protobuf.util.Long = Long;
protobuf.configure();

Which would be the equivalent of doing something like this in a require environment:

var $protobuf = require("protobufjs/minimal");
var Long = require("long");
$protobuf.util.Long = Long;
$protobuf.configure();

Just my two cents, I could have some misconception too though.

@cusher
Copy link
Author

cusher commented Nov 22, 2017

Thank you! I'll be honest, I totally skipped over that part of the documentation since it's under the same heading as the stuff about building the library yourself.

@khawaga
Copy link

khawaga commented Dec 16, 2017

  1. Add long and @types/long to your dependencies.
  2. Add "long" to your tsconfig's types property. (typeRoots should already be set to "node_modules/@types")

@rynop
Copy link

rynop commented Nov 20, 2018

For those who run into this with Angular:

I use a google.protobuf.Timestamp type in my .proto file. pbts implements this as a number | Long | null. The Long surfaced the problem brought up in this issue:

export namespace google {
  namespace protobuf {
    interface ITimestamp {
      seconds?: number | Long | null;
      nanos?: number | null;
    }
...

I had to yarn add -dT long @types/long and add "long" to the types list in tsconfig.app.json Angular file.

@sandogeek
Copy link

If you are using angular, you can insert these code to main.ts.

import * as protobuf from 'protobufjs/minimal';
import * as Long from 'long';


if (environment.production) {
  enableProdMode();
}
protobuf.util.Long = Long;
protobuf.configure();

It works fine for me.

@JustinBeckwith
Copy link
Contributor

Greetings folks! I think we fixed this over in #1166

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

No branches or pull requests

6 participants