1. Functions of TS interface:
What are typescript interfaces for?
Let’s start with an example:
Define a function in JavaScript that can be used to get the user’s name and age strings:
const getUserInfo = function(user) { return `name: ${user.name}, age: ${user.age}`}
Copy the code
The correct way to call is as follows:
getUserInfo({name: "coderwhy", age: 18})
Copy the code
However, when the project is large or multi-person, the following error may occur:
// Uncaught TypeError: Cannot read property 'name' of undefinedconsole.log(getUserInfo({name: }) // name: coderwhy, age: undefinedgetUserInfo({name: "codeWhy ", height: 1.88}) // name: coderwhy, age: undefinedCopy the code
And we know that JavaScript is a weakly typed language, which does not detect incoming code, which is not only non-standard and not easy to maintain and expand, but also has various similar security risks.
Typescript avoids these problems by using interfaces:
If the appeal function method uses an interface, it can be reconstructed as follows:
Define an IUser interface:
Interface IUser {name: string; age: number; }Copy the code
Specific functions:
const getUserInfo = (user: IUser): string => { return `name: ${user.name}, age: ${user.age}`; }; GetUserInfo ({name: "coderwhy", age: 18});Copy the code
It is common to define an interface and then continue to write the implementation methods of that interface in the class that inherits the interface. Why not just write the implementation methods in the class and never define the interface again? When we have this doubt, we need to understand the meaning of interfaces.
The specific meaning of interfaces lies in:
(1) The class of an interface describes an entity, including the state of the entity and the actions that the entity may emit;
(2) The interface defines the actions that an entity may issue, but it is important to note that it only defines the prototype of these actions, and there is no implementation and no state information.
(3) Interface is like a specification/protocol, which specifies the properties and methods to be provided in the class. It is an abstract concept; A class is a concrete entity that implements the protocol and satisfies the specification. It is a concrete concept.
(4) From the point of view of the program: interface for function declaration, class for function implementation, it should be noted that the same declaration may have many kinds of implementation.
2. The significance of using interfaces in development:
1. Simple and normative:
In code design, an interface is a specification, and if a project is large, the use of the interface not only informs developers about the relevant business requirements, but also restricts naming conventions.
2. Maintenance and expansion
Interface will be related specifications and specific attributes and implementation method separated, when the implementation method changes, even if the interface is used in many places, these places do not need to change, just modify the specific implementation method, so as to achieve the convenience of system maintenance and expansion.
3, security, tightness: interface is an important means to achieve decoupling, it describes all the external services of the system without involving specific implementation details, so it will be more secure and strict.