Type Systems & Type Safety
Variables & Data Types · Coding Fundamentals track
6 min read
In this module
What Is a Type System?
A type system is a set of rules a programming language uses to classify values, check that operations make sense, and prevent certain categories of errors. When you write age + 1, the type system verifies that age holds a number and that the + operator is defined for numbers.
Without a type system, you could accidentally try to multiply a string by a boolean, divide an array by a character, or pass a number where a file handle is expected. Type systems catch these nonsensical operations before they cause damage.
Static vs. Dynamic Typing
The biggest divide in type system design is when type checking happens.
Statically-typed languages check types at compile time, before your program ever runs. Write code that breaks the type rules and the compiler refuses to compile it.
// In a statically-typed languagelet count: int = 5count = "hello" // COMPILE ERROR: cannot assign string to intExamples: C, C++, Rust, Go, Java, TypeScript.
Dynamically-typed languages check types at runtime, while the program executes. Variables can hold values of any type, and type errors only surface when the problematic line actually runs.
// In a dynamically-typed languagecount = 5count = "hello" // No error - the variable now holds a stringcount + 1 // RUNTIME ERROR: cannot add string and integerExamples: Python, JavaScript, Ruby, Lua.
Static typing catches type errors before the program runs, providing stronger safety guarantees. Dynamic typing offers more flexibility at the cost of discovering type errors only at runtime. Neither approach is universally better; the right choice depends on the project's needs.
Strong vs. Weak Typing
Orthogonal to the static/dynamic distinction is how strictly a language enforces its type rules.
Strongly-typed languages refuse to perform operations on mismatched types. Try to add a string and a number and you get an error.
// Strongly typed"5" + 3 // ERROR: cannot add string and integerWeakly-typed languages perform implicit conversions (also called coercions) to make operations work, even when the types don't match.
// Weakly typed"5" + 3 // JavaScript: "53" (the 3 becomes a string)"5" - 3 // JavaScript: 2 (the "5" becomes a number)The rules are fixed, so the answer is always the same, but the same-looking operators convert in opposite directions. Weak typing is convenient for quick scripts and a steady source of subtle bugs. The programmer has to keep track of which conversions happen automatically.
Type Inference
Modern statically-typed languages often support type inference: the compiler deduces the type from context so you don't have to write it out.
let x = 42 // compiler infers: x is an integerlet name = "Alice" // compiler infers: name is a stringlet ratio = 3.14 // compiler infers: ratio is a floatYou get the safety of static typing without writing type annotations everywhere. The compiler still fully checks every type; it just works out what those types are on your behalf.
Type Casting and Conversion
Sometimes you need to convert a value from one type to another. The flavours:
- Widening (safe): converting a smaller type to a larger one, like int32 to int64. No information is lost.
- Narrowing (dangerous): converting a larger type to a smaller one, like int64 to int32. Values that don't fit get truncated or cause errors.
- Explicit casting: you tell the compiler to perform a conversion, accepting the risk yourself.
let big: int64 = 1000000let small: int8 = (int8)big // narrowing - value will be wrong!Knowing when conversions are safe and when they hide bugs ties straight back to the integer overflow and precision concepts from the previous lesson.
Why Type Systems Matter
Type systems are one of the primary tools we have for writing correct software:
- Bug prevention. Entire categories of errors (null dereferences, type mismatches, integer overflows) can be eliminated at compile time by a sufficiently expressive type system.
- Documentation. Type annotations serve as machine-checked documentation. When you see
function calculateArea(width: float, height: float) -> float, you immediately know what the function expects and returns. - Performance. Knowing the exact type of every variable lets the compiler emit efficient machine code. It can pick the right CPU instructions, allocate the right amount of memory, and skip runtime type checks.
- Refactoring safety. Change a type or a function signature and the compiler will surface every place in the codebase that needs updating.
As you learn more programming concepts in the modules ahead, the type system will keep returning, shaping how you write functions, define data structures, and reason about correctness.
Types at the Machine Level
At the lowest level, memory is just bytes. The CPU doesn't inherently distinguish between an integer, a float, a pointer, and a character. In a compiled language like C, types exist only in the compiler, which uses them to pick instructions: declare a variable as float and you get floating-point instructions, declare it as int and you get integer ones. By the time the program runs, the types are gone. (Dynamic languages keep a type tag next to each value at runtime, which is how they can report those runtime type errors.) The danger in C comes from reinterpretation. Read a float's bytes through a union or a pointer cast to int and the compiler emits integer instructions for bits laid out as a float, and the CPU cheerfully produces garbage without any error. Understanding types means grasping both the abstract rules they enforce and the concrete machine behaviour they control.
The visualiser below holds one 32-bit pattern and reads it three ways: as a signed int, as a float, and as four bytes of text in the order a little-endian x86 or ARM machine stores them. Start on 0x64636261, which is the string "abcd" as an int of about 1.68 billion and a float near 1.7 × 10²². Load the 1.0f preset and the same bits that mean 1.0 to the FPU become the integer 1065353216. Nothing in the bits says which reading is right.
Bytes in memory on a little-endian CPU, lowest address first