7

When I first studied TypeScript, I found that node.js doesn't execute TypeScript, so you need to install a TypeScript compiler that converts your TypeScript code into JavaScript.

I searched until I found ts-node (TypeScript execution and REPL for node.js), but when I read the documentation I found that they do the same (here). Even deno (A modern runtime for JavaScript and TypeScript), is doing the same (here).

So my question is: are there any engines to execute TypeScript code without converting it to JavaScript?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Islam Goher
  • 413
  • 5
  • 9
  • 3
    `are there any engines to execute TypeScript code without converting it to JavaScript?` Not that i'm aware of. Would there be any benefit to that? Seems like it would be a lot of work to implement, and would have the same result as deleting the types (which is basically what transpiling typescript is) and then using existing javascript engines – Nicholas Tower Nov 17 '21 at 14:28
  • 2
    it might be a problem to execute TS directly since TS does not have specification – captain-yossarian from Ukraine Nov 17 '21 at 14:28
  • 1
    @captain-yossarian do you refer to the ECMAScript specifications? – Islam Goher Nov 17 '21 at 14:40
  • 2
    I meant that TS has not any specification at all, it completely depends on JavaScript which in turn has its own specification ECMAScript. Imagine that you want to write a compiler for some language without specification, how would you do it? – captain-yossarian from Ukraine Nov 17 '21 at 14:44
  • You tagged [Deno](https://deno.land/). Doesn't it execute TypeScript? – jabaa Nov 17 '21 at 14:55
  • 2
    It doesn't, it converts TypeScript to Javascript first, then executes JavaScript. the link to documentation is in the body of the question. – Islam Goher Nov 17 '21 at 15:06
  • In that case there won't be any engines to run TypeScript or JavaScript directly because a CPU can't interpret it. You always have to translate that code into machine code. – jabaa Nov 17 '21 at 15:19
  • 2
    yes, I'm aware of that, but I was asking about executing TypeScript without converting it to JavaScript first – Islam Goher Nov 17 '21 at 16:27

2 Answers2

5

No, TypeScript is not a "standalone" language in that sense. It is and always will be a superset of JavaScript. This is why the TypeScript Compiler is often referred to as a transpiler: it doesn't compile to a lower-level language. After tsc has run its checks it transforms existing source to JavaScript by simply stripping out all the TypeScript constructs.

From the intro of the official TypeScript Handbook:

The goal of TypeScript is to be a static typechecker for JavaScript programs - in other words, a tool that runs before your code runs (static) and ensures that the types of the program are correct (typechecked).

So in order to execute TypeScript, you will always need a JavaScript engine. You could adapt an existing JavaScript engine (or build your own) to understand TypeScript as well, but still it would always first have to be an engine conforming to the ECMAScript specification.

Deno is no different. It has a built-in TypeScript Compiler, which is a copy of the official one. From the TypeScript chapter of the Deno manual.

At a high level, Deno converts TypeScript (as well as TSX and JSX) into JavaScript. It does this via a combination of the TypeScript compiler, which we build into Deno, and a Rust library called swc. When the code has been type checked and transformed, it is stored in a cache, ready for the next run without the need to convert it from its source to JavaScript again.

After transpilation, Deno runs the output JavaScript on Google's V8 Engine, the same engine used in NodeJS and Chrome.

Martin Devillers
  • 17,293
  • 5
  • 46
  • 88
  • 4
    What about [Deno](https://deno.land/)? Doesn't it execute TypeScript? _"One of the benefits of Deno is that it treats TypeScript as a first class language, just like JavaScript or Web Assembly, when running code in Deno. What that means is you can run or import TypeScript without installing anything more than the Deno CLI."_ – jabaa Nov 17 '21 at 14:55
  • 2
    It depends on how you look at it. Yes, it "executes" TypeScript in the sense that you can feed it TypeScript files directly without first having to run `tsc`. But internally, it just does `tsc` for you. Deno ships with a built-in TypeScript Compiler, which is a copy of the official one. The transpiled JavaScript is then run on Google's V8 Engine, no different than NodeJS or Chrome. – Martin Devillers Nov 17 '21 at 15:08
  • 2
    The question is _"Are there any engines to execute TypeScript code directly?"_ The obvious answer is: Yes, you can. Deno will handle this internally for you. Of course every programming language has to be translated into machine code (in one or more steps). There are no CPUs that can run JavaScript or C++ or any other language. – jabaa Nov 17 '21 at 15:16
  • 5
    I don't think the answer is an _obvious yes_. The author already mentions in their own post that they don't view Deno as a TypeScript engine, since internally it just runs the same transpiler you would otherwise run manually. Which is why I wrote in my comment "it depends on how you look at it". If you view Deno as a blackbox, yes it "runs" TypeScript. But to me, the V8 engine is the one executing the code and that code is JavaScript. The implicit transformation from TS to JS to me is a developer convenience feature of Deno and not a fundamental difference in how it operates versus Node+TS. – Martin Devillers Nov 17 '21 at 15:53
  • 2
    So you are saying it's impossible to write an engine that interprets and evaluates TypeScript without converting it to JavaScript? And you are sure this doesn't exist, even as a science proof of concept? – jabaa Nov 17 '21 at 17:07
  • 1
    To build an engine you need a specification. The TypeScript specification covers all the JavaScript building blocks from the perspective of type correctness. For instance it specifies that the `instanceof` operator takes an Any, String or Number and returns a Boolean. It doesn't state what `instanceof` is supposed to do. The ECMAScript standard does that. So to build this engine you would need to implement both standards at the same time. Asides from any combability issues between these standards, what would this produce? Another JavaScript engine, just with TypeScript support :-) – Martin Devillers Nov 17 '21 at 20:59
  • 1
    The question is if it's possible to evaluate TypeScript without converting to JavaScript before and if such an engine already exists. Of course you can do that and probably there already some engines as proof of concept. The result is one engine that implements JavaScript and TypeScript and evaluates it in one single step. – jabaa Nov 17 '21 at 21:12
  • @jabaa people are using the absence of a standard for TypeScript as an explanation for why a direct compiler is not possible. The solution is a standard for TypeScript. I assume that is inevitable. – Sam Hobbs Sep 20 '22 at 04:51
  • This answer is overly strict. Nothing is stoping someone from writing a compiler from TypeScript directly to machine code, or Java VM instructions or the Python VM, or anything else, skipping the conversion to JavaScript step. It just seems like no one has seriously done it yet, though [garkin's answer](https://stackoverflow.com/a/72581848) mentions [a project](https://github.com/ASDAlexander77/TypeScriptCompiler) using LLVM. – Boris Verkhovskiy Oct 30 '22 at 10:01
4

As of now there are no TypeScript-aware V8-like JavaScript engine. It's unlikely we would see one. Sometime in the future TC39 will decide on official JS-With-Types proposal. And then, typing and corresponding optimizations will gradually be added to V8 itself.

It may be unlikely for TypeScript to be directly compatible, but it will still be easily transpilable back and forth.

Currently there are several R&D AOT compilation projects (directly to binary, wasm/native) for the Typescript, or at least for a viable subset of it.

https://github.com/ASDAlexander77/TypeScriptCompiler

garkin
  • 139
  • 2
  • 4