Your First TypeScript Program
Introduction
Running one TypeScript file end-to-end locks in the workflow: write .ts, configure the compiler, emit .js, run with Node. Earlier chapters installed tools and configured the editor; this chapter walks through a small program from an empty project folder so you can repeat the steps confidently.
Prerequisites
- Installing TypeScript and Project Setup
- Editor Setup for TypeScript (optional but helpful)
- Node.js LTS on your PATH
The Compile Pipeline
TypeScript does not run directly in Node. The usual path:
hello.ts ──► tsc (compiler) ──► hello.js ──► node
│ reads plain
│ tsconfig.json JavaScript
types checked
(erased in output)Code explanation:
- Source (
.ts) may include type annotations tsctype-checks and writes JavaScript according tocompilerOptions- Runtime (Node or browser) only sees the emitted
.js
Tip
Types Disappear at Runtime
Type annotations are compile-time only. They help you and the compiler; they are not present in the emitted JavaScript.
Create a Fresh Practice Project
From a parent directory:
mkdir ts-first-app
cd ts-first-app
npm init -y
npm install --save-dev typescript
npx tsc --initTrim tsconfig.json for clarity:
Create the source folder:
mkdir srcWrite src/index.ts
A slightly richer example than plain console.log—a typed greeting function:
// Return a greeting string for a given name
function greet(name: string): string {
return `Hello, ${name}!`;
}
const appName: string = "ts-first-app";
const message: string = greet("TypeScript learner");
console.log(message);
console.log(`Running project: ${appName}`);Code explanation:
name: stringand: stringreturn type tell the compiler what shapes are allowedconstvariables get explicitstringtypes here for practice; inference would also work (see the next chapter)- Template literals (backticks) work the same as in JavaScript
Save the file. Open the ts-first-app folder in VS Code if you use it.
Compile
From the project root:
npx tscOn success, you should see:
dist/
index.jsIf tsc reports errors, fix them before running—unlike JavaScript, TypeScript blocks emit when type-checking fails (unless you use options that allow emit on error, which this tutorial does not recommend).
Optional watch mode while editing:
npx tsc --watchRun the Output
node dist/index.jsExpected output:
Hello, TypeScript learner!
Running project: ts-first-appCode explanation:
- You run
dist/index.js, notsrc/index.ts - Node has no built-in TypeScript runner in this workflow—you compile first
Compare Source and Output
Open dist/index.js. You will see plain JavaScript—no : string annotations:
function greet(name) {
return `Hello, ${name}!`;
}
const appName = "ts-first-app";
const message = greet("TypeScript learner");
console.log(message);
console.log(`Running project: ${appName}`);That is what ships to production or the browser: same logic, types stripped away.
Add an npm Script
In package.json:
{
"scripts": {
"build": "tsc",
"start": "node dist/index.js"
}
}Then:
npm run build
npm startCode explanation:
buildis the type-check + compile step teams run in CIstartassumesdist/is up to date afterbuild
Intentional Type Error (Learning Exercise)
Change one line to break the type contract:
const message: string = greet(123);Run npx tsc again. You should see an error similar to:
Argument of type 'number' is not assignable to parameter of type 'string'.Revert to greet("TypeScript learner") before continuing. This is the core value of TypeScript: catching mistakes before runtime.
Project Layout Reference
ts-first-app/
├── package.json
├── package-lock.json
├── tsconfig.json
├── node_modules/
├── src/
│ └── index.ts
└── dist/ ← generated; often gitignored
└── index.jsAdd to .gitignore:
node_modules/
dist/FAQ
Can I run .ts files without compiling?
Tools like tsx or ts-node exist for development. This track uses tsc + node first so you understand the default compiler pipeline.
Why is dist/ empty until I run tsc?
The compiler only writes output when you run it (or watch mode). Saving in VS Code does not emit .js unless you configure a build task or use --watch.
Do I commit dist/ to git?
For libraries you sometimes publish compiled output; for apps, teams usually build in CI and deploy artifacts. For learning repos, ignoring dist/ is common.
My error mentions rootDir or include—what now?
Ensure .ts files live under src/ if rootDir is "src", and that include matches your folder (for example "src/**/*").
Is index.ts a required name?
No. Any filename works; adjust node dist/your-file.js accordingly. index.ts is a convention for entry points.