Skip to content
BENIBIX Learn

Type Annotation vs Type Inference

People think a typed language makes you write a type on every name, and it does not. A type annotation is a type you write yourself, such as count: number. Type inference is the language working the type out from the value, so let count = 0 is already a number. Most code uses both, with notes where they help the reader.

Development🧭 Programming Paradigms

Type Annotation

A note that says what kind of value a name holds, like count: number.

A type annotation is a note in the code that says what kind of value a name holds. In TypeScript you write a colon and the type, like count: number. Python has type hints that look the same, like width: int. A checker reads them and warns when a value of the wrong kind is used. They also tell the next reader what each function expects and gives back.

Example
Jack marks area to take two whole numbers and give one back. When he passes it a word by mistake, his editor shows a red line.
Why it matters
Annotations turn guesses into checked facts. Editors also use them to suggest the right names as you type.
Full entry
Development🧭 Programming Paradigms

Type Inference

The language works out each value's type. You do not have to write it.

Type inference is when the language works out a value's type without you writing it. If you write let count = 5, TypeScript knows count is a number. From then on it holds you to that, so putting text in count is an error. You get the safety of types with less typing. Go, Rust, Kotlin, Swift and TypeScript all do it.

Example
Emma writes let count = 5 and later sets count to the word five. TypeScript stops her, though she never wrote number anywhere.
Why it matters
It keeps typed code short. You write types where they help a reader, and let the language fill in the rest.
Full entry

In one line

Type Annotation
A note that says what kind of value a name holds, like count: number.
Type Inference
The language works out each value's type. You do not have to write it.