Types and contracts
What are interfaces?
On this page
An interface specifies operations a consumer expects. A narrow Clock interface with now() lets a test choose the instant at which a lease expires. Specify whether now() represents wall-clock time or monotonically elapsed time. The type signature alone cannot answer that question.
Worked example
TypeScriptinterface Clock { elapsed(): number }
function leaseExpired(clock: Clock, deadline: number): boolean {
return clock.elapsed() >= deadline;
}
const manual = { elapsed: () => 10 };
if (!leaseExpired(manual, 10)) throw Error("Deadline is inclusive");Explore class relationships in the OOP Playground