Type checking verifies that a program uses values in ways its type system allows. A type describes which operations a value supports. The language’s rules determine which types are compatible when assigning a value, passing an argument to a function or using its result. Two types can be compatible even when they have different names.
Consider a JavaScript program that stores a greeting and then tries to call it:
const message = "Hello World!";
message();
The first line gives message a string value. The parentheses on the second line ask JavaScript to call that value as a function. The text of the greeting is irrelevant. Changing it to another string leaves the same error, because a function call requires a callable value.
Static and Runtime Checking
A static type checker examines the source before it runs. TypeScript can infer from the first line that message holds a string, even though the author supplied no type annotation. It flags the call because a string has no call signature. A developer can find the mistake in an editor or during a build without writing a test that reaches the line.
Dynamic type checking happens as the program executes. When JavaScript reaches message(), it uses the actual value and raises a TypeError. If the call sits inside a branch the program never takes, that execution never attempts the call. A passing test for another branch therefore says little about this one.
The example separates two questions. Can the checker identify a problem from the source, and what happens when a particular input reaches the operation? They agree about the string call. Elsewhere a checker can flag a use even if a particular run succeeds. Whether a diagnostic prevents a build from producing runnable code depends on the tool and its settings.
Annotations, Inference, and Checking Tools
Type annotations state which types a variable, parameter or return value is expected to have. They give authors a way to document expectations at a function boundary. A checker can compare arguments at the call site with the parameter types and check the function body against its return type. Inference supplies type information from the code itself, as the greeting example shows. Writing a type next to every name is unnecessary when the checker can infer it.
Python remains dynamically typed when a developer adds type hints. An optional static checker such as mypy can use the hints and the types it infers to flag incompatible calls. Running Python alone does not enforce a function annotation. For example, a function annotated to accept an integer can still receive a string at runtime. What happens next depends on the operations its body performs and any validation the application provides.
Gradual typing lets a program mix statically checked and dynamically typed parts. A function with annotated parameters may receive calls from code for which the checker has little type information. Coverage depends on the information available and the checks performed. The runtime still applies its own rules to the values it receives.
What a Type Check Establishes
Type checking can uncover unsupported operations, inconsistent assignments and arguments passed to the wrong kind of parameter. A type error identifies a mismatch. A runtime error identifies when a failure occurs. The attempted string call qualifies as both when JavaScript executes it.
Passing a type check answers a narrower question than passing a test of the program’s behavior. A quantity may have the expected integer type and still be outside the allowed range. Adding a range check can enforce that separate requirement. A program can also add two compatible numbers when the calculation calls for subtraction. The types permit the operation, while a test with a known result can reveal the mistake.
Advertisement — FinchTrade.
FinchTrade services
Get started