Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

TS
TypeScript
0/10 completed
Beginner
Why TypeScript?Basic types and inferenceTyping functionsObjects, interfaces and type aliases
Intermediate
Unions and narrowingLiteral types, tuples and enumsGenerics
Advanced
Utility typesMapped and conditional types
Pro
TypeScript in real projects
Tracks / TypeScript / Beginner

Typing functions

Functions are where types pay off most: they document exactly what goes in and what comes out, and they catch every caller that gets it wrong.

Parameters and return types

TypeScript · editable

Return types can usually be inferred, but writing them on exported functions is good practice: it stops you from accidentally returning the wrong thing.

TypeScript · editable

TypeScript reports: "Function lacks ending return statement and return type does not include 'undefined'." Add return 0; at the end to fix it.

Optional and default parameters

TypeScript · editable

Inside the function, an optional parameter has the type string | undefined, so TypeScript makes you handle the missing case.

Rest parameters

TypeScript · editable

Function types

Functions are values, so they have types too. This is essential for callbacks:

TypeScript · editable

Notice that in ops, the arrow functions didn't need annotations. TypeScript used the contextual type MathOp. The same happens with .map, .filter, event handlers and so on.

Typing objects as parameters

TypeScript · editable

You'll learn to name these object shapes with interface and type in the next lesson.

Async functions

An async function always returns a Promise<T>:

TypeScript · editable

Overloads (briefly)

When a function behaves differently depending on its arguments, you can declare several signatures:

TypeScript · editable

In practice, union types (next lessons) are often simpler than overloads.

Practice

Type this small cart module. Every function should have typed parameters and an explicit return type, and the last line should be an error.

TypeScript · editable
✎ Check your understanding

1.What does greeting?: string mean in a parameter list?

2.What is the return type of async function getAge(): ... that returns a number?

← Basic types and inference