Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

TS
TypeScript
0/10 completed
Beginner
Why TypeScript?Basic types and inferenceTyping functionsObjects, interfaces and type aliases
Intermediate
Unions and narrowingLiteral types, tuples and enumsGenerics
Advanced
Utility typesMapped and conditional types
Pro
TypeScript in real projects
Tracks / TypeScript / Pro

TypeScript in real projects

You know the type system. Now let's set up TypeScript the way professional teams do: configuration, strictness, third-party types, validating external data, and migrating an existing JavaScript project.

tsconfig.json

Every TypeScript project has a tsconfig.json that controls the compiler. Here's a solid modern starting point:

json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "lib": ["ES2022", "DOM", "DOM.Iterable"],
    "jsx": "react-jsx",

    "strict": true,
    "noUncheckedIndexedAccess": true,
    "noImplicitOverride": true,
    "noFallthroughCasesInSwitch": true,

    "isolatedModules": true,
    "verbatimModuleSyntax": true,
    "skipLibCheck": true,
    "noEmit": true,

    "baseUrl": ".",
    "paths": { "@/*": ["./src/*"] }
  },
  "include": ["src"]
}
Option Why
strict: true Always on. Enables strictNullChecks, noImplicitAny and more. Most of TypeScript's value depends on it.
noUncheckedIndexedAccess arr[i] becomes T | undefined, which catches out-of-bounds bugs
moduleResolution: "Bundler" Matches how Vite, Next.js and other bundlers resolve imports
noEmit Let your bundler compile; tsc only type-checks
paths Import aliases like @/components/Button

Type-check in CI

Bundlers like Vite and esbuild strip types without checking them (that's why they're fast). Run the checker separately, and in CI:

json
// package.json
{
  "scripts": {
    "typecheck": "tsc --noEmit",
    "build": "npm run typecheck && vite build"
  }
}

Types for third-party libraries

  • Many libraries ship their own types (axios, zod, date-fns).
  • Others have community types on DefinitelyTyped: npm i -D @types/node @types/express.
  • If a library has no types at all, declare a minimal module yourself:
ts
// src/types/legacy-lib.d.ts
declare module 'legacy-lib' {
  export function doThing(input: string): number;
}

Files ending in .d.ts contain only types (declarations), no runtime code.

External data: types are not validation!

Types only exist at compile time. JSON from an API, form input or localStorage can be anything at runtime. Asserting as User is a lie the compiler will believe:

TypeScript · editable

Validate at the boundary with a type guard, or (much more commonly) a schema library like Zod, which gives you runtime validation and the TypeScript type from one definition:

ts
import { z } from 'zod';

const UserSchema = z.object({
  id: z.number(),
  email: z.string().email(),
});
type User = z.infer<typeof UserSchema>;     // { id: number; email: string }, derived automatically

const result = UserSchema.safeParse(await res.json());
if (!result.success) {
  console.error(result.error.issues);
} else {
  const user: User = result.data;           // ✓ guaranteed correct at runtime
}

Here's a hand-written guard you can run:

TypeScript · editable

Sharing types across the stack

  • Monorepos share a types package between frontend and backend.
  • OpenAPI / GraphQL code generators create types from your API schema.
  • tRPC and Server Actions (Next.js) give end-to-end type safety without a schema step.
  • ORMs like Prisma and Drizzle generate types from your database schema.

Migrating a JavaScript project

  1. Add tsconfig.json with "allowJs": true and "checkJs": false.
  2. Rename files one at a time, .js → .ts (or .jsx → .tsx), starting with leaf modules (utilities with no dependencies).
  3. Fix errors. Use unknown or narrow types rather than any. Use // @ts-expect-error with a comment for anything truly temporary.
  4. Gradually turn on strict options.
  5. Track progress, and ban new any with the ESLint rule @typescript-eslint/no-explicit-any.

Common mistakes to avoid

Mistake Better
any everywhere unknown + narrowing, or proper types
Annotating everything Let inference work; annotate boundaries (params, exports)
as assertions to silence errors Fix the type, or validate the data
Turning off strict Keep it on; fix the root cause
Huge clever types in app code Simple, readable types; clever ones only in shared libraries

🎓 TypeScript track complete!

You can type functions, objects, unions, generics and advanced patterns, configure a project, and validate real-world data. Next up: React (with TypeScript!) and Next.js.

Practice

Write a type guard isProduct for { id: string; name: string; price: number; tags: string[] }. Then use it to filter an array of unknown values parsed from JSON, keeping only valid products, and print their names.

TypeScript · editable
✎ Check your understanding

1.Why isn't const user = data as User enough for API data?

2.What should almost every project's tsconfig have?

← Mapped and conditional types