Typescript–interface Concept: Typescript interface defines the type of object (my understanding: Defines the types of object attributes and the public methods in the extraction Class) categories:
Optional attributes of an interface: the attributes in an interface are not all necessary, and interfaces with optional attributes are similar to ordinary interfaces, but are added after optional attributes. ’
Optional properties of the interface
interface Squareconfig {
color?: string;
Width? : number;
}
function Createsquare (config:squareconfig): {"Color": string, ' area ': number} {let
newsquare = {color: ' White ', area : M};
if (config.color) {
newsquare.color = Config.color;
}
if (config.width) {
Newsquare.area = config.width * config.width;
}
return newsquare;
}
Let MySQUARE = Createsquare ({color: "BLACK"});
Console.log (MySQUARE);
Read-only properties: Some properties in an interface can only modify their values when the object is just created, and adding ' ReadOnly ' typescript before the read-only property has the Readonlyarray type, which is similar to array, except that all the mutable methods are removed. Therefore, you can make sure that the array is created and can no longer be modified:
The interface for read-only properties
interface Point {
readonly x:number;
ReadOnly y:number;
}
Export class Appcomponent implements OnInit {
//read-only Property interface
Readonlyfun (): void {
Console.log ("This is the interface for read-only Properties");
Let P1:point = {x:10, y:10};
Console.log (p1);
Error
//p1.x = 5;
Readonlyarray<number> let
a:number[] = [1,2,3,4];
Let ro:readonlyarray<number> = A;
Console.log (RO);
Ro[0] =
Const: For the target identifier is a variable
ReadOnly: For the target identifier is an object property function type interface: Defines an invocation signature for an interface. It's like a function definition that has only a parameter list and a return value type. Each parameter in the parameter list requires the name and the parameter name of the type function to not match the name defined in the interface
function Type
interface Searchfunc {
(source:string, substring:string): boolean;
}
Export class Appcomponent implements OnInit {
//function type sou:string, sub:string
mysearch:searchfunc = function (so Urce:string, substring:string) {let result
= Source.search (subString);
return result >-1;
}
Ngoninit () {
console.log (this.mysearch (' nihao1234566 ', ' Nihao ')
}
}
An interface to an index type: an indexed type has an index signature that describes the type of object index and the corresponding index return value type.
Index type
interface Stringarry {
[index:number]: string;
}
Export class Appcomponent implements OnInit {
//index type
stringarray (): void {let
myarry:stringarry;
Myarry = ["Bod", "Fred"];
Let mystr:string = myarry[0];
Console.log (MYSTR);
}
Ngoninit () {
this.stringarray ();
}
}
Interface inheritance: Interfaces can inherit mixed types from one another: interface properties can be multiple types of interface inheritance classes: Interfaces in inheriting classes (both private and public), implementing an interface that can only be implemented by its subclasses