TExcelTAD
Home › TypeScript › TS Intro

TypeScript Introduction

TypeScript is JavaScript with types. It was created at Microsoft and first released in 2012, and today it is one of the most popular languages on the web. Every valid JavaScript program is also a valid TypeScript program — TypeScript only adds things: a way to describe what kind of value each variable, parameter and return value holds.

Those descriptions are checked by the TypeScript compiler (tsc) before your code runs. If you pass a string where a number is expected, misspell a property name or forget that a value might be undefined, the compiler tells you immediately — instead of a user finding the bug in production.

How TypeScript works

Browsers and Node.js do not understand type annotations. So TypeScript works in two steps:

  1. Type-check – the compiler reads your .ts files and reports type errors.
  2. Emit JavaScript – it removes the type annotations and outputs plain .js that runs anywhere.

The types exist only at development time. At runtime there is just JavaScript, so TypeScript adds no performance cost.

JavaScript TypeScript
Typing Dynamic: checked while running Static: checked before running
Errors found At runtime, often by users In your editor, as you type
File extension .js .ts (and .tsx for JSX)
Runs directly in browsers Yes No — compiled to JavaScript first
Editor support Good Excellent: autocomplete, rename, go-to-definition

Your first TypeScript program

The playgrounds in this course type-check your code with TypeScript 5.9 in strict mode. If the check passes, the compiled JavaScript runs and you see its console output. Press ▶ Run:

ts · try it yourself
const greeting: string = "Hello, TypeScript!";
const year: number = 2012;

console.log(greeting);
console.log(`TypeScript has been around since ${year}.`);
Hello, TypeScript!
TypeScript has been around since 2012.

The : string and : number parts are type annotations. They tell the compiler what each constant must hold. Apart from them, this is ordinary JavaScript.

Catching a bug before it runs

Here is the same idea, but with a mistake. The function expects a number, and we pass a string:

ts · try it yourself
function double(n: number): number {
  return n * 2;
}

console.log(double(21));
console.log(double("21"));
main.ts(6,20): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.

The code was not executed at all — the compiler stopped it. The message tells you the file, the line and column (6,20), an error code (TS2345) and a readable explanation. In plain JavaScript, "21" * 2 would silently produce 42, but "21" + 2 would produce "212" — exactly the kind of surprise TypeScript protects you from.

Fix it by passing a number:

ts · try it yourself
function double(n: number): number {
  return n * 2;
}

console.log(double(21));
console.log(double(Number("21")));
42
42

Tip: Error messages in this course look exactly like the ones tsc prints in a terminal. Learning to read them is one of the most useful TypeScript skills.

Why learn TypeScript?

  • Fewer bugs: whole classes of mistakes (typos, wrong argument types, missing null checks) are caught at compile time.
  • Better tooling: editors know the shape of every value, so autocomplete and refactoring "just work".
  • Self-documenting code: a function signature like (id: number) => User | undefined tells you how to use it.
  • Industry standard: Angular is written in TypeScript, and React, Vue, Svelte, Node.js and Deno projects use it widely.
  • Gradual adoption: you can rename one .js file to .ts and add types step by step.

What you need to know first

This course assumes you know basic JavaScript: variables (let, const), functions, arrays, objects and classes. TypeScript adds types on top of those features; it doesn't replace them.

Test yourself

What happens to TypeScript type annotations when the code is compiled?

  1. They are kept and checked at runtime
  2. They are removed; the output is plain JavaScript
  3. They are converted to comments
  4. They are turned into runtime assertions

Which statement about TypeScript and JavaScript is true?

  1. Every JavaScript program is valid TypeScript syntax
  2. TypeScript code runs natively in all browsers
  3. TypeScript is a completely different language from JavaScript
  4. TypeScript makes code run faster

In main.ts(6,20): error TS2345, what does 6,20 mean?

  1. Error 6 of 20
  2. Line 6, column 20
  3. TypeScript version 6.20
  4. 20 errors on line 6