Phase 7 — Type Hints
Container Hints
Describing what's inside a list or dict, not just that it is one
So far the type hints have described a single value. Containers need one more piece of information: what's inside them.
list[...]
TypeScript:
let users: string[] = ['John', 'David'];
// or: Array<string>Python:
users: list[str] = ["John", "David"]list[str] reads as "a list, of strings" — the type inside the brackets describes every
item in the list.
dict[...]
TypeScript's closest match is Record<KeyType, ValueType>:
const users: Record<number, string> = {
1: 'John',
2: 'David',
};Python:
users: dict[int, str] = {1: "John", 2: "David"}The general shape is dict[KeyType, ValueType] — key type first, value type second, in that
order.
list[ItemType]
dict[KeyType, ValueType]Keep this reading habit — it's the same pattern you'll use to build up the nested types on the next page.