Comments (9)
Hi @gnzzz yep, I think a plugin option is a good idea. There is an option already called context
that shows how some of that works. Are you up for submitting a PR around that? I could give a few hints but in theory it shouldn't be too bad..
from ts-proto.
I got an initial stab working for the option at https://github.com/gnzzz/ts-proto . It feels like I'm butchering the ts-poet statements though, so any pointers there would be helpful.
The output looks pretty good at the moment, and I will write a simple test in the same way as the other. If there are any other scenarios that I should test, please let me know.
from ts-proto.
@gnzzz this looked really good! I went ahead and merged it.
I was seeing some test/compile errors; can you check out this commit: 5706ff9
...what's the thing with import * as Long from "long"
vs. import Long from "long"
? I was pretty sure one of the tsconfig values, like esModuleInterop
or allowSyntheticDefaultImports
allowed always using the 2nd version...
Should we change the code generated output to use import Long from "long"
? For that commit above, I just did it by hand to see if it'd work / get the tests to pass.
from ts-proto.
...what's the thing with
import * as Long from "long"
vs.import Long from "long"
? I was pretty sure one of the tsconfig values, likeesModuleInterop
orallowSyntheticDefaultImports
allowed always using the 2nd version...
Yes, you're right. esModuleInterop
set to true
( or allowSyntheticDefaultImports
depending on how you see it ) allows you to use import Long from "long"
for any commonjs package. My understanding, which might be wrong, is that import *
will be a namespace import. That is to say if it's a class like structure, using that is safe on any legacy setting of typescript as long as you treat it as a class. import Long from "long"
is a default import and will only work if the package has .default
export. This is where esModuleInterop
comes in as the compiler will automatically convert any import that does not have a default
to use the top level export as default
.
The problem here is that a package normally has the luxury to decide this internally, but ts-proto
is a bit special as it ejects code that will be used by other projects. Since esModuleInterop
was not true
by default a couple of years ago then this will force developers to change.
Interestingly, one of our projects doesn't have this setting and with the import Long from "long"
the compiler will error with TS1192: Module node_modules/@types/long/index has no default export.
Was the error in CircleCI? If that's the case, I think that leaving it as import *
and then using Long.fromNumber
as you did or Long.ZERO
should work?
from ts-proto.
I can experiment a bit with this tomorrow if you can enable CircleCI for PRs or forks. I don't have any experience with Circle but seems like a small configuration change would allow it.
from ts-proto.
@gnzzz ah nice, that was a great explanation, particularly that downstream projects may/may not have esModuleInterop set. I've pushed a commit that removes esModuleInterop
from the tsconfig.json
and changes back to use import *
for both long as well as dataloader and object-hash, which whether other packages I was cheating a bit on and leaning on the esModuleInterop
flag.
I believe everything should be good now?
Take a look at the last few commits and let me know, and then I'll push out a new release.
I also enabled the "build forks" setting in CircleCI, afaiu...
Thanks!
from ts-proto.
@stephenh that looks great, just tried the master
version and the output works perfectly. Thank you very much.
I'll put up a new PR with the documentation you requested shortly.
from ts-proto.
Great, I'll go ahead and close this out. Thanks again for the contribution!
from ts-proto.
I released v1.9.0 with your commits.
from ts-proto.
Related Issues (20)
- I'm using the buf tool to generate a ts file and have set `-ts_proto_opt=esModuleInterop=true`, but the generated code still contains `import Long = require("long");` HOT 1
- Module has already exported a member named 'ServerStreamingMethodResult' HOT 7
- basic_string::_M_construct null not valid HOT 5
- removeEnumPrefix not working HOT 1
- fromJSON should accept both snake_case and camelCase keys HOT 1
- Provide Options type for execution in TS HOT 1
- Add common types to their own file/export w/outputIndex option HOT 2
- Questions about defaults, optionals, arrays, null/undefined HOT 2
- Support `google.api` attributes/conventions HOT 1
- NestJS without Observables HOT 1
- meta generated for messages with oneOf properties is incorret for other properties HOT 1
- Add types for toJSON/fromJSON methods HOT 1
- TypeError on Runtime ChannelCredentials object HOT 1
- Value/ListValue do not include the required dependencies when using nestJs=true
- Error message type naming collision HOT 5
- Optionally sort keys in output interface types? HOT 2
- Should deprecated fields be optional? HOT 3
- How to use with node js HOT 1
- Wrapper Types conversion does not work for repeated field
- Oneof objects do not follow generated interface. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ts-proto.