Ask HN: Is it possible to compile TypeScript to Golang?

9 points by alexbezhan ↗ HN
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?

21 comments

[ 3.2 ms ] story [ 62.4 ms ] thread
>Typescript lets me write the complex logic

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

No, it is not (nor does it seem feasible). Why not just spin up multiple workers?
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.

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.

> Ts is dynamically typed unfortunately

What do you mean, specifically, with this statement? The point of TS is to give JS static types.

That you can’t transpile dynamic language into a static one.

If you call ts a statically typed language -> you dont know ts :)

(comment deleted)
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.
You can get the semantics of coroutines and no parallelism by setting GOMAXPROCS=1, either via environment variable or set in the program initialization.
It would remove the benefit of running on Go. It would be less efficient than v8 + libuv because your TypeScript on Golang runtime wouldn't be as fast as v8 + JIT to run the application code which is now written in a language built for dynamic dispatch.
I don't need Javascript runtime semantics. I just want to use Typescript.
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.

I don't think it's possible to run Ts in Golangs compiler. Have you considered rewriting your code to Go?
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.

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