0

I know the title looks stupid but hear me out, if we have a typescript file that will not run on the browser then why is there a need to compile it? I mean it already runs without compiling atleast in development so theres no need to compile it.

Maybe theres something on the production side of things that I am not aware of but to my current knowledge, theres no need to compile it.

1
  • Browsers/NodeJS can't understand typescript files so you have to compile it to javascript. In development your build system does this for you automatically in background. Also note that a typescript file without any typescript syntax can be run inside browser/node without compiling since its all there left is JS code. Commented Nov 22, 2022 at 15:29

1 Answer 1

0

I mean it already runs without compiling atleast in development so theres no need to compile it.

That assumption is not accurate.

Running in development, no matter whether it is Nest or ts-node or deno all transpile the TypeScript to JavaScript behind the scenes:

[ts-node] JIT transforms TypeScript into JavaScript, enabling you to directly execute TypeScript on Node.js without precompiling. ts-node Readme

At a high level, Deno converts TypeScript (as well as TSX and JSX) into JavaScript. Deno docs

And while I couldn't find anything in the Nest Docs on this topic, the typescript package is a direct dependency of the Nest CLI indicating that it's not just used for development (but also in the CLI itself to compile the project) and a rough glance through the codebase of the CLI also makes it look like the project is being compiled (either by Webpack or TS) prior to being executed.


To answer the question in the title: No, not necessarily. I have run a project in production with ts-node in the past.

Would I recommend it? No, not really. It's JIT compiled, meaning while the process executes stuff, it trims out stuff on the fly before it's being executed, not really helping performance. In development, it doesn't matter and ts-node-dev with its watch feature is incredibly useful, not having to wait for full compilation of the project but for production, having JavaScript there instead of TypeScript is a good idea, eventhough not strictly required.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.