Annotate variable

2 snippets across 2 stacks — Python, TypeScript

PYPython

Type Hints / Annotations

PY · Variables & Types
syntax
name: type = value
example
price: float = 19.99
items: list[str] = ["apple", "bread"]
user_map: dict[str, int] = {"alice": 1}
output
# No runtime enforcement — hints are for tools and readers

Note Type hints do not prevent wrong types at runtime. Use mypy or pyright for static checking. Since 3.12, you can use the new type statement for aliases.

TSTypeScript

Variable Annotations

TS · Type Annotations
syntax
let varName: Type = value;
const varName: Type = value;
example
let orderId: string = "ORD-7821";
const maxRetries: number = 3;
let tags: string[] = ["urgent", "billing"];

// Often unnecessary when the type is obvious
let count = 0; // inferred as number
const label = "total"; // inferred as literal "total"
output
// Explicit annotations override inference when needed

Note Let TypeScript infer when the type is obvious from the initializer. Add explicit annotations when: the inferred type is too wide, you are declaring without initializing, or you want documentation clarity.