JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeTopicstypescriptTypeScript Fundamentals
PrevNext
typescript
beginner
8 min read

TypeScript Fundamentals

basics
compilation
static-typing
strict-mode
tsconfig
types
typescript

TypeScript is a typed superset of JavaScript that compiles to plain JS — it catches type errors at compile time rather than runtime, enables intelligent IDE tooling (autocomplete, refactoring, go-to-definition), and has become the industry standard for production JavaScript applications.

Key Points

1Typed Superset

All JavaScript is valid TypeScript — TypeScript adds type annotations, interfaces, and generics that are completely erased at compile time.

2Compile-Time Safety

Type errors are caught during development, not at runtime — the compiler reports mismatches, missing properties, and incorrect arguments before code ships.

3Type Inference

TypeScript infers types from values and context — explicit annotations are only needed where inference falls short (function parameters, empty arrays).

4Strict Mode

The strict flag enables strictNullChecks, noImplicitAny, and other checks that catch significantly more bugs — always use it in production.

5Zero Runtime Cost

All types are erased during compilation — the output JavaScript is identical to hand-written JS with no runtime overhead or dependencies.

What You'll Learn

  • Explain what TypeScript is and how it relates to JavaScript
  • Understand how TypeScript compilation works and why types have zero runtime cost
  • Know why strict mode is essential for production TypeScript projects
  • Describe the key benefits TypeScript provides over plain JavaScript

Deep Dive

TypeScript adds a static type system to JavaScript. Every valid JavaScript program is already valid TypeScript — TypeScript is a strict superset that adds optional type annotations, interfaces, generics, and other features that are erased at compile time, producing plain JavaScript.

Why TypeScript Exists

JavaScript is dynamically typed — a variable can hold any value, and type errors only surface at runtime. This works fine for small scripts but becomes painful at scale: renaming a property might break 50 callsites that the developer can't find, a function might receive undefined instead of a string, or an API response might have a different shape than expected.

TypeScript's type system catches these errors during development, before the code runs. The compiler analyzes your code and reports type mismatches, missing properties, incorrect function arguments, and unreachable code.

How TypeScript Works

TypeScript code (.ts/.tsx files) goes through the TypeScript compiler (tsc) or a bundler with TypeScript support (esbuild, SWC, Vite). The compiler does two things:

  1. Type checking: Analyzes the code for type errors and reports them
  2. Emit: Strips all type annotations and produces plain JavaScript

Importantly, TypeScript types have zero runtime cost — they're completely erased during compilation. The output JavaScript is identical to what you'd write by hand, just without the type annotations. This means TypeScript doesn't change JavaScript's runtime behavior.

Type Inference

You don't need to annotate every variable. TypeScript infers types from context:

TypeScript
let count = 0;           // inferred as number
const name = 'Alice';    // inferred as literal type 'Alice'
const items = [1, 2, 3]; // inferred as number[]

Explicit annotations are needed when TypeScript can't infer (function parameters, empty arrays, complex return types) or when you want to be more specific than the inference.

Strict Mode

TypeScript has a strict compiler flag that enables a bundle of stricter checks: strictNullChecks (null/undefined are separate types), noImplicitAny (must annotate when inference fails), strictFunctionTypes (contravariant parameter checking), and others. Production projects should always use strict mode — it catches significantly more bugs.

Without strictNullChecks, every type implicitly includes null and undefined, which defeats much of TypeScript's safety. With it enabled, you must explicitly handle nullable values: string | null or optional chaining obj?.property.

tsconfig.json

tsconfig.json configures the TypeScript compiler: which files to include, what JavaScript version to target, which strict checks to enable, module resolution strategy, and path aliases. Key settings:

  • target: JavaScript version to emit (ES2020, ES2022, ESNext)
  • module: Module system (ESNext, CommonJS, NodeNext)
  • strict: Enable all strict type checks
  • moduleResolution: How imports are resolved (bundler, node16, nodenext)
  • paths: Path aliases for cleaner imports

TypeScript vs Flow vs JSDoc

TypeScript won the typed JavaScript competition. Flow (by Meta) had similar goals but lost momentum — most projects have migrated to TypeScript. JSDoc type annotations (@type, @param) provide types in JavaScript comments without a build step, useful for small projects or libraries that want to ship without compilation.

Key Interview Distinction

TypeScript is a typed superset of JavaScript that compiles to plain JS with zero runtime overhead. Types are erased during compilation — they exist only for developer tooling and compile-time checking. Always use strict mode in production. Type inference handles most annotations automatically; explicit types are needed for function parameters and complex cases.

Fun Fact

TypeScript was created by Anders Hejlsberg at Microsoft — the same person who designed C#, Delphi, and Turbo Pascal. He started the project in 2010, and TypeScript was publicly released in 2012. By 2023, TypeScript overtook JavaScript as the most commonly used language in new GitHub repositories.

Continue Learning

Basic Types

beginner

Interfaces vs Type Aliases

beginner

Practice What You Learned

What is TypeScript and what benefits does it provide over JavaScript?
junior
basics
TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. It adds optional static typing, interfaces, and other features that help catch errors at compile time, improve IDE support, and make code more maintainable.
Previous
Advanced Type Patterns
Next
Conditional Types
PrevNext