implements implements & Classes as Types

Last updated: 2026-10-22

implements implements & Classes as Types

implements makes a class claim an interface: the compiler verifies the class provides every required member with matching types. The interface says "what it can do", the class answers "how" — one interface, many different implementations.

GroupFunctions & Classes
KindSyntax
Strict modeNot required

ℹ️ Info The editor transpiles TypeScript to JavaScript and runs it (no type checking); see each example's Output for type-check results.

📝 Syntax

interface Storage {
  save(key: string, value: string): void;
  load(key: string): string | null;
}

class MemoryStorage implements Storage {
  private map = new Map<string, string>();
  save(key: string, value: string): void { this.map.set(key, value); }
  load(key: string): string | null { return this.map.get(key) ?? null; }
}

⚙️ Parameters

implementsClaims interfaces: several at once allowed (implements A, B); the check is "class satisfies interface"
extends + implementsBoth may be combined: class Foo extends Base implements Storage — inherit implementation while honoring the contract

Returns:Method return types must match the interface or be narrower (covariant); parameters must be wider (contravariant) — otherwise a compile error.

▶ Example

Edit the code below and click Try it to see the transpiled result:

Output:

dark null

(Runtime output after transpilation; deleting load or widening its return type to string is a compile error — implements checks member by member)

💡 Notes

ℹ️ Info implements is a compile-time check with no runtime effect; after transpilation the interface is gone and the class is a plain class.
⚠️ Note Interfaces describe public members only — private fields are not part of the check; conversely every interface member must exist on the class, no exceptions.
💡 Tip This is dependency inversion in practice: business code depends on the Storage interface, tests inject MemoryStorage, production injects LocalStorage — caller code never changes.
More cases coming soon.

📚 Related tutorials

❓ FAQ

Q implements vs extends?
A extends inherits implementation (takes code); implements promises a shape (takes a contract). A class extends one base but may implement many interfaces.
Q Can a class be assigned to an interface type without implements?
A Yes — TS is structural, shape alone suffices. implements adds explicit intent and surfaces "interface changed, class didn't" at the class definition instead of at every assignment site.
Q Can an interface constrain a constructor?
A Yes, via a new signature: interface Ctor<T> { new (...args: any[]): T } — common for factory and container registration types.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