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

Basic types and inference

Annotating variables

Add : type after a variable name:

TypeScript · editable

The primitive types match JavaScript's: string, number (integers and decimals), boolean, null, undefined, bigint and symbol.

Type inference: let TypeScript work it out

You don't need to annotate everything. TypeScript infers types from values:

TypeScript · editable

Rule of thumb: let TypeScript infer the types of local variables. Add annotations to function parameters, return types of public functions, and anywhere the inferred type isn't what you want.

Arrays

TypeScript · editable

any: the escape hatch (avoid it)

any switches type checking off for a value. Anything goes, so bugs slip through:

TypeScript · editable

That example compiles but crashes when run. Use any only as a temporary measure while migrating JavaScript code.

unknown: the safe "anything"

unknown also accepts any value, but you must check its type before using it:

TypeScript · editable

Use unknown for data from outside your program: JSON from an API, user input, catch errors.

void and never

TypeScript · editable
  • void means "this function doesn't return a value".
  • never means "this can never happen": a function that always throws, or an impossible branch. You'll see why it's useful in the narrowing lesson.

Type assertions with as

Sometimes you know more than TypeScript. An assertion tells the compiler to trust you:

TypeScript · editable

⚠️ Assertions don't check anything at runtime. If you're wrong, you'll crash. Prefer real checks (narrowing), or a validation library like Zod for external data.

Practice

Add types so that every line marked ✗ produces an error and every ✓ line compiles. Don't use any.

TypeScript · editable
✎ Check your understanding

1.What type does TypeScript infer for let total = 0?

2.What's the difference between any and unknown?

← Why TypeScript?