Typescript: Any plan to compile TypeScript to C#, .NET CLR or other targets

Created on 19 Dec 2018  路  8Comments  路  Source: microsoft/TypeScript

Search Terms

Compile, C#, CSharp, .NET, Mono, WebAssembly

Suggestion

As I said, I hope that Typescript can compile to more targets, such as C#.
(I found a related issue, but it closed. (#9202))

Use Cases

As we all know, C# can be compiled into JavaScript (by Bridge.NET), and it can also be compiled to multiple platforms. Although TypeScript is just a superset of JavaScript, it is really another language. If it can achieve faster performance with a more static type than JavaScript, why not compile it directly, as we did with C#.

Although TypeScript was born to serve JavaScript, I think it can go further because it already has a huge user base.

Early TypeScript type support is not as good as C#, no int, etc., but as a superset of Javascript, you can completely refine the number type. It seems that 3.0 has added support like BigInt, (#4639 , #15096 ) which is a big step forward. And more types support greatly improve the performance optimization compiled to other platforms, and further improve the robustness, just as TypeScript's subset AssemblyScript uses the bytecodes like i32 and f32 for compiling to WebAssembly type.

Also, I think that the typescript should be able to AoT compile the platform on the mobile side to achieve near-native speed.

Moreover, TypeScript has a lot to do with C#. I think, borrowing C#'s compilation ideas shouldn't be difficult. I found something like this michaelcheers/TypescriptToCS2.

What triggered me was the appearance of Blazor. But it is to serve people who are familiar with C# and let them turn to the frontend. However, TypeScript seems to be serving the frontend. It doesn't seem necessary to compile TypeScript like Blazor to compile C# to wasm.

However, already with Bridge.NET compiling C# into javascript, why do they have to compile C# into WebAssembly? Actually, I didn't think about the reason very carefully, but it made me wonder if TypeScript can do it too?

Perhaps if TypeScript has more compilation targets, it will become more versatile. Frontend engineers can bring their knowledge to every part of the system. And it can also attract other types of engineers. After all, cross-platform and faster than JavaScript is undoubtedly very good. There are already many engineers who use TypeScript instead of JavaScript, right ?

Examples

Compile TypeScript to wasm. In general, compiling JavaScript to wasm seems to be a lot of work, and it's inefficient. But TypeScript has static types, even if it is doped with some dynamic types like 'any', but the speed should be faster. Like Blazor's ability to interoperate with JavaScript, replacing C# with TypeScript should be more comfortable because TypeScript is inextricably linked to JavaScript.
(Forgive me for always mentioning wasm and some frontend related things, because I am very interested in these. But TypeScript should be useful in other compiled targets.)

Checklist

My suggestion meets these guidelines:

  • [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [x] This wouldn't change the runtime behavior of existing JavaScript code
  • [x] This could be implemented without emitting different JS based on the types of the expressions
  • [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • [ ] This feature would agree with the rest of TypeScript's Design Goals.

Written at the end

Maybe a TypeScript to C# converter is enough. But the converter I mentioned just now seems to be no longer updated. And a converter is far less efficient than direct compilation. I sincerely hope that TypeScript can add this feature. This will be a huge convenience.

Out of Scope Suggestion

Most helpful comment

Searched for this feature because, with the advent of Blazor, there are lots of available components written in typescript today but no way to leverage them from a blazor app (you can write an interop layer but it's not pretty). If there was some way to convert the typescript source to c# I thought that might be a useful start on the journey to reusing stuff within a blazor app. Reading the above I understand this is not a goal of typescript but just wanted to add why I was looking for this feature.

All 8 comments

This doesn't fit to typescipt's goals. If you want c# go write c# actually its much more efficient instead of transforming typescript into c#. what can be done is multiple target. Like some hot parts of code can be targeted as wasm and other parts plain old js this would improve lots of things actually but again wasm is not exactly catching up yet. And i think u should recheck your check list.

This doesn't fit to typescipt's goals. If you want c# go write c# actually its much more efficient instead of transforming typescript into c#. what can be done is multiple target. Like some hot parts of code can be targeted as wasm and other parts plain old js this would improve lots of things actually but again wasm is not exactly catching up yet. And i think u should recheck your check list.

@kaaninel

You're right.But what I actually think is to use C# as an intermediate language. After all, there is a little bit of ready-made stuff. But with TypeScript updates, the converter is more complicated. Direct compilation to multiple targets is undoubtedly correct.

Does the 'out of scope' tag mean that multi targets support is impossible to be added ? Or just not goal now ?

I changed the last item of the checklist to be not satisfied. Is it a right ?

See #5200 for similar discussion.

@DanielRosenwasser It is analogous. But the answer is a little different from my thoughts. I hope to compile directly to multiple platforms, not using javascript as an intermediary. Unfortunately, I can't search out for related project that directly compiles ts.
Maybe AoT compilation or pre-release compilation will greatly promote TypeScript.
Thought this idea is out of scope, I still hope that more people can pay attention to this idea.

@HighCWu yes it would be great to have Typescript supported in CLR.
Typescript would be easily compile in to WebAssambly and using blazor it would be great.

Compilation targets outside of JS are very much outside our design goals. You might try https://github.com/JavascriptNet/Javascript.Net if you want to run JS code inside a .NET program

Searched for this feature because, with the advent of Blazor, there are lots of available components written in typescript today but no way to leverage them from a blazor app (you can write an interop layer but it's not pretty). If there was some way to convert the typescript source to c# I thought that might be a useful start on the journey to reusing stuff within a blazor app. Reading the above I understand this is not a goal of typescript but just wanted to add why I was looking for this feature.

A little off-topic: i was wondering if there is a typescript to javascript transpiler that does not depend on node.js (nor deno) but depends only on .NET Core?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

weswigham picture weswigham  路  3Comments

CyrusNajmabadi picture CyrusNajmabadi  路  3Comments

siddjain picture siddjain  路  3Comments

blendsdk picture blendsdk  路  3Comments

jbondc picture jbondc  路  3Comments