Python Extension Types: Zero-Cost Inline Wrappers in Dart 3.3
Introduced in Dart 3.3, **Extension Types** (`extension type Name(UnderlyingType)`) provide compile-time type safety and custom APIs around an underlying representation type without allocating an extra wrapper object on the heap. At runtime, the extension type completely disappears and operates directly as the raw underlying type (zero memory overhead).
"An extension type is like putting on specialized sunglasses: looking through them gives you specialized tinted vision (custom API methods), but your actual eyes (the underlying data) remain unchanged."
Deep Dive: How It Works
Zero-Cost Abstraction: Prevents heap object allocations, garbage collector overhead, and pointer indirection.
Representation Declaration: `extension type Id(int value)` declares `int` as the backing storage.
Compile-Time Boundary: The compiler treats `Id` and `int` as distinct types, preventing passing a raw `int` where an `Id` is expected.
Syntax Blueprint
// Extension Type Definition
extension type UserId(int id) {
bool get isValid => id > 0;
void printBadge() => print("User #$id");
}Zero-cost type wrapper wrapping int with custom methods.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Expecting `extension type` to perform runtime polymorphism checks with `is`.Why it happens: At runtime, extension types erase to their underlying representation type.
How to fix: Use extension types for compile-time safety and JS interop, not runtime subtype polymorphism.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Create Meters Extension Type
Create `extension type Meters(double value) { double get toKm => value / 1000; }`.
In `main()`, instantiate `final dist = Meters(5000.0);` and print `KM: ${dist.toKm}`.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.