Capítulo 1 de 36

Chapter 1: The Basics

Core Idea

TypeScript is a static type system layered on JavaScript: it predicts what your code will do before it runs, catching a class of bugs (typos, uncalled functions, unreachable branches, wrong property access) that JavaScript itself only reports as runtime TypeErrors or silent undefineds.

Key Concepts

  • Static vs. dynamic typing: JavaScript is purely dynamic — the only way to know what a function does with a value is to run it. TypeScript adds a static layer that reasons about shapes/behaviors ahead of time.
  • Non-exception failures: TypeScript flags things JavaScript itself considers "valid" but almost certainly aren't intended — e.g. accessing a property that doesn't exist on an object (JS returns undefined silently; TS errors).
  • Tooling, not just checking: the same type-checker that reports errors also powers autocompletion, quick fixes, refactors, and go-to-definition in any editor with TS support.
  • tsc: the TypeScript compiler. Installed via npm install -g typescript; tsc file.ts type-checks and emits a .js file.
  • Type annotations are erased at compile time — they never affect runtime behavior. tsc strips them and downlevels newer syntax (e.g. template literals → string concatenation) to the configured target.
  • noEmitOnError: by default tsc still emits JS even when type errors are found (useful mid-migration from JS). Turn this flag on to block emit on error.
  • Type inference: TypeScript infers types without annotations in most cases (let msg = "hi" infers string) — don't annotate when inference already gets it right.
  • Strictness is a dial, not a switch: the strict flag (or "strict": true in tsconfig.json) toggles every strictness flag at once; each can also be opted in/out individually. The two most consequential:
    • noImplicitAny: errors when TS can't infer a type and silently falls back to any.
    • strictNullChecks: stops null/undefined from being silently assignable to every other type.

Code Examples

function greet(person: string, date: Date) {
  console.log(`Hello ${person}, today is ${date.toDateString()}!`);
}
greet("Maddison", new Date());
  • What it demonstrates: explicit parameter type annotations (person: string, date: Date) let TypeScript catch a call-site mistake — e.g. passing Date() (which returns a string in plain JS) instead of new Date() — at compile time instead of at runtime.

Reference Tables

FlagEffect
strictEnables every strictness flag at once
noImplicitAnyErrors on any value TS can't infer and would otherwise silently type as any
strictNullChecksnull/undefined are no longer assignable to arbitrary types by default
noEmitOnErrorBlocks .js output when type errors are present
targetSets the ECMAScript version tsc downlevels/compiles to (default ES5; most projects can safely target ES2015+)

Key Takeaways

  1. New codebases should turn strict on from day one — it costs a bit of upfront annotation work but pays back in caught bugs and better tooling.
  2. Type annotations are a compile-time-only construct; they never change what your code does at runtime.
  3. Don't over-annotate — let inference do the work except where TypeScript genuinely can't determine a type on its own.
  4. tsc emitting despite errors is intentional (great for incremental JS→TS migration); use noEmitOnError once you want it to act as a hard gate.

Connects To

  • Everyday Types: the next step — the primitive and object type syntax used to write the annotations introduced here.
  • tsconfig.json / Choosing Compiler Options: where strict, target, and friends are actually configured for a project.