creditkarma / thrift-typescript

Generate TypeScript from Thrift IDL files
Apache License 2.0
155 stars 32 forks source link

Render thrift service without namespace #142

Closed hayes closed 5 years ago

hayes commented 5 years ago

It would be really great if there were a way to remove typescript namespaces from the generated code. namespaces are not properly supported in vscode, and are being phased out in a lot of libraries.

My specific use cases for wanting to remove namespaces is that my company has standardized our js/ts tooling around babel 7, and babel does not, and will not support typescript namespaces. Currently we use thrift compiler for compiling node services that use thrift, but everything else (front end and backend) uses babel and we would prefer to be able to take full advantage of our normal toolset with our thrift services.

I have not dug into this too deeply, but one solution which seems like it would be relatively easy to implement would be to add a new option that would render services into a new ts file. This file would contain the exact same content/exports that the current service namespace, so the rendering code would not need to change much other than switching out the target for where the code needs to be rendered.

Manually making these changes was easy enough, pasting the content of a service namespace into a new file, adding the correct imports for thrift namespaces at the top of the file, and replacing the previous service namespace with:

import * as ServiceName from './ServiceName.ts'

export { ServiceName }

everything seemed to work.

I'd be happy to implement this approach if this seems like a good direction.

kevin-greene-ck commented 5 years ago

I think this is a reasonable approach. I've actually been wanting to split up the generated code for a while, where every value/type would render into its own file and just be reexported through index.ts. I see your POC PR and will give it a look over as soon as I can.