What practical differences are there between defining an interface method:
interface Foo {
bar(): void;
}
and defining a property with
The most critical difference is actually that using the property approach typescript actually checks contravariently for types. For Eg:
type FProp = {
fork: (a: A) => void
}
type FMeth = {
fork(a: A): void
}
type Cat = {
isPurring: boolean
}
type Dog = {
isBarking: boolean
}
const dd = { fork: (a: Cat & Dog) => void 0 }
const fa: FProp = dd // will throw up
const fb: FMeth = dd // will not issue any error
This is documented —
The stricter checking applies to all function types, except those originating in method or constructor declarations. Methods are excluded specifically to ensure generic classes and interfaces (such as Array) continue to mostly relate covariantly.
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-6.html