TypeAlias creates readable names for complex types.
1from typing import TypeAlias23# Simple alias4UserID: TypeAlias = int5JSON: TypeAlias = dict[str, any]67# Complex type8Handler: TypeAlias = Callable[[str, int], bool]9Result: TypeAlias = tuple[bool, str | None]1011# With generics12from typing import TypeVar, Generic1314T = TypeVar("T")15Response: TypeAlias = dict[str, T]1617# Usage18def get_user(user_id: UserID) -> JSON:19 return {"id": user_id}2021def register_handler(handler: Handler) -> None:22 handlers.append(handler)2324# In function signatures25def process(26 data: JSON,27 callback: Handler28) -> Result:29 # ...30 return (True, None)3132# Python 3.12+ syntax33type UserID = int34type JSON = dict[str, any]
Benefits: