TypeScript in Practice

Getting started · Lesson 2 of 12 · 11 min

A modern TypeScript setup

A good setup catches mistakes while you type, not in production. Here's the configuration we'll use for the rest of the course.

Create the project

npm create vite@latest ts-practice -- --template react-ts

Turn on the strict options

tsconfig.json
{
  "compilerOptions": {
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "exactOptionalPropertyTypes": true,
    "noImplicitOverride": true
  }
}

Add a typecheck script

package.json
{ "scripts": { "typecheck": "tsc --noEmit" } }

Run it in CI. Your editor shows errors in open files; tsc checks all of them.

noUncheckedIndexedAccess makes items[0] return T | undefined. It feels strict at first, and it prevents a whole family of "cannot read properties of undefined" bugs.

Project structure

src
components
lib
api.ts# typed fetch helpers
main.tsx
tsconfig.json

Exercise: clone the starter project from the resources below and run npm run typecheck. It should pass with zero errors.

Resources