HACKER Q&A
📣 alexbezhan

Is it possible to compile TypeScript to Golang?


Typescript lets me write the complex logic, but the Node concurrency model being single-threaded is very limited.

So ideally I would want to have Typescript running in Golang. Is there such a possibility?


  👤 pc_edwin Accepted Answer ✓
This is not directly related to question but I would suggest using go just for those use cases.

Programming in general has become a lot more abstract and standardised so with some exceptions (C/rust) you can easily use multiple languages without incurring much if any costs.

Something else you can do thats slightly unorthodox is to write in typescript then ask chatGPT to rewrite your code in golang. You still have to maintain in golang but you get do the initial implementation part in typescript.


👤 fathyb
No, the Go threading runtime is fundamentally incompatible with JavaScript's event-loop model. TypeScript does not define runtime semantics, JavaScript does, and it's built to run in a single-threaded environment. You cannot use it to leverage Go's coroutines unfortunately.

👤 thesuperbigfrog
It sounds like you need a language that supports stronger types and concurrency.

There are many good programming languages that might work if neither Go nor Typescript individually would meet your needs.

I recommend examining the following to see if they would work better for you:

Ada: https://learn.adacore.com/courses/intro-to-ada/index.html

Elixir: https://elixir-lang.org/

Java: https://docs.oracle.com/javase/tutorial/

Rust: https://doc.rust-lang.org/book/

All of them have excellent support for concurrency and strong typing to various degrees that help you to write complex logic quickly.


👤 from
Why are routine technical questions constantly appearing on HN? Ask on a specific subreddit Reddit or Stackoverflow.

👤 aristofun
Ts is dynamically typed unfortunately. And also this implies the whole js library to be made thread safe.

But the good thing is A. Horizontal scalability is far better approach for most usecases anyway B. Nodejs is so performant youd rarely beat it with other GC languages in real applications.


👤 patrickthebold
It would be helpful to know what specific problems you have with the single threaded model. You might still have some options staying with node. I think you can have worker threads but maybe you can't share memory, or you might be able to run multiple node processes.

Otherwise you probably want to just switch to go.


👤 techn00
Write concurrent heavy tasks in Go. Write other tasks in typescript.

Use https://connect.build to connect them.


👤 Alifatisk
I don't think it's possible to run Ts in Golangs compiler. Have you considered rewriting your code to Go?

👤 crop_rotation
No, it is not (nor does it seem feasible). Why not just spin up multiple workers?

👤 bkq
>Typescript lets me write the complex logic

What is it about Go that prevents you from writing this complex logic?