This is the fifth day of my participation in the August More text Challenge. For details, see: August More Text Challenge
Interfaces are constraints on relatively complex cases
This chapter introduces the basic specifications applied to objects and classes
Object interface
Object, function
Let’s look at (recall) the constraints on objects and functions:
let num3:object = {a:1}
function fn(arg: object) :void {}
Copy the code
The parameter arg in the above function is just a specification of the parameter type. Arg must be Object.
However, there is no constraint on the key-value pairs within an Object
interface
Here is an example of the basics of interfaces
interface FnArg {
a: number, c? : number// Optional attributes;
}
function reLoad(arg: FnArg) :void { }
reLoad({ a: 1.c: 2 });
reLoad({ a: 1.c: 3.b: 2 })
Copy the code
Resolution:
In this example, an interface named FnArg is declared. Attribute A is mandatory and attribute C is optional.
An error will be reported if the reLoad method is passed with no property a or any property other than a or b.
Read-only property
Add the modifier readonly to the property
interface Num {
readonly x: number;
}
let numObj: Num = { x: 10 };
numObj.x = 5;
//Cannot assign to 'x' because it is a read-only property.
Copy the code
Note: Property x is read-only and cannot be changed. Modifiers such as private and public cannot be added.
Bypass the constraint
In the example of the first interface, the constraints on parameters are relatively strict: there can be neither more nor less attributes.
Now rewrite the first interface example:
interface FnArg {
a: number, c? : number// Optional attributes;
}
function reLoad(arg: FnArg) :void {}let obj = { a: 1.c: 3.b: 2 }
reLoad(obj)
Copy the code
If we declare the obj argument ahead of time, we wait until we call the method to pass in obj;
At this point, if the parameter is more than a few, is not an error. The constraints become looser.
The following example uses assertions to achieve the same effect.
interface FnArg {
a: number, c? : number// Optional attributes;
}
function reLoad(arg: FnArg) :void { }
reLoad({ a: 1.c: 3.b: 2 } as FnArg);
Copy the code
A third way: using index types can achieve the same effect. (Mentioned below)
The function interface
function
Let’s look at a function that takes an argument of type number and returns type number
function fn(num1: number) :number{
return num1
}
Copy the code
If there are many functions of the same type, it can be cumbersome to write the restricted type every time.
interface
This is simplified by means of interfaces:
interface FnFunc {
(num1: number): number
}
let f2: FnFunc;
f2 = function (num: number) {
return num
}
Copy the code
The FnFunc interface specifies the input and return types of functions.
Inference type
In combination with the types of inference mentioned in the previous chapter, the final result is as follows:
interface FnFunc {
(num1: number): number
}
let f3: fnFunc;
f3 = function (num) {
return num
}
Copy the code
The index type
An index type is also a restricted category. It is different from string and number types:
interface Index {
[key: number]: string;
}
// Index key key, index value number, index query result type string;
let arr: Index = [1.3.'3'] / digital1,3Do not conform to the; An error;Copy the code
Description in the Index interface: The key value in the Index is a number, and the return value is a string.
It can be either an array of string types (number index, query result is string)
It can also be the string ‘LKDJFKLSDJFKL’ or any other type that fits the description;
The class interface
The keyword implements
1. An interface applied to a class; Combination of properties and functions
interface Example {
name: string;
run():void;
}
class Father implements Example {
name: number;
run(){}}Copy the code
The class interface describes the parameters and methods that must be used in the class. Use an interface keyword: implements cannot be omitted
Interface Inheritance interface
Interfaces can be inherited just like classes
interface Father {
name: string
}
interface Mom {
sex: string
}
interface Bro extends Father, Mom {
weight: string;
}
Copy the code
As above, inheritance in an interface can have more than one parent class at a time; A specification that has multiple parent classes in a subclass;
Interface inheritance class
Interfaces can inherit from classes
class Father {
state: string;
}
interface FaInt extends Father {
select(): void;
}
class Son extends Father implements FaInt {
select(){}}class Nobody implements FaInt {
state: string;
select(){}}Copy the code
In the example above, the interface from Father inherits the constraint of the state attribute and the modifier public for state
The Son class inherits from the Father class, so it satisfies both the state attribute and the select method
The Nobody class does not inherit from its parent class. In this case, the Nobody class must satisfy both the state property and the select method
That concludes the basics of classes;