当前位置:网站首页>Typescript interface
Typescript interface
2022-06-28 05:30:00 【Ahchoo ahchoo-】
TypeScript One of the core principles of the is to check the type of the value .
Use interfaces to define the types of objects , Interfaces are descriptions of object properties and methods
Interface : It's a type of 、 A specification 、 An ability 、 A constraint
Optional attribute
- Attributes in a definable interface do not have to be , grammar
attribute ?
interface IPerson {
firstName: string
lastName?: string // Non mandatory attributes
}
Read-only property
- The attributes in the interface can be defined as read-only , Non modifiable , grammar
readonly attribute
interface IPerson {
readonly id: number // Read-only property
firstName: string
lastName: string
}
Function type
- Function type : Define an interface , Type as function
- Use an interface to represent the type of a function , You need to define a call signature for the interface
- Call signature : It is like a function definition with only parameter list and return value type . Each parameter in the parameter list needs a name and type
// Define an interface , Type used as a function
interface ISearchFunc {
// Define a call signature
(source: string, subStr: string): boolean
}
// Define a function , The type is the above interface
const searchString: ISearchFunc = function (source: string, subStr: string): boolean{
return source.search(subStr) > -1
}
// Call function
console.log(searchString(' Kite in the sky ', ' wind ')); //true
Class types
- Class types : The class type is implemented through the interface , Used to explicitly force a class to conform to a contract
// Define an interface
interface IFly {
fly() // This method has no implementation
}
// Define a class , Its type is the interface defined above
class Person implements IFly {
// Implement the methods in the interface
fly() {
console.log(' Flying ');
}
}
// Instantiate objects
const person = new Person
person.fly()
The relationship between classes and interfaces is not called inheritance , It's called realizing
- A class can implement multiple interfaces
interface ISwim {
swim()
}
class Person2 implements IFly,ISwim {
fly() {
console.log(' fly 2');
}
swim() {
console.log(' swimming 2');
}
}
const person2 = new Person2
person2.fly()
person2.swim()
Class can implement an interface , Multiple interfaces can be implemented , Pay attention to the real implementation of the interface contents
- An interface can inherit multiple interfaces
interface IAll extends IFly, ISwim {
}
// Define a class , Directly implement the inherited interface
class Person3 implements IAll {
fly() {
console.log(' fly 3');
}
swim() {
console.log(' swimming 3');
}
}
const person3 = new Person3
person3.fly()
person3.swim()
summary : Interfaces and interfaces are called inheritance ( Use extends keyword ), Between classes and interfaces is called implementation ( Use implements keyword )
边栏推荐
- Reactive dye research: lumiprobe af594 NHS ester, 5-isomer
- Concurrent wait/notify description
- What is the difference between AC and DC?
- Intensive learning notes
- Zzuli:1072 frog climbing well
- 【Linux】——使用xshell在Linux上安装MySQL及实现Webapp的部署
- Docker installs mysql5.7 and starts binlog
- Gee learning notes 3- export table data
- Quartus replication IP core
- Why is point shield cloud forced to quit playing?
猜你喜欢

2022 new version NFT source code source code of China meta universe digital collection art trading platform

Biovendor sRAGE protein solution

Leetcode 88: merge two ordered arrays

gorm事务体验

Docker安装Mysql5.7并开启binlog

【Linux】——使用xshell在Linux上安装MySQL及实现Webapp的部署

Extjs library management system source code intelligent library management system source code

MySQL export database dictionary to excel file

JSP

How to do a good job of dam safety monitoring
随机推荐
OpenSSL client programming: SSL session failure caused by an obscure function
二级造价工程师考试还没完?还有资格审核规定!
mysql 导出查询结果成 excel 文件
To batch add background pictures and color changing effects to videos
Enum
mysql导出数据库字典成excel文件
How to develop the language pack in the one-to-one video chat source code
Oracle基础知识总结
How does guotaijun charge for safe varieties? Let's talk about the futures account opening process
Assembly common instructions
数据中台:数据治理的建设思路以及落地经验
数据中台:AI中台的实施与总结
2022 new version NFT source code source code of China meta universe digital collection art trading platform
Docker安装Mysql5.7并开启binlog
指定默认参数值 仍报错:error: the following arguments are required:
Create NFS based storageclass on kubernetes
[leetcode] 12. Integer to Roman numeral
sklearn 特征工程(总结)
sqlmap工具使用手册
WordPress zibll sub theme 6.4.1 happy version is free of authorization