当前位置:网站首页>TS interface
TS interface
2022-07-25 23:21:00 【Intern Sen】
TS Interface
An interface is a declaration of a series of abstract methods , Is a collection of method features .
These methods should be abstract , It needs to be implemented by specific classes , Let specific classes execute specific methods .
1. Simple interface instance
// Define an interface User
interface User {
name: string,
age: number,
sing: ()=>string
}
// Define a variable xiaoXu
let xiaoXu: User = {
name: " Xiao Xu ",
age: 18,
sing: ():string => " Xu Qianjin "
}
console.log(xiaoXu.name); // Xiao Xu
console.log(xiaoXu.age); //18
console.log(xiaoXu.sing()); // Xu Qianjin
2. Union types and interfaces
interface RunOptions {
program:string;
commandline:string[]|string|(()=>string);
}
// commandline Is string
var options:RunOptions = {
program:"test1",
commandline:"Hello"
};
console.log(options.commandline)
// commandline It's an array of strings
options = {
program:"test1",
commandline:["Hello","World"]
};
console.log(options.commandline[0]);
console.log(options.commandline[1]);
// commandline It's a function expression
options = {
program:"test1",
commandline:()=>{
return "**Hello World**";}
};
var fn:any = options.commandline;
console.log(fn());
3. Interfaces and arrays
The index value and element of the array can be set to different types in the interface , The index value can be a number or a string .
Set element to string type :
interface namelist {
[index:number]:string
}
var list1:namelist = ["Google","Runoob","Taobao"]; // The same type , correct
var list2:namelist = ["Runoob",1,"Taobao"]; // Error element 1 No string type
4. Interface inheritance
Interface inheritance means that an interface can extend itself through other interfaces .
TS Allow interfaces to inherit multiple interfaces , Use keywords extends.
4.1 Single interface inheritance
interface Person {
age:number
}
interface Musician extends Person {
instrument:string
}
var drummer = <Musician>{
};
drummer.age = 27
drummer.instrument = "Drums"
console.log(" Age : "+drummer.age)
console.log(" A favorite musical instrument : "+drummer.instrument)
4.2 Multi interface inheritance
interface IParent1 {
v1:number
}
interface IParent2 {
v2:number
}
interface Child extends IParent1, IParent2 {
}
var Iobj:Child = {
v1:12, v2:23}
console.log("value 1: "+Iobj.v1+" value 2: "+Iobj.v2)
边栏推荐
- @Autowired注解 required属性
- POI special effects Market Research
- 学习探索-3d轮播卡片
- [interface performance optimization] reasons for index failure and how to optimize SQL
- Drive board network cable directly connected to computer shared network configuration
- 四旋翼飞行器的飞控实现「建议收藏」
- 推荐系统——An Embedding Learning Framework for Numerical Features in CTR Prediction
- About using NPM command under the terminal, the installation error problem is solved (my own experience)
- Explain in detail the addition (+) operation in JS, basic data type addition, reference data type addition, and the underlying operation rules, [] + {}, {} + []
- JS regular expression matches IP address (IP address regular expression verification)
猜你喜欢

Dynamic memory management

电商RPA,大促轻松上阵的法宝

Secure code warrior learning record (IV)

Multimodal deep multi modal sets

Design of Butterworth filter and drawing of amplitude frequency characteristic curve

The fifth article in the series of radar Fundamentals: the function of radar modulation style

BI 系统中为什么会有很多快照表?

5 ROS仿真建模(3- rviz+gazebo+控制仿真机器人)

File contains vulnerability

行云管家V6.5.1/2/3系列版本发布:数据库OpenAPI能力持续强化
随机推荐
MathType installation and solution cannot solve the problem of crtl+v
Analysis of direction finding error of multi baseline interferometer system
Enabling partners, how can Amazon cloud technology "get on the horse and get a ride"?
推荐系统——An Embedding Learning Framework for Numerical Features in CTR Prediction
加拿大EE通道
Wamp MySQL empty password
数组中重复的数字
ASP date function (what if the disk function is incorrect)
类和对象(3)
Network Security Learning notes-1 file upload
wordpress去掉网站发布时间
Solve the problem phpstudy failed to import the database
File contains vulnerability
Node基础
Ffmpeg first learning (only for coding)
PHP binary array is sorted by a field in it
物理防火墙是什么?有什么作用?
Serialize common default values and column parameters
About priority queues
关于优先队列