Lesson 1 · Module 0.1 — Reading TS syntax
: and the =>One confusion comes up again and again when you start reading TypeScript: after a
:, is that a return type or a variable type? And does
=> mean “arrow function” or something else? Both questions have the same
answer — position decides meaning. Learn the two positions and TS syntax
stops being ambiguous.
Every piece of TypeScript is in one of two worlds:
type position (annotations — erased at runtime) or
value position (real JavaScript — survives at runtime).
The same symbols (:, =>) mean different things in each.
Find the world first; the meaning follows.
: starts a type, = starts a valueHere is the core maxim, made precise:
Everything after a:and before the next=is type information. Everything after=is a real runtime value.
const add: (a: number, b: number) => number = (a, b) => a + b;
// └──────────── TYPE ────────────┘ └─── VALUE ───┘
The left => lives in the type; the right => lives in the value.
They are not the same thing — see below.
: means three things — told apart by where it sitswhere the : sits | what follows it | example |
|---|---|---|
| after a parameter name | that parameter’s type | f(x: string) |
after the parameter list ) | the return type | f(x: string): number |
| after a variable / property name | that variable’s type | const u: User |
So in a function declaration, the : before the body is a return type:
function greet(name: string): string { return `hi ${name}`; }
// ^param type ^return type
But when you annotate a variable that happens to hold a function, the whole function type is the variable’s type — and the return type is buried inside it:
const greet: (name: string) => string = (name) => `hi ${name}`;
// └ the variable's type ─┘ └──── the value ─────┘
// the return type is the `string` after the => inside the type
Same return type, two different places, because one is a declaration and the other is an annotation. That is the whole confusion, resolved.
=> means two things — told apart by world| world | => means | left / right | survives runtime? |
|---|---|---|---|
| type position | a function type | params / return type | no — erased |
| value position | an arrow function | params / function body | yes — real code |
The classic proof — look at what each compiles to:
let MakePoint: () => { x: number; y: number }; // TYPE position
// compiles to: let MakePoint; (the whole annotation is erased)
const MakePoint = () => ({ x: 1, y: 2 }); // VALUE position
// compiles to: const MakePoint = () => ({ x: 1, y: 2 }); (real function survives)
Top one is a description of a function (a function type); bottom one is a
function. The => looks identical but lives in different worlds.
:, or inside a type /
interface / generic <…>? → type world.
A => here is a function type; its right side is a return type.=, or being returned / passed as an
argument? → value world. A => here is an arrow
function; its right side is the body.Here is a curried thunk (the dispatch/getState puzzle, common in
Redux) that often trips people up. Watch the boundary — same shape on each side of the
=, opposite worlds:
const addToCart:
(id: string, qty: number) => (dispatch: Dispatch, getState: () => State) => Promise<void>
// └─ TYPE (after : , before = ): every => is a function type; final return is Promise<void>
= (id, qty) => async (dispatch, getState) => { ... };
// └─ VALUE (after = ): two real arrow functions, one returning the other
On the type side, (dispatch, getState) are parameter
types and each => is a function type. On the value
side, they’re real parameters and each => is a real arrow function. The left
describes the function; the right is it.
Recall, don’t re-read. (Answers reveal on click.)
const f: (a: number) => string = (a) => "x";, the left => is…function area(r: number): number, the second : number is the…let p: () => number; compile to just let p;?The official handbook page that nails the function-type-vs-arrow distinction is
TypeScript Handbook — Function Type Expressions.
For the “types are erased” idea, see
Everyday Types.
Best way to feel it: paste both MakePoint versions into the
TS Playground and watch the .js output panel.
Got a tricky line? Decide its world first — ask “type world or value world?” Next lesson (0.2) builds straight on this: annotation vs. inference vs. definition — three “type” words that are easy to mix up.