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

Use ES6 import for Long in pbts output #1814

Closed
nmccrea opened this issue Sep 23, 2022 · 3 comments
Closed

Use ES6 import for Long in pbts output #1814

nmccrea opened this issue Sep 23, 2022 · 3 comments

Comments

@nmccrea
Copy link

nmccrea commented Sep 23, 2022

protobuf-cli version: 1.0.2

pbts output includes the line:

import Long = require("long");

Babel subsequently chokes on this line:

SyntaxError: .../protobufs.d.ts: `import Long = require('long')` is not supported by @babel/plugin-transform-typescript
Please consider using `import Long from 'long';` alongside Typescript's --allowSyntheticDefaultImports option.

Manually changing this line to import Long from "long"; makes Babel happy.

I'd like to keep my Babel configuration the way it is. Is there any way to tell pbts to use ES6 syntax for this line? Alternatively, is there a way to tell it to omit this line?

@Zsar
Copy link

Zsar commented Sep 23, 2022

FWIW: There seems to be a regression here:

  • protobufjs 6.11.3: This line does not get emitted for me.
  • protobufjs 7.1.1 + protobufjs-cli 1.0.2: This line does get emitted for me, even though it is unused.

@Zsar
Copy link

Zsar commented Sep 23, 2022

Cause found: #1802 just tacks on the import. It does not check whether the import is used.

... Reading up on import-require, it looks like that syntax should be legal (if uncommon). So I'd think Babel should consume it? Unsure.

@nmccrea
Copy link
Author

nmccrea commented Sep 26, 2022

@Zsar awesome thanks for taking a look it helped me a lot.

After a bit more time digging into this, it finally dawned on me that Babel should not be consuming this file at all. I have solved the issue by excluding .d.ts files from Babel.

A bit more info before I close this:

I am using the common setup in which TypeScript's tsc is used for type-checking and linting, but not for transpilation to JavaScript. Instead, Babel does the transpilation using its robust TypeScript support.

The import = syntax is a valid syntax in TypeScript. However it is not supported by @babel/plugin-transform-typescript (see Caveat 2). Therefore, when the .d.ts file generated by pbts is consumed by Babel (rather than tsc), it cannot transpile this line.

However, Babel does not do any type checking. It expects you to have validated your types separately. As a result .d.ts files, which are intended to provide type information only, are not the domain of Babel, and are not meant to be consumed.

As a result, this line should not be a problem whether you are using Babel or tsc for transpilation.

  • If you are using Babel (like me), the file can be ignored, for example passing --ignore '**/*.d.ts* to the babel command.
  • If you are using tsc, it should be able to parse this import statement without a problem.

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