There is no difference on the interface . This uses an interface that uses both entries (let me use string as a return type for clarity):
interface MyInterface { foo(): string;
Here foo is a function with a return type of string . And type bar also a function with return type string . The first notation is usually cleaner. The following object corresponds to the interface:
let x: MyInterface = { foo: () => "", bar: () => "" }
In a class, one notation adds a function to the prototype, and the other adds it as a property of a new instance ( this.myFunc = ... ). This has the same effect as in JavaScript (and you almost never need to add a function / method as an instance property).
class MyClass { bar(): string { return ""; }
As we see below, the class corresponds to our original interface, although I changed the value of foo and bar . Therefore, the interface does not impose implementation details of how the method will be installed.
let y: MyInterface = new MyClass();
Note that a class is not a special type - it effectively represents an interface and an implementation. Thus, you can use MyClass , like any other type (be it an interface or a class):
let z: MyClass = { foo: () => "", bar: () => "" }
As for types (this is what defines the interface), there is no difference, although when used in a class, different implementations produce notations.
source share