AnyCompiler

Introduction to TypeScript

TypeScript is a superset of JavaScript — every valid JavaScript program is already valid TypeScript. What TypeScript adds on top is a type system: you can describe the shape of your data (strings, numbers, objects, functions), and the compiler checks that your code actually respects those shapes before it ever runs.

TypeScript code doesn't run directly — it compiles down to plain JavaScript first. The type annotations exist purely to catch mistakes while you write the code; once compiled, the output is ordinary JavaScript with the type information stripped away, and that's what actually executes.

Why bother with types?

  • Catching bugs before running the code — a typo in a property name, or passing the wrong kind of value, shows up immediately as an error instead of a runtime surprise
  • Better editor support — autocomplete, inline documentation, and safe renaming all rely on the editor knowing what type a value is
  • Self-documenting code — a function's type signature tells you what it expects and returns without needing to read its implementation
  • Safer refactoring — changing a type's shape immediately flags every place that assumed the old shape

A quick preview

Here's a real TypeScript program, executed for real on this page's own compiler — not a mockup:

let greeting: string = "Hello from TypeScript!";
console.log(greeting);
Output
Hello from TypeScript!

That's it — a normal variable, just with an explicit type. The next lesson runs your first full program and explains exactly what's happening.

Practice exercise

Declare a variable named version with type number set to 5, then print "TypeScript version:" followed by version using console.log(...).