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.
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.
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.
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.