utility types and tsconfig
What Are Utility Types?
TypeScript ships with a set of built-in utility types that transform existing types into new ones — saving you from rewriting similar interfaces over and over.
Partial<T>
Makes every property in a type optional. Handy for update functions where only some fields might change.
interface User {
name: string;
age: number;
email: string;
}
function updateUser(user: User, updates: Partial): User {
return { ...user, ...updates };
}
const original: User = { name: "Alice", age: 28, email: "alice@example.com" };
const updated = updateUser(original, { age: 29 }); // only 'age' needs to be provided Required<T>
The opposite of Partial — forces every property to be required, even ones marked optional in the original type.
interface Settings {
theme?: string;
notifications?: boolean;
}
const config: Required = {
theme: "dark",
notifications: true
}; // both properties are now mandatory Readonly<T>
Makes every property immutable after creation.
interface Point {
x: number;
y: number;
}
const p: Readonly = { x: 5, y: 10 };
p.x = 20; // Error: Cannot assign to 'x' because it is a read-only property Pick<T, Keys>
Creates a new type using only the selected properties from an existing type.
interface User {
name: string;
age: number;
email: string;
}
type UserPreview = Pick;
const preview: UserPreview = { name: "Bob", email: "bob@example.com" }; Omit<T, Keys>
The opposite of Pick — creates a new type excluding the selected properties.
type UserWithoutEmail = Omit;
const noEmailUser: UserWithoutEmail = { name: "Charlie", age: 40 }; Record<Keys, Type>
Builds an object type where every key is of a given type and maps to a specified value type — great for dictionaries and lookup maps.
type Role = "admin" | "editor" | "viewer";
const permissions: Record = {
admin: true,
editor: true,
viewer: false
}; Quick Reference Table
| Utility Type | What It Does |
|---|---|
Partial<T> | Makes all properties optional |
Required<T> | Makes all properties required |
Readonly<T> | Makes all properties immutable |
Pick<T, K> | Selects a subset of properties |
Omit<T, K> | Removes a subset of properties |
Record<K, V> | Builds a dictionary-style object type |
Configuring a Project with tsconfig.json
Real TypeScript projects use a tsconfig.json file to control how the compiler behaves — which files to include, which JavaScript version to target, and how strict the type checking should be.
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"strict": true,
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src/**/*"]
}| Option | Purpose |
|---|---|
target | Which JavaScript version to compile down to |
strict | Enables the full set of strict type-checking rules (recommended) |
outDir | Where compiled .js files are placed |
rootDir | Where your source .ts files live |
Best practice: Always enable"strict": truein new projects. It turns on stricter null checks and type enforcement that catch far more bugs early.
Course Recap
Congratulations — you've completed the TypeScript Basics course! You now understand:
- What TypeScript is and how it compiles to JavaScript
- Basic types, type inference, and union types
- Typing functions with optional, default, and rest parameters
- Interfaces and type aliases for describing object shapes
- Arrays and tuples
- Enums for fixed sets of named constants
- Classes, access modifiers, and inheritance
- Generics for flexible, type-safe reusable code
- Union/intersection types and type narrowing
- Utility types and basic project configuration
Next steps: From here, explore TypeScript with real frameworks like React, Node.js with Express, and advanced typing patterns like mapped and conditional types.