JSTAcademy
0 XP
Dashboard
Crash Courses
TypeScript Fundamentals — Types & Inference
10 min
Basic+150 XP
Crash Courses · Basic

TypeScript Fundamentals — Types & Inference

Understand static typing, type inference, and why TypeScript catches bugs before runtime.
10 min read+150 XP on completionCert: TypeScript Crash Course
Tap any word in the text below to start reading from there.

TypeScript Fundamentals

TypeScript is JavaScript with types. It catches bugs at compile time, provides autocomplete, and makes large codebases maintainable.

Primitive Types

const name: string = 'Jordan'
const age: number = 28
const active: boolean = true

// Inference — TypeScript figures out the type
const score = 100     // inferred: number
const label = 'Pro'   // inferred: string

Function Types

function greet(name: string): string {
  return `Hello, ${name}`
}

const add = (a: number, b: number): number => a + b

function logError(msg: string): void {
  console.error(msg)
}

// Optional parameter
function greetUser(name: string, title?: string): string {
  return title ? `${title} ${name}` : name
}

Arrays and Tuples

const names: string[] = ['Jordan', 'Owen']
const ids: number[] = [1, 2, 3]

// Generic syntax
const tags: Array<string> = ['react', 'typescript']

// Tuple — fixed positions and types
const coord: [number, number] = [12.5, -77.0]

any vs unknown

// any — disables type checking, avoid
let bad: any = 'hello'
bad.toUpperCase()   // no error even if bad is a number

// unknown — safe, must narrow before use
let input: unknown = getInput()
if (typeof input === 'string') {
  console.log(input.toUpperCase())  // OK after narrowing
}

Type Aliases

type UserId = string
type Status = 'active' | 'inactive' | 'pending'

const status: Status = 'active'
// const bad: Status = 'deleted'  // Error — not in union
0%