Static Typing
- In Turkish
- Statik Tipleme
- Pronunciation
- STAT-ik TY-ping
In short
Static typing means the types of variables and expressions are checked before the program runs, usually by the compiler, so many type errors are caught early.
What is static typing?
In a statically typed language such as Java, C#, Go, Rust, Kotlin or TypeScript, the compiler checks that values are used consistently with their types. Passing a string where a number is expected, calling a method that doesn't exist or forgetting to handle a possible null value can be reported as errors before the code ever runs.
Types don't always have to be written out. Type inference lets the compiler work them out, so let count = 0 is known to be a number without a label. Explicit types are still common on function signatures, where they double as documentation: a reader can see what goes in and what comes out without reading the body.
The benefits grow with the size of the codebase. Editors can offer accurate autocompletion and safe automatic refactoring, such as renaming a method everywhere it is used, and a change that breaks a caller far away shows up immediately. The cost is more upfront ceremony and occasionally fighting the type checker when the types are hard to express.
A common misconception is that static typing means strong typing. They are separate ideas: static versus dynamic is about when types are checked, while strong versus weak is about how freely values are converted between types. C is statically but fairly weakly typed, and Python is dynamically but strongly typed.
Key takeaways
- Types are checked before the program runs, usually at compile time.
- Java, C#, Go, Rust, Kotlin and TypeScript are statically typed.
- Type inference means many types don't have to be written by hand.
- It enables reliable autocompletion, refactoring and early error detection.
- Static versus dynamic is about when types are checked, not how strict they are.
Example
function totalPrice(price: number, quantity: number): number {
return price * quantity;
}
totalPrice(9.99, 3); // fine
totalPrice("9.99", 3);
// Error: Argument of type 'string' is not assignable to parameter of type 'number'.
// Reported by the compiler and the editor; the code never runs with the bug.
let count = 0; // inferred as number, no annotation needed
count = "zero"; // Error: Type 'string' is not assignable to type 'number'.Readers ask
Is TypeScript statically typed?
Yes. TypeScript adds static types to JavaScript and checks them at compile time. The types are removed when it is compiled to JavaScript, so they don't exist at runtime.
Does static typing prevent all bugs?
No. It catches a class of mistakes, such as wrong types and missing fields, but not wrong logic. Tests are still needed to check that the code does the right thing.
What is gradual typing?
Adding optional types to a dynamically typed language, so a codebase can be typed bit by bit. TypeScript for JavaScript and type hints checked by tools like mypy for Python are examples.
Often compared
See also
- Dynamic TypingProgramming Fundamentals, p. 16Dynamic typing means types belong to values, not variables, and are checked while the program runs, so a variable can hold a number now and a string later.
- Data TypeProgramming Fundamentals, p. 14A data type is a classification that tells a program what kind of value a piece of data holds, such as a number or text, and which operations work on it.
- Type InferenceProgramming Fundamentals, p. 55Type inference is a compiler feature that works out the type of a variable or expression automatically, so you don't have to write every type annotation.
- TypeScriptWeb Development, p. 50TypeScript is a programming language built on JavaScript that adds static types, catching many bugs before the code runs, and compiles to plain JavaScript.
- CompilerProgramming Fundamentals, p. 11A compiler is a program that translates source code written in a programming language into a lower-level form, such as machine code, that a computer can run.
- GenericsProgramming Fundamentals, p. 24Generics are a language feature that lets you write functions, classes, and types that work with many data types while still keeping full type safety.
Spotted a mistake or something missing on this page?Suggest an edit