interface UserObj{
[index:string]:string;
}
var arr:UserObj = {name:'shawn'};
Class Type Interface
To constrain Class, similar to Abstract Class.
interface Animal{
name:string;
eat(str:string):void;
}
class Dog implements Animal{
name:string;
constructor(name:string){
this.name = name;
}
eat(){ console.log('Eat Meat'); // ===> Without implementing this method, would be wrong
}
var d = new Dog('Huang');
d.eat(); // Huang Eat Meat
Without implementing this eat( ) method, would be wrong, but passing parameters is only optional.
Interface Inherits Interface
extends
interface Animal{
eat():void;
}
interface Person extends Animal{
work():void;
}
class Web implements Person{
public name:string;
constructor(name:string){
this.name = name;
}
eat(){console.log('Eat Meat')};
work(){console.log('Write Code')};
}
var w = new Web('shawn');
w.work(); // shanw Write Code
-extending another Class at the same time
interface Animal{
eat():void;
}
interface Person extends Animal{
work():void;
}
class Programmer {
public name:string;
constructor(name:string){
this.name = name;
}
coding(code:string){ console.log(this.name+code};
}
class Web extends Programmer implements Person{
constructor(name:string){
super(name);
}
eat(){console.log('Eat Meat')};
work(){console.log('Write Code')};
}
var w = new Web('shawn');
w.coding('writes TS'); // shanw writes TS;
Similar to the
Abstract Class
in ES5, while it has more.Interface
can definetype
,function
,class
, etc.Property Interface
to constrain behavor and batch of methods, and also JSON
Optional Properties
An example in
ajax
Function Interface
To constrain the
passed parameters
, andreturned values
, andpatches
;encrypt function interface
Indexable Interface
To constrain
Array, Objects
, not common,Class Type Interface
To constrain
Class
, similar toAbstract Class
.Without implementing this
eat( )
method, would be wrong, butpassing parameters
is only optional.Interface Inherits Interface
extends
-extending another Class at the same time