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

Is it possible to use 'import_style' for Typescript #69

Closed
wiewioraseb opened this issue Jun 6, 2018 · 9 comments
Closed

Is it possible to use 'import_style' for Typescript #69

wiewioraseb opened this issue Jun 6, 2018 · 9 comments
Labels
enhancement New feature or request javascript triaged Issue has been triaged

Comments

@wiewioraseb
Copy link

I want to have access to types when I get statically generated protobuff files.

Running code:
protoc -I=Protos --js_out=import_style=es6,binary:Javascript Protos/MessageEvnelope.proto Protos/Messages.proto

Generates .js files that are not easily imported in Typescript. When:
import { SomeMessage } from './Messages_pb';
I got error:

Could not find a declaration file for module './Messages_pb' ... implicitly has an 'any' type.

To overcome this, I have to use require('./Messages_pb') which results with no types, which does not satisfy me.

Is there an automatic way to create types for such generated js file? Or some other way to have types in typescript?

@acozzette acozzette added enhancement New feature or request javascript labels Jun 18, 2018
@acozzette
Copy link
Member

I'm afraid we don't have much experience with TypeScript and so I'm not sure of the best way to handle that use case well. If you have any ideas then we would probably be open to taking pull requests, though.

@xfxyjwf xfxyjwf self-assigned this Aug 22, 2018
@dominikeinkemmer
Copy link

@wiewioraseb we currently use this plugin to generate the typescript typings, maybe it helps you?
It basically generates .d.ts files for all javascript files.
https://github.com/improbable-eng/ts-protoc-gen

@clehene
Copy link

clehene commented Feb 18, 2019

@dominikeinkemmer would it make sense to name the issue "TypeScript Support"?

@acozzette note that grpc-web already implemented Typescript support - see here https://github.com/grpc/grpc-web#typescript-support

@dominikeinkemmer In order to get the definitions working it's possible to just create a dummy gRPC service that depends on some protobuf hierarchy and using

import_style=commonjs+dts: existing CommonJS style stub + .d.ts typings
import_style=typescript: full TypeScript output

will work.
My suggestion is to start from the existing plugin (https://github.com/grpc/grpc-web/blob/master/javascript/net/grpc/web/grpc_generator.cc) or from one of the various implementations out there, but regardless it should be consistent between protocol buffers and grpc at least.

As an additional note, it would also be nice to support a fluent API and have some thinking around JS / TS number type vs the variety that protobuf supports.

Reference

https://github.com/dcodeIO/ProtoBuf.js/#usage-with-typescript
https://github.com/improbable-eng/ts-protoc-gen
https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/google-protobuf/google/protobuf

@clehene
Copy link

clehene commented Feb 18, 2019

This is related / duplicates #98

@acozzette
Copy link
Member

I haven't worked on Javascript in a while but @dankurka and @TeBoring might have thoughts on this.

@grzpotry
Copy link

grzpotry commented Oct 6, 2019

Is workaround with gRPC service still necessary just to import generated files with type definitions to typescript ?

@thesayyn
Copy link

thesayyn commented Dec 4, 2019

For this particular problem, we have created a plugin that generates typescript sources. You do not need to depend on js generator or ts-protoc-gen. It is just pure typescript sources that work out-of-the-box.

This might solve your problem.

https://github.com/thesayyn/protoc-gen-ts

@chris-kruining
Copy link

I've made a PR for "native" Typescript support over at protocolbuffers/protobuf#9412.

Any help is more than welcome, I am way out of my depth in c++

@acozzette acozzette transferred this issue from protocolbuffers/protobuf May 16, 2022
@dibenede dibenede added the triaged Issue has been triaged label Sep 23, 2022
@dibenede
Copy link
Contributor

Duplicate of #98

@dibenede dibenede marked this as a duplicate of #98 Sep 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request javascript triaged Issue has been triaged
Projects
None yet
Development

No branches or pull requests

10 participants