TypeScript Function Types: Parameters, Overloads, void
Type parameters, optional and default values, rest arguments, void vs undefined, function type expressions and overloads, with examples.
In this chapter
Typing functions
More on Everyday types
- Why TypeScript? Benefits Over JavaScript, Explained
- TypeScript Types: Primitives, Arrays and Tuples
- TypeScript Interface vs Type Alias, and Object Types
- Structural Typing and Excess Property Checks in TypeScript
- any vs unknown vs never in TypeScript
- TypeScript readonly, as const and Type Widening
- TypeScript Classes: private, #private, abstract, implements
- TypeScript Enums vs Union Types: Should You Use Enums?
- Annotate or let TypeScript infer
Keep learning
1. The problem: arguments in the wrong order
A booking service takes a time and a party size. In plain JS, if you swap them, the code still runs and books the wrong thing. Nothing warns you. plain JavaScript → 'Table for 19:30 at 4' Runs fine. Books the wrong thing. typed parameters Argument of type 'number' is not assignable to parameter of type 'string'. → string (inferred)
2. Optional and default parameters
type inside the body tip: number callers must pass it tip?: number number | undefined callers may skip it tip: number | undefined callers must pass it, even undefined Does line 2 compile under strict? no: 'tip' is possibly 'undefined'
optional ones go last
A required parameter cannot follow an optional parameter. channel: string Why line 1 fails: arguments fill slots left to right. If you skip channel, your text would land in the channel slot. So optional slots must come last.
3. Rest parameters
A rest parameter (...name) collects the remaining arguments into an array. skus: string[] ok: skus is [] Argument of type 'number' is not assignable to parameter of type 'string'. Line 7 fails: every extra argument must fit string, and 2 is a number. A tuple is an array with a fixed length and a type per slot. Use one as the rest type to fix both.
a tuple type gives the rest args fixed slots
Argument of type 'number' is not assignable to parameter of type 'string'. Line 4 fails: slot 1 (event) must be a string, but 120 comes first.
4. void vs undefined
void means "ignore what comes back". undefined means "returns exactly undefined". declared : void Type 'number' is not assignable to type 'void'. push returns number: allowed : undefined r: undefined Use undefined only when callers rely on the value being undefined. Otherwise prefer void.
5. Function types: type the whole function at once
repeat every signature Three copies of the same signature to keep in sync. one type, applied to each a: number, b: number You did not annotate a or b, yet they are numbers. TypeScript reads them from MathOp, the type on the left. This is called contextual typing.
a fetch that throws on HTTP errors
input: RequestInfo | URL, init?: RequestInit declare once apply to the expression
6. Overloads, a first look
No overload matches this call. Overload 1 of 2: Argument of type 'boolean' is not assignable to parameter of type 'number'. Overload 2 of 2: … not assignable to parameter of type 'string'. Line 10 fails: TypeScript tries line 1 (number) and line 2 (string). true fits neither. Line 3 accepts number | string, but callers cannot use it directly. Callers see only the overloads Line 3 (the implementation) is hidden from them. Use a union param instead: price(input: number | string). Return depends on input Overloads or generics (types with a fill-in slot like <T>). Covered later.
Remember
x?: T is T | undefined inside. A default removes the undefined. Optional parameters go after required ones. ! Cheat code
More courses
- Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
- Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
- React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
- Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
- System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
- NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
- Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
- Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
- AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
- Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
- CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
- Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
- Docker — What an image is, why layers cache, and how containers find each other on a network.
- Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
- Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
- AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
- Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.