
Statically Typed vs Dynamically Typed Languages
Short answer: in a statically typed language, the type of each variable is checked before the program runs (at compile time). In a dynamically typed language, types are checked while the program runs. Static typing catches many mistakes early, and dynamic typing gives flexibility and shorter code.
Static typing in practice
// TypeScript
let age: number = 30;
age = "thirty"; // error found before the program runs
The compiler knows age must be a number and rejects the assignment. Languages such as Java, C#, Go, Rust and TypeScript are statically typed.
Dynamic typing in practice
# Python
age = 30
age = "thirty" # allowed: the type belongs to the value, not the variable
The program works until it tries to use a value in a way that does not fit, such as adding a number to text, and then it raises an error at run time. Python, JavaScript and Ruby are dynamically typed.
Comparison
| Static typing | Dynamic typing | |
|---|---|---|
| When types are checked | Before running | While running |
| Early error detection | Better | Weaker, needs tests |
| Code length | More annotations | Shorter, flexible |
| Editor support | Strong autocomplete and refactoring | Good, but less precise |
| Prototyping speed | Slower start | Faster start |
| Large codebases | Easier to maintain | Needs discipline and tests |
Strong vs weak typing is a different idea
Static or dynamic describes when types are checked. Strong or weak describes how strictly the language prevents mixing types. Python is dynamic but fairly strong: "5" + 3 raises an error. JavaScript is dynamic and more permissive: "5" + 3 gives "53".
Gradual typing
Many dynamic languages now allow optional types. TypeScript adds static types to JavaScript, and Python supports type hints checked by tools such as mypy. This lets you start flexible and add safety as a project grows.
Which should you choose?
For large, long-lived projects with many developers, static typing usually pays off in fewer bugs and safer changes. For small scripts, experiments and data exploration, dynamic typing is quick and comfortable. Choose the approach that fits the size and risk of your project.
Examples of type errors
// Statically typed (Java): caught before running
int count = "five"; // compile error
# Dynamically typed (Python): caught when running
count = "five"
total = count + 1 # TypeError at run time
The mistake is the same, but the first is reported while you write and compile, and the second only when that line executes, possibly in production.
Key takeaways
- Static typing checks types before running, and dynamic typing checks them while running.
- Static types help large teams catch errors early and refactor safely.
- Dynamic types speed up small scripts and prototypes.
- Tools such as TypeScript and Python type hints let you add types gradually.
Type inference: static without the noise
A common belief is that static typing means writing type names everywhere. Modern statically typed languages infer most types automatically:
// TypeScript
const count = 5; // inferred as number
const names = ["Ana", "Ben"]; // inferred as string[]
// Kotlin
val price = 9.99 // inferred as Double
// C#
var orders = new List<Order>(); // inferred as List<Order>
// Go
total := 0 // inferred as int
You still get full compile-time checking, but you write types mainly at boundaries such as function parameters and public APIs, where they also serve as documentation.
Popular languages by typing
| Language | Static or dynamic | Notes |
|---|---|---|
| Java, C#, Go, Rust, Kotlin, Swift | Static | Types checked at compile time, strong inference in most |
| C, C++ | Static | Allow unsafe conversions; weaker guarantees |
| Python, Ruby, PHP | Dynamic | Optional type hints checked by external tools |
| JavaScript | Dynamic | Weak typing with implicit conversions |
| TypeScript | Static (gradual) | Types checked at compile time, removed at runtime |
Type hints in Python
def apply_discount(price: float, percent: int) -> float:
return price * (1 - percent / 100)
apply_discount("100", 10) # a type checker such as mypy flags this before you run it
Python ignores these hints at runtime, but editors and type checkers use them to catch mistakes and improve autocompletion. Many large Python codebases adopt hints gradually.
Common mistakes
- Overusing escape hatches such as
anyin TypeScript orObjectin Java, which remove the benefits of typing. - Assuming static types replace tests; they catch type errors, not wrong business logic.
- Ignoring runtime validation of external data, such as API responses, which types alone cannot guarantee.
- Writing dynamic code with no hints or tests in large projects, making refactoring risky.
Learn more in what TypeScript is, compiled vs interpreted languages and TypeScript generics.
Frequently asked questions
Is TypeScript statically typed?
Yes. TypeScript adds static type checking to JavaScript at compile time, then produces plain JavaScript that runs without the types.
Does static typing make code faster?
It can help compilers optimize, but the main benefit is catching errors early and improving tooling. Speed depends on many other factors.
Do dynamically typed languages need more tests?
Usually, yes. Tests and linters take over some of the checking that a compiler does in statically typed languages.
Which is better for beginners?
Dynamic languages such as Python let beginners focus on logic without type syntax. Static languages give clearer error messages before code runs. Either works; many people start with Python or JavaScript and later add TypeScript or a static language.
Can I add static typing to an existing JavaScript project?
Yes. TypeScript can be introduced gradually: rename files one at a time, start with loose compiler settings and tighten them as coverage grows. JSDoc comments with type checking enabled are another option that keeps plain .js files.
