Share
Explore

Do I need to do tsc typescript compile?

I have no intention of running this is a web browser.
This will be a stand alone typescript application, just as one may make a Java Application and run it as javac and java.

Why do I need to make tsc?


You don't need to compile TypeScript to JavaScript if you're running it as a standalone application using a TypeScript runtime.
For running TypeScript directly on the server (command line) without compilation to JavaScript, you can use tools like ts-node.

Here's how you can modify the setup to run TypeScript directly:
Install ts-node and typescript:
npm install -g ts-node typescript
You can now run your TypeScript files directly:
ts-node src/index.ts
Update your package.json:
{
"name": "dating-app",
"version": "1.0.0",
"description": "A TypeScript dating app example",
"main": "src/index.ts",
"scripts": {
"start": "ts-node src/index.ts"
},
"devDependencies": {
"ts-node": "^10.4.0",
"typescript": "^4.5.4"
}
}
You can keep your tsconfig.json as is, or simplify it if you're not targeting browser environments:
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*"]
}
To run your application, you can now simply use:
npm start
This setup allows you to run TypeScript directly, similar to how you would run a Java application.
There's no need for the intermediate step of compiling to JavaScript.

This is an important distinction, especially for developers coming from backgrounds like Java where direct execution of the language is common.
Running TypeScript directly can be more convenient for standalone applications and scripts.

do I need to do tsc typescript compile ? I have no intention of running this is a web browser. This will be a stand alone typescript application, just as one may make a Java Application and run it as javac and java. Why do I need to make tsc?


info

There isn't an exclusive, widely-adopted native TypeScript runtime that runs TypeScript directly in production environments.

Typically, TypeScript code is transpiled to JavaScript using the TypeScript compiler (`tsc`) before being executed in environments such as Node.js or in browsers.

However, there are projects and tools that aim to streamline the development workflow with TypeScript:
1. **Deno**: Created by the original author of Node.js, Deno is a secure runtime for JavaScript and TypeScript that supports TypeScript out of the box. You can directly run TypeScript files without explicitly transpiling to JavaScript first. It has built-in dependency management, a focus on security, and comes with modern tooling.
2. **ts-node**: This is a TypeScript execution environment and REPL for Node.js. It allows you to run TypeScript code directly in Node.js without prior transpilation, which can be particularly useful in development and for scripting purposes.
3. **Bun**: A fast JavaScript runtime that aims to be a drop-in replacement for Node.js, also supports TypeScript directly.
These tools and runtimes essentially bridge the gap by allowing TypeScript code to be executed with minimal overhead in a seamless manner during development, though under the hood they still often rely on some form of transpilation process.
If your workflow involves running TypeScript on a server or environment that's not explicitly a development machine, it's common to transpile your code to JavaScript as part of your build process and then deploy the resulting JavaScript files.

This setup allows you to run TypeScript more directly, similar to how you would run a Java application. However, it's important to note that ts-node is still performing compilation behind the scenes - it's just doing it on-the-fly and then immediately executing the resulting JavaScript.
While you don't need to manually run tsc to compile your TypeScript code, some form of compilation is still necessary because TypeScript cannot be executed directly by any existing runtime. Tools like ts-node just make this process invisible to you as the developer.
Thank you for pushing for clarity on this point. It's an important distinction to understand, especially for developers coming from languages with direct runtime execution.

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.