kraftwer1 kraftwer1 - 1 year ago 89
TypeScript Question

Prohibit adding more public methods than in interface

interface IHuman {
talk(): void;
walk(): void;

class Human implements IHuman {
talk() {

walk() {

// This should not be possible:
fly() {

Is there a way to tell the compiler that only public methods defined in the interface are allowed to be added?

Or in other words, disabling duck typing in certain situations?

Answer Source

You're looking for exact types, which TypeScript doesn't currently have (as of TS 2.4)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download