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 missing

Built-in Utility Types

Utility TypeDescriptionExample
Partial<T>All properties optionalPartial<User>
Required<T>All properties requiredRequired<Config>
Readonly<T>All properties readonlyReadonly<User>
Pick<T, K>Keep only selected keysPick<User, 'name' | 'id'>
Omit<T, K>Remove selected keysOmit<User, 'password'>
Record<K, V>Object with key type K, value type VRecord<string, number>
Exclude<T, U>Exclude types assignable to UExclude<'a'|'b'|'c', 'a'>
Extract<T, U>Keep only types assignable to UExtract<string|number, string>
NonNullable<T>Remove null and undefinedNonNullable<string | null>
ReturnType<T>Get function return typeReturnType<typeof fetch>
Parameters<T>Get function param types as tupleParameters<typeof add>
Awaited<T>Unwrap Promise typeAwaited<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: true in tsconfig.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.