Author : MD TAREQ HASSAN

Introduction

Interfaces are capable of describing the wide range of shapes that JavaScript objects can take. In addition to describing an object with properties, interfaces are also capable of describing function types.

Details: https://www.typescriptlang.org/docs/handbook/interfaces.html

Class with interface

interface ClockInterface {
    currentTime: Date;
}

class Clock implements ClockInterface {
    currentTime: Date = new Date();
    constructor(h: number, m: number) { }
}

Function type with interface

interface Foo {
    (x: string, y: string): boolean;
}

let foo: Foo = function(x: string, y: string) {

	// ... ... ...

    let isOk = ...;
	
	
    return isOk;
}

Function parameter type with interface

interface Foo {
    x: string;
	y?: string;
}

function bar(foo: Foo): number {

	// ... ... ...
	
	
    return 1;
}

Extending interface

interface Foo {
	x: number;
	xyz() {
	
	}
}

To extend Foo declare again (it would not override, it woul extend)

interface Foo {
	y: number;
	abc() {
	
	}
}

Now Foo has x, y, xyz() and abc()