Python for JS Devs · Reference

Type hints — TypeScript → Python

Modern Python 3.10+ hints. Prefer lowercase built-ins and | None.

MeaningTypeScriptPython (3.10+)
Stringstringstr
Integernumberint
Floatnumberfloat
Booleanbooleanbool
Arraystring[]list[str]
Tuple[int, int]tuple[int, int]
Dict / recordRecord<string,int>dict[str, int]
SetSet<str>set[str]
Optional / nullablestring | nullstr | None
Unionstr | intstr | int
Literal union"a" | "b"Literal["a", "b"]
AnyanyAny (from typing)
Function param(n: string)(n: str)
Return type): string) -> str:
Optional paramrole?: stringrole: str | None = None
Callable(x: int) => strCallable[[int], str]
Variablelet x: int = 3x: int = 3
Full function
def summarize(scores: list[int], label: str | None = None) -> dict[str, int]:
    return {"count": len(scores), "total": sum(scores)}
Remember Plain hints are not enforced at runtime (like TS — erased) — a checker (mypy/ty/pyright) verifies them in dev. The exception is Pydantic/FastAPI, which read hints at runtime to validate. Legacy code uses Optional[str] / List[str] from typing — prefer the lowercase forms above.
Run a checker with uv uvx ty check or uvx mypy . — no install needed.