Skip to content
BENIBIX Learn

Type Annotation

Also known as Type Hint, Type Hints, Type Declaration

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

Think of it like

Think of it like a label on a jar. It says sugar, so nobody fills it with salt.

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.

Definition

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 code

python
def area(width: int, height: int) -> int:
    return width * height

print(area(3, 4))  # 12
The hints say both inputs and the answer are whole numbers.

Common mistake

Expecting Python's hints to stop a wrong value on their own. Python ignores them at run time; a separate checker reads them.

Key takeaways

  1. 1
    A label on a name. Says what kind it holds.
  2. 2
    Read by checkers. They warn on a mismatch.
  3. 3
    Read by people. Shows what a function wants.

Compare