Terminology (5)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Interface
- A contract for supported operations, and in TypeScript potentially data properties, that different types can satisfy.
- Trait
- Rust’s contract, implemented explicitly with impl Trait for Type.
- Structural typing
- Fitting a type by its shape, whatever it was declared as.
- Nominal typing
- Fitting a type only by an explicit declaration, such as an impl or a base class.
- Default method
- A method a contract provides, used unless a type supplies its own.
Separate the consumer from its implementations
An interface or trait names a set of operations that a consumer can use without knowing every concrete type. An implementation supplies those operations for a particular type. The signatures describe how to call them; their intended meaning still needs to be kept by the implementation.
TypeScript interfaces describe structure: a value can satisfy a contract by having compatible members. An implements clause checks a class declaration against that shape; it does not install methods or add runtime validation.
01Satisfying a contract
Structure, explicit implementation or a virtual base
A hotel is connecting several charge providers to one bill. The billing code must call the intended price or discount implementation, keep a bill usable when a coupon is absent, and reconcile room occupancy separately from booking revenue. Which contract does each consumer actually check?
The bill starts with two charge providers: a room stay and a tour. Follow the common price consumer and predict the two amounts including its fee.
interface Priced {
price(): number;
}
class Room {
nights = 3;
price(): number {
return this.nights * 40;
}
}
function bill(item: Priced): number {
return item.price() + 5;
}
console.log(bill(new Room()), bill({ price: () => 7 }));
npx tsc --noEmit --strict main.ts && node main.ts
Use the setup from the first reading for your language.
02Satisfied by accident
A method with the right name may not keep the promise
A provider adapter is wired to the same billing contract, but its price method reports a thermometer reading. Use this reduced probe to check what the type system verifies before trusting an adapter’s meaning.
interface Priced {
price(): number;
}
class Thermometer {
price(): number {
return 21;
}
}
const items: Priced[] = [new Thermometer()];
console.log(items[0]!.price());
03No implementation
An implementation that may be missing must be checked
The bill can accept a code that the hotel no longer recognizes. Before subtracting a discount, inspect the value returned for an absent coupon and predict how this caller handles it.
interface Coupon {
discount(): number;
}
const coupons = new Map<string, Coupon>([["SAVE10", { discount: () => 10 }]]);
const coupon = coupons.get("NONE");
console.log(coupon.discount());
04Repair the hotel bill
Discounts, coupons, then distinct rooms
The hotel billing utilities need three repairs. First apply the room discount. Then accept only coupons that exist. Finally count each booked room once.
Each file contains its imports, reader and complete caller.
Repair the hotel bill
Apply each room discount, preserve valid bills without coupons, then count numeric room identities while retaining revenue from every booking.
Sources
Sign in to keep your read marks on every device. Sign in
Was this helpful?