Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 96 ยท Getting Started

Getting Started with TypeScript

Learn what TypeScript is, how it adds static types to JavaScript, why teams rely on it, and write and run your first type-checked program in our editor.

TypeScript is JavaScript with types. You write the same code you would write in JavaScript, and you add small notes that say what kind of value each variable, parameter and function result should hold. A program called the TypeScript compiler reads those notes and checks the whole program for mistakes before it runs. Then the notes are removed, and what's left is plain JavaScript that runs anywhere JavaScript runs: browsers, Node.js, Deno, Bun, phones and servers.

That one idea, checking types ahead of time, changes how it feels to write code. Typos in property names, functions called with the wrong arguments, forgotten null checks and half-finished refactors are reported while you type, not by a user at 3 a.m. That's why TypeScript has become the default for new web projects, large front-end apps, Node.js back ends and most popular libraries.

This course assumes you know some JavaScript: variables, functions, objects and arrays. If you're rusty, the JavaScript course covers everything you need. Every lesson here has live editors that type-check and run real TypeScript, so you can try each idea as you read.

JavaScript plus types

Here is a small JavaScript function. It works, but nothing in the code says what price and quantity should be:

JavaScript
function lineTotal(price, quantity) {
  return price * quantity;
}

lineTotal(4.5, 3);     // 13.5
lineTotal('4.5', 3);   // 13.5 (the string is quietly converted)
lineTotal(4.5);        // NaN (quantity is undefined)

JavaScript happily runs all three calls. The last one produces NaN, and you only find out when a strange total shows up somewhere far away from the real mistake.

Now the TypeScript version. The only change is : number after each parameter and after the parameter list:

TypeScript Your first TypeScript function

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Press Run. The program prints Total: 13.5 and Rounded: 13.50. The annotations are called type annotations: price: number means "price must be a number", and the : number after ) means "this function returns a number".

The interesting part is what happens when the code is wrong. The next editor makes the two mistakes from the JavaScript version:

TypeScript TypeScript catches the mistakes (fails on purpose)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Nothing runs. Instead you get two errors:

TEXT
main.ts(5,23): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
main.ts(6,13): error TS2554: Expected 2 arguments, but got 1.

Each line names the file, the position as (line,column), an error code and a plain-English message. Line 5, column 23 is exactly where the string '4.5' starts. Line 6 is the call with a missing argument. Fix both calls (for example lineTotal(4.5, 3) and lineTotal(4.5, 1)) and run again. You will learn to read these messages in detail in Your First Types.

TypeScript is a superset of JavaScript. Every JavaScript feature works in TypeScript, and the runtime behavior is the same. TypeScript adds a type layer on top, plus a few extra features such as enums. It does not add a new runtime, a new standard library or a new way of running code.

How our editors run TypeScript

The editors in this course work like a real TypeScript project in two steps:

  1. Type-check. The TypeScript 7 compiler checks your code in strict mode, the strictest standard setting, which is what professional projects use.
  2. Run. If there are no type errors, the types are removed and Node.js 24 runs the resulting JavaScript. Whatever you print with console.log() appears in the output panel.

Here are the rules worth remembering:

  • Any type error stops the run. You see only the list of errors, and none of your code runs, not even the lines before the error. That's on purpose: it matches a build that refuses to ship broken code.
  • Errors use the format main.ts(line,column): error TSxxxx: message. Your code is always the file main.ts.
  • Runtime errors are different. Code that type-checks can still fail while running, for example when you throw an error. Then the output shows a stack trace pointing at the line in main.ts.
  • Output looks like Node.js. Objects print as { name: 'Ada', age: 36 }, arrays as [ 1, 2, 3 ], and strings inside structures get single quotes.
  • Every run starts fresh, and Reset brings back the original example.
  • There is no browser page. The code runs in Node.js, so document and window don't exist. Lessons about the DOM explain how to work around that.

Try this one. It type-checks, so it runs, and it prints a few kinds of values:

TypeScript Values in the Node.js console

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

string[] means "an array of strings", and Map<string, number> means "a map from string keys to number values". You'll meet both properly in Arrays and Tuples and Generics. Notice that author has no annotation at all, yet TypeScript still knows its shape. That's type inference, and it means you write far fewer annotations than you might expect.

Why teams use TypeScript

TypeScript costs a little extra typing. Here is what you get back.

Mistakes caught before running

The compiler finds whole families of bugs without running a single line: misspelled properties, missing arguments, values that might be undefined, comparisons that can never be true, and code paths that forget a case. The next example has a classic typo, user.emial:

TypeScript A typo the compiler spots (fails on purpose)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

The error reads Property 'emial' does not exist on type '{ name: string; email: string; }'. Did you mean 'email'? (code TS2551). In plain JavaScript, user.emial is simply undefined, and the program would cheerfully "send" mail to nobody. Fix the typo and run it.

Better editor help

Because the editor knows every type, it can offer accurate autocompletion, show documentation as you type, jump to definitions and rename a function across hundreds of files safely. Many developers say this is the biggest day-to-day win. Professional editors such as VS Code have this built in.

Code that documents itself

A signature like function lineTotal(price: number, quantity: number): number tells the next reader what goes in and what comes out. Unlike a comment, it can't drift out of date, because the compiler checks it on every build.

