Are there any engines to execute TypeScript code directly?
Asked Answered
S

5

9

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?

Sices answered 17/11, 2021 at 14:16 Comment(8)
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 enginesApodictic
it might be a problem to execute TS directly since TS does not have specificationMasterpiece
@captain-yossarian do you refer to the ECMAScript specifications?Sices
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?Masterpiece
You tagged Deno. Doesn't it execute TypeScript?Forereach
It doesn't, it converts TypeScript to Javascript first, then executes JavaScript. the link to documentation is in the body of the question.Sices
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.Forereach
yes, I'm aware of that, but I was asking about executing TypeScript without converting it to JavaScript firstSices
C
8

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.

Choroid answered 17/11, 2021 at 14:29 Comment(10)
What about Deno? 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."Forereach
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.Choroid
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.Forereach
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.Choroid
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?Forereach
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 :-)Choroid
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.Forereach
@Forereach 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.Slashing
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 mentions a project using LLVM.Vaasta
is there a way to update TypeScript in runtime and be re-compiled and applied by nodejs?Hoxsie
K
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

Kylstra answered 11/6, 2022 at 4:59 Comment(0)
D
1

bun.sh has recently been released as a JavaScript runtime that runs directly TypeScript code, without the tedious step of transpiling your TypeScript into JavaScript source using tsc. It uses its own internal transpiler, which makes this very fast. Have a look on the documentation.

Note: Bun does not typecheck the typescript files during transpiling nor during your script being running. You still need tsc to catch static type and ensure your code quality stays as best as it can.

Bun is designed as a replacement for NodeJS, and is powered by Apple's JavaScriptCore (JSC) instead of Google's V8. This is the javascript compiler behind Webkit, used by the Safari Browser. Some things you have with NodeJS may not have been adapted into Bun yet (e.g. clusters).

Dipsomania answered 17/11, 2023 at 15:41 Comment(0)
C
0

There are no engines that execute TypeScript code directly without converting it to JavaScript. TypeScript is designed to be a superset of JavaScript, meaning it adds features like static typing on top of existing JavaScript syntax. However, browsers and JavaScript runtimes like Node.js only understand JavaScript.

This is why tools like the TypeScript compiler (tsc), ts-node, and Deno are necessary:

  • TypeScript Compiler (tsc): This is the official TypeScript compiler. It takes your TypeScript code and translates it into plain JavaScript. This JavaScript output is what can then be run in a browser or Node.js environment.
  • ts-node: This tool provides a streamlined way to run TypeScript code directly in Node.js. Under the hood, it uses the TypeScript compiler to transpile your code on the fly, so you don't have to do a separate compilation step.
  • Deno: Deno is a modern runtime environment for JavaScript and TypeScript. It has a built-in TypeScript compiler, so you can run TypeScript code directly without a separate compilation step. However, Deno still transpiles TypeScript to JavaScript internally before executing it. Deno and TypeScript:

Deno's built-in TypeScript support simplifies the development process, offering several benefits:

  • Secure defaults: Deno's security features like sandboxing and permission management enhance the safety of running code.
  • Enhanced developer experience: Deno's tools for type checking, code completion, and debugging make TypeScript development smoother.
  • Cross-platform compatibility: Deno's availability on Windows, macOS, and Linux facilitates development and deployment of TypeScript applications across various platforms. While Deno streamlines TypeScript development, it's crucial to remember that it still transpiles TypeScript to JavaScript internally before execution. There are currently no engines that directly execute TypeScript without this conversion step.

Deno Project Repo : https://github.com/denoland/deno/

Carolanncarole answered 22/5 at 4:20 Comment(0)
A
0

There isn't any engine that will execute Typescript code directly as Typescript is a transpiled language. However, there is a language that is similar to Typescript and has compilers. That language is called assemblyscript.

Here's an example of code written in assemblyscript that is taken from https://www.assemblyscript.org/introduction.html

export function fib(n: i32): i32 {
  var a = 0, b = 1
  if (n > 0) {
    while (--n) {
      let t = a + b
      a = b
      b = t
    }
    return b
  }
  return a
}
Astraddle answered 9/6 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.