Paradigms: objects, functions and types · Part 12 of 36
Static types and design principles
Intermediate 15 min
How TypeScript and Python type hints catch bugs early, and the design principles that keep code changeable.
What you will learn
- You can add type annotations in TypeScript and Python.
- You can explain what a type checker does and does not guarantee.
- You can apply cohesion, coupling and single responsibility to a design.
Before this: Functional programming: values in, values out
Dynamic vs static typing
JavaScript and Python check types only at run time: "5" * {} fails (or does something odd) when that line runs — maybe in front of a user. Statically typed languages such as Java, C#, Go, Rust and TypeScript check types before the program runs, so a whole class of mistakes is found while you type.

TypeScript and type hints
TypeScript is JavaScript plus type annotations; a compiler checks them and strips them away. (Node.js can now run many .ts files directly by stripping the types — enabled by default since Node.js 23.6 in January 2025 — but checking still needs the tsc compiler.)
interface Bed { name: string; areaM2: number; plants: string[] }
function totalArea(beds: Bed[]): number {
return beds.reduce((s, b) => s + b.areaM2, 0);
}
totalArea([{ name: "north", areaM2: "3.6", plants: [] }]);
// ^ error: string is not assignable to number
Python has had type hints since 3.5. The interpreter ignores them; tools such as mypy or pyright check them, and editors use them for autocomplete.
from dataclasses import dataclass
@dataclass
class Bed:
name: str
area_m2: float
plants: list[str]
def total_area(beds: list[Bed]) -> float:
return sum(b.area_m2 for b in beds)
Useful type features you'll meet: union types (string | null, str | None) that force you to handle the "missing" case, generics (Array<T>, list[T]) for containers of any type, and literal types ("basic" | "expert") that restrict values.
What types do not guarantee
A type checker proves that values have the right shape, not that your logic is right: totalArea would also type-check if it multiplied instead of adding. Data from outside (JSON, forms, APIs) has no type until you validate it at the boundary; TypeScript's JSON.parse returns any. Types and tests complement each other.
Design principles that last
Types help most when the design is clear. A few principles that have survived decades:
- High cohesion: a module or class does one well-defined job. If you describe it with "and", consider splitting it.
- Low coupling: modules know as little as possible about each other's internals; they talk through small interfaces.
- Single responsibility: a piece of code has one reason to change. Parsing, calculating and displaying are three reasons.
- Don't repeat yourself (DRY) — but don't merge things that only look alike today. Wrong abstractions are expensive.
- Make illegal states unrepresentable: choose types so that impossible combinations can't be written down (a
"loading" | "done" | "error"status instead of three booleans).
These are part of the well-known SOLID principles for object-oriented design; the same thinking applies to functional code.
Key points
- Static type checking finds type mistakes before the program runs.
- TypeScript adds types to JavaScript; Python type hints are checked by tools such as mypy or pyright.
- Union types, generics and literal types describe data precisely.
- Types check shapes, not logic — validate outside data and still write tests.
- Aim for high cohesion, low coupling, single responsibility; make illegal states unrepresentable.
Flashcards
- Question: Does Python enforce type hints at run time?
- Answer: No — tools like mypy or pyright check them; the interpreter ignores them.
- Question: What does a union type like string | null force you to do?
- Answer: Handle the case where the value is missing.
- Question: What is low coupling?
- Answer: Modules depend on each other only through small, stable interfaces.
- Question: Why validate JSON even in TypeScript?
- Answer: Parsed outside data has no checked type; the compiler cannot see it.
Check yourself
Think about your answer, then open it. In the app the quiz is interactive, earns points and brings back what you missed.
When does TypeScript report a type error?
- When the compiler/type checker runs, before execution
- Only when the line runs in the browser
- Never — types are comments
- Only in production
Show the answer
When the compiler/type checker runs, before execution
TypeScript checks types at compile time and then removes them.
Source: TypeScript Handbook
Which status type makes illegal states unrepresentable?
- "loading" | "done" | "error"
- three booleans isLoading, isDone, isError
- any
- string
Show the answer
"loading" | "done" | "error"
One union value can only be one state at a time; three booleans allow impossible combinations.
Source: MIT 6.031
Which statements about Python type hints are true?
- The interpreter ignores them at run time
- mypy or pyright can check them
- They make the program run faster automatically
- Editors use them for autocomplete
Show the answer
The interpreter ignores them at run time; mypy or pyright can check them; Editors use them for autocomplete
Hints are metadata for tools and editors; CPython does not enforce them or speed up because of them.
Source: Python docs, typing
A class that does one well-defined job has high ___.
Show the answer
cohesion
Cohesion measures how closely the parts of a module belong together.
Source: MIT 6.031
Sources
- TypeScript Handbook, Microsoft, 2025
- Python documentation: typing — Support for type hints, Python Software Foundation, 2025
- MIT 6.031 Software Construction, Reading 2: Basic Java / Static Checking, MIT, 2023
Facts checked on 2026-11-30. Skillgrove lessons are free learning material — no certificate and no accreditation.