in other languages, interfaces are used to be shape classes, where a class implements and interface.
interface is more clearer that it does type an object (since object is an instance of Object).
interfaces can be implemented by classes.
a class can implement multiple interfaces, and it can have more logic than the interface it implements.
both type and interface can use readonly modifiers.
both can be used to type functions.
both can have optional properties.
Type
Interface
describe object
describe class or object
more flexible in typing
-
-
describe classes, where classes can implement interfaces
-
clearer in typing objects (object is an instance of Object or other class)
-
can be used to share implementation between classes since a class can implement multiple interfaces
you can use readonly modifier
you can use readonly modifier
-
interfaces can extend other interfaces
used to type functions
used to type functions
types can have optional properties
interfaces can have optional properties
example, implement:
interfaceGreetable{name:string;greet(phrase:string):void;}classPersonimplementsGreetable{name:string;constructor(name:string){this.name=name;}greet(phrase:string){console.log(`${phrase}${this.name}`);}}constgreet1:Greetable={// Greetable is an interface, so we can type it as an objectname:"Max",greet(phrase:string){console.log(`${phrase}${this.name}`);},};constgreet2:Greetable=newPerson("Max");// Person implements Greetable
example, readonly modifier:
interfacePersonI{readonlyname:string;}typePersonT={readonlyname:string;};classPersonimplementsPersonI{name:string;// Person implements PersonI, so name is readonlyconstructor(name:string){this.name=name;}}constperson1:PersonI={name:"Max"};constperson2:PersonT={name:"Max"};constperson3=newPerson("Max");person1.name="Anna";// error, readonly, person1 implements PersonI, so it can not be reassignedperson2.name="Anna";// error, readonly, person2 implements PersonT, so it can not be reassignedperson3.name="Anna";// error, readonly, person3 is an instance of Person class that implements PersonI, so it can not be reassigned
example, extending interfaces:
interfaceNamed{name:string;}interfaceAged{age:number;}interfaceGreetableextendsNamed,Aged{// extends multiple interfaces// now Greetable can have name property and age propertygreet(phrase:string):void;}
example typing a function:
interfaceDoubleValueFunc{// interface that describe function, syntax: function(arg: type): return type(number1:number,number2:number):number;}letmyDoubleFunction:DoubleValueFunc;myDoubleFunction=function(value1:number,value2:number){return(value1+value2)*2;};typeDoubleValueFuncType=(number1:number,number2:number)=>number;// type of functionletmyDoubleFunctionType:DoubleValueFuncType;myDoubleFunctionType=function(value1:number,value2:number){return(value1+value2)*2;};
example, optional properties:
interfaceSquareConfig{color?:string;height:number;}typeSquareConstructorParams={color?:string;height:number;};classSquareimplementsSquareConfig{color?:string;// making color optionalheight:number;constructor(config:SquareConstructorParams){this.color=config.color;this.height=config.height;}}// orclassSquareimplementsSquareConfig{color?:string;// making color optionalheight:number;constructor(height:number,color?:string){// color is optional in the constructor function definitionif(config){this.color=config.color;this.height=config.height;}}}