Tooling & Build
TypeScript Introduction
TypeScript adds a static type system to JavaScript, catching bugs at compile time rather than runtime. It's now the default for most professional JavaScript projects — React, Node.js, Next.js, and beyond.
What is TypeScript and should I learn it?
TypeScript is JavaScript with type annotations. You describe what shape your data has (name: string, age: number), and the compiler catches mistakes like typos, wrong arguments and missing properties before the code runs. It compiles to plain JavaScript. Most professional front-end and Node.js projects use it, so learn it once you're comfortable with JavaScript.
type User = { id: number; name: string; email?: string };
function greet(user: User): string {
return `Hello, ${user.name}`;
}
greet({ id: 1, name: "Ada" }); // OK
greet({ id: 1 }); // Error: 'name' is missingBuilt-in Utility Types
| Utility Type | Description | Example |
|---|---|---|
Partial<T> | All properties optional | Partial<User> |
Required<T> | All properties required | Required<Config> |
Readonly<T> | All properties readonly | Readonly<User> |
Pick<T, K> | Keep only selected keys | Pick<User, 'name' | 'id'> |
Omit<T, K> | Remove selected keys | Omit<User, 'password'> |
Record<K, V> | Object with key type K, value type V | Record<string, number> |
Exclude<T, U> | Exclude types assignable to U | Exclude<'a'|'b'|'c', 'a'> |
Extract<T, U> | Keep only types assignable to U | Extract<string|number, string> |
NonNullable<T> | Remove null and undefined | NonNullable<string | null> |
ReturnType<T> | Get function return type | ReturnType<typeof fetch> |
Parameters<T> | Get function param types as tuple | Parameters<typeof add> |
Awaited<T> | Unwrap Promise type | Awaited<Promise<string>> |
Common Pitfalls
Using 'any' everywhere — defeats the entire purpose of TypeScript, no type safety at all
Use 'unknown' instead of 'any'. Narrow it with type guards before use. Use 'noImplicitAny' in tsconfig.
Type assertions (as X) without validation — can cause runtime crashes if wrong
Prefer type guards: 'if (typeof x === "string")'. Use assertions only when you're 100% certain.
Non-null assertion operator (!) without checking — if value is actually null, you get a runtime error
Check before using: 'if (element !== null) element.focus()' instead of 'element!.focus()'
Keeping 'strict: false' in tsconfig — misses many type errors, partial TypeScript benefits
Enable 'strict: true' in tsconfig.json from the start. It enables strictNullChecks, noImplicitAny, etc.
Interactive Demo
Code Editor
<p>TypeScript (compile-time types) and the JavaScript it becomes:</p>
<div class="cols">
<pre>// user.ts
type User = { id: number; name: string };
function greet(user: User): string {
return `Hi ${user.name}`;
}
greet({ id: 1, name: "Ada" }); // ✓
greet({ id: 1 });
// ✗ Error: Property 'name' is missing</pre>
<pre>// user.js (output)
function greet(user) {
return `Hi ${user.name}`;
}
greet({ id: 1, name: "Ada" });
greet({ id: 1 }); // "Hi undefined" at runtime</pre>
</div>Preview
TypeScript Introduction: summary
Key points
- Types are erased at compile time; they don't validate data at runtime (use Zod or similar for that).
- Type inference means you rarely need to annotate local variables.
- Start with
strict: trueintsconfig.json. - Your editor uses the types for autocomplete and refactoring, even in JavaScript files.
Frequently asked questions
›Should I learn JavaScript or TypeScript first?
JavaScript first. TypeScript adds types on top of JavaScript, so you need the underlying language before the types make sense.