ReviseAlgo Logo

Type Hints & Annotations

Type Aliases

Creating reusable type shortcuts — named types for complex annotations, improving readability and reducing repetition in type hints.

Interview: Code readability — shows ability to make complex type systems manageable.

Last Updated: June 12, 2026 5 min read

Type aliases give names to complex type expressions, making code more readable and reducing repetition. Instead of writing dict[str, list[dict[str, int | str]]] everywhere, define APIResponse = dict[str, list[dict[str, int | str]]] once.

Creating Aliases

  • Vector = list[float] — simple assignment (works in all versions)
  • type Vector = list[float] — explicit declaration (Python 3.12+)
  • Type aliases are just variables — no runtime overhead
  • Use UPPER_CASE for aliases that act like constants

Interview Insight

Type aliases improve code readability. Use them for complex nested types that appear in multiple places. In Python 3.12+, the type keyword makes aliases explicit.

Use Cases

Domain modeling — Email, UserID, Score as named types

API types — complex response/request structures

Mathematical code — Vector, Matrix, Point types

Callback signatures — Handler, Validator, Transformer types

Configuration types — complex nested config structures

Common Mistakes

Creating aliases for simple types — just use int, str directly

Not updating aliases when the underlying type changes

Confusing TypeAlias (the marker) with the alias itself

Using NewType when a simple alias suffices — NewType has runtime cost in isinstance checks

Creating too many aliases — balance between readability and simplicity