Course contentsShow
← Computer Science
Lesson 1999 of 2,87245. Type Systems and SemanticsFree lesson

Type Checking at Runtime

Explore how dynamically typed languages defer type checking until program execution.

Type Checking at Runtime

What you'll learn: How dynamically typed languages postpone type verification until your program actually runs.

The Core Idea

In statically typed languages, the compiler checks types before any code executes. But in dynamically typed languages like Python, JavaScript, or Ruby, type checking happens at runtime—meaning types are verified only when the program encounters each operation during execution.

Think of it like a restaurant kitchen: static typing is when the chef checks all ingredients before starting to cook (compile time), while dynamic typing is when the chef grabs each ingredient as needed and only then verifies it's correct (runtime).

How It Works

When your program runs, the interpreter or runtime environment tracks the type of each value. Before performing an operation—like adding two numbers or calling a method—the runtime checks whether the types are compatible. If they're not, you get a runtime type error.

x = "hello"
y = 5
result = x + y  # Type error raised HERE, during execution

The runtime doesn't know there's a problem until it tries to add a string and integer. This delayed checking means type errors surface only when (and if) that particular code path executes.

Tradeoffs

Flexibility: You can write code quickly without type annotations, and the same variable can hold different types at different times.

Late failure: Bugs hide until execution reaches them—a rarely-used code path might contain type errors that won't appear until months later in production.

Key Takeaway: Runtime type checking defers all type verification until program execution, trading compile-time safety for flexibility and faster initial development.