Safe changes in big code bases

Rename a property or add a required parameter, and the compiler lists every place that must change. That makes refactoring a large project a checklist instead of a gamble.

What TypeScript does not do

It's just as important to know the limits:

  • Types don't exist at runtime. They are removed before the code runs, so they can't check data that arrives while the program runs, such as JSON from a server or text a user typed. You still validate that data with real code. How TypeScript Works shows this in action.
  • It doesn't make code faster or slower. The JavaScript that runs is essentially the code you wrote, minus the types.
  • It can't catch every bug. A function that returns the wrong number still type-checks. Types catch mismatched shapes and missing cases; tests catch wrong logic.

Here is a program that type-checks perfectly but still crashes at runtime, because the problem only appears while it runs:

TypeScript A runtime error after a clean type-check

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

The first call prints Age 30 is fine. The second throws, and the output shows Error: Age cannot be negative: -4 with a stack trace pointing at line 3 of main.ts. Types and runtime checks work together: types make sure checkAge always receives a number, and the if makes sure that number makes sense.

TypeScript and JavaScript side by side

JavaScript TypeScript
Typing Dynamic: types are checked only when the code runs Static: types are checked before the code runs
Errors Found at runtime, often by your users Most type mistakes found in the editor or at compile time
Execution Runs directly in browsers and Node.js Compiled (or type-stripped) to JavaScript first
Tooling Basic autocomplete Precise autocomplete, safe renaming and refactoring
Scale Fine for small scripts; gets harder to change in big codebases Types document intent, which helps teams and large projects
Learning curve Lower: no extra syntax Moderate: you learn annotations, generics and compiler options

TypeScript is a superset of JavaScript: every valid JavaScript program is also valid TypeScript syntax, so you can adopt it one file at a time. The extra learning is real but small at the start, because inference means you annotate far less than you might expect.

TypeScript 7 in one paragraph

TypeScript was created at Microsoft and first released in 2012. It is open source and has had a new version every few months ever since. TypeScript 7 is a milestone: the compiler was rewritten from TypeScript into Go, which makes type-checking roughly ten times faster on large projects. The language didn't change, so code written for TypeScript 5 or 6 works the same way, and everything in this course applies to all recent versions. Where a feature arrived in a specific version, the lesson says so. What's New in TypeScript has the full history.

What you will learn

The course follows a path from first annotations to advanced type-level code:

  1. Getting Started: how the compiler works, annotations, inference, tsconfig.json and strict mode.
  2. Basic Types: primitives, arrays, tuples, objects, unions, literals, aliases, interfaces and enums.
  3. Functions and Narrowing: typing functions and callbacks, overloads, and how TypeScript follows your if checks.
  4. Objects and Classes: structural typing, classes, access modifiers, inheritance and immutability.
  5. Generics and Type Manipulation: reusable generic code, keyof, utility types, mapped, conditional and template literal types.
  6. Modules, Async and Modern Features: modules, declaration files, promises, error handling, decorators, using and satisfies.
  7. TypeScript in Practice: Node.js, the browser, APIs, React and other frameworks, tooling and best practices.

Five projects, a cheat sheet, practice programs, exercises and interview questions close the course.

Tips for learning TypeScript

  • Read the error, then the line. TypeScript's messages are precise. Start with the first error; later ones are often caused by it.
  • Break examples on purpose. Change a number to a string and see what the compiler says. Errors are the fastest teacher in TypeScript.
  • Don't annotate everything. Let inference do its job. Annotate function parameters and the places where you want to state a promise.
  • Never reach for any to silence an error. It turns type-checking off. You'll learn safer tools like unknown soon.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What happens to TypeScript's type annotations when the program runs?

    TypeScript checks types ahead of time and then erases them. The code that runs is plain JavaScript, so types cannot validate data that arrives at runtime.
  2. 2. In our editor, line 7 of your code has a type error. What does the output show?

    Any type error stops the run before anything executes. A stack trace appears only for runtime errors in code that type-checked.
  3. 3. What does main.ts(5,23) in an error message tell you?

    The numbers in parentheses are the line and column where the problem starts in main.ts.
  4. 4. function area(w: number, h: number): number { return w + h; } has a bug: it adds instead of multiplying. What does TypeScript say?

    Types check shapes, not intentions. Wrong logic with correct types compiles fine, which is why tests still matter.
  5. 5. Which statement about TypeScript 7 is true?

    TypeScript 7 is a native port of the compiler. Code that worked in recent 5.x or 6.0 versions keeps working; checking is just much faster.

Summary

  • TypeScript is JavaScript plus static types. The compiler checks the types before the program runs, then removes them.
  • Type annotations such as price: number and ): number state what a value or function result must be.
  • Type errors look like main.ts(5,23): error TS2345: message: file, line, column, code and explanation.
  • In our editors, any type error stops the run; code that type-checks runs on Node.js 24 and prints with console.log().
  • Teams use TypeScript for early bug detection, great editor help, self-documenting code and safe refactoring.
  • Types don't exist at runtime: data from outside still needs real checks, and types don't replace tests.
  • TypeScript 7 is the same language with a much faster, Go-based compiler.

Next: How TypeScript Works โ€” the compiler, type erasure and the tools that run TypeScript.