Protobuf.js: Long not imported in generated TypeScript static code

Created on 14 Nov 2017  路  6Comments  路  Source: protobufjs/protobuf.js

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.

bug

Most helpful comment

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.

All 6 comments

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.

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.

  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")

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.

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.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

b1naryMan picture b1naryMan  路  4Comments

chloe2018s picture chloe2018s  路  3Comments

ArvoGuo picture ArvoGuo  路  4Comments

ghost picture ghost  路  3Comments

zD98 picture zD98  路  3Comments