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:
{
"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:
// 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:
// 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:
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:
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:
Sharing types across the stack
- Monorepos share a
typespackage 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
- Add
tsconfig.jsonwith"allowJs": trueand"checkJs": false. - Rename files one at a time,
.js→.ts(or.jsx→.tsx), starting with leaf modules (utilities with no dependencies). - Fix errors. Use
unknownor narrow types rather thanany. Use// @ts-expect-errorwith a comment for anything truly temporary. - Gradually turn on
strictoptions. - Track progress, and ban new
anywith 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.
1.Why isn't const user = data as User enough for API data?
2.What should almost every project's tsconfig have?