Programming languages · intermediate
Static vs dynamic typing
Static typing checks type constraints using program information before the relevant execution, while dynamic typing associates types with runtime values and checks applicable operations during execution. Languages can combine these approaches through inference, runtime checks, optional annotations, or gradual typing.
Why it matters
Typing strategy changes which mistakes tools catch early, how APIs document constraints, what runtime checks remain, and how flexible code evolution feels. It does not by itself determine safety, performance, or code quality.
Mental model
How to reason about static vs dynamic typing
Ask when a specific invalid operation is rejected and what evidence the checker uses. Static analysis proves selected constraints without running that path; dynamic checking validates an operation against the actual values that reach it.
Analogy
Static checking is reviewing permitted connector shapes in a blueprint; dynamic checking tests the actual connectors when assembly reaches that step. A project can use both reviews and runtime tests.
Examples
See the boundary, not just the happy path
Worked example · Static checker rejects an assignment
let count: number = 'three';TypeScript reports this mismatch during checking. Its ordinary type annotations are erased from emitted JavaScript, so separate runtime validation is still needed for untrusted external values.
Worked example · Dynamic operation fails when reached
'3' + 4.0 # TypeError in PythonPython knows the runtime types of the values and rejects this unsupported addition when execution evaluates it. An unexecuted path does not raise that error.
Useful contrast · Type inference remains static typing
let count = 3A static language can infer count's type without an annotation and still check later uses before execution. Annotation volume and checking time are separate properties.
Common mistakes
Misconceptions to remove early
Equating dynamic typing with no types
Runtime values still have types and operations enforce type rules. The distinction concerns when and how constraints are checked, not whether types exist.
Assuming static types validate external data
Network responses, files, reflection, unsafe operations, and foreign interfaces can introduce unchecked values. Programs need runtime validation at trust boundaries even with strong static checking.
Quick check
Can you predict the result?
1. Does omitting an explicit annotation necessarily make a language dynamically typed?
- • No; a static checker can infer the type and still verify uses before execution.
- • Yes; static typing requires every type to be written by hand.
- • Yes; inference removes all type constraints.
2. Why must a statically typed program still validate parsed JSON from the network?
Keep building