What is Python's typing.TypedDict?
Why Interviewers Ask This
Advanced questions like this reveal whether a candidate has internalized Python deeply enough to make architectural decisions. Strong answers demonstrate both breadth and depth of experience.
Answer
TypedDict (Python 3.8+, typing.TypedDict) allows defining dict shapes with type annotations, enabling static type checkers to validate dict access. from typing import TypedDict; class User(TypedDict): name: str; age: int; email: str. Using total=False makes all keys optional: class Config(TypedDict, total=False): debug: bool; port: int. Mix required and optional with inheritance: class RequiredFields(TypedDict): name: str; class OptionalFields(RequiredFields, total=False): age: int. TypedDict provides no runtime validation — it is purely a type hint mechanism for static checkers like mypy and pyright. At runtime, a TypedDict is just a regular dict. Use TypedDict for: API response structures, configuration dicts, function parameters that accept dicts with known shapes. For runtime validation, use Pydantic BaseModel instead. @dataclass is often better than TypedDict for new code since it provides a proper class with attribute access.
Pro Tip
If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.