当前位置:网站首页>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 )
边栏推荐
- Realizing color detection with OpenCV
- Binder面试之:内存管理单元
- CpG solid support research: lumiprobe general CpG type II
- 【C语言练习——打印空心正方形及其变形】
- Install kubebuilder
- 8VC Venture Cup 2017 - Elimination Round D. PolandBall and Polygon
- 数据中台:AI中台的实施与总结
- How high is the gold content of grade II cost engineer certificate? Just look at this
- Based on the order flow tool, what can we see?
- [JVM] - Division de la mémoire en JVM
猜你喜欢

Oracle 常用基础函数

How high is the gold content of grade II cost engineer certificate? Just look at this

線條動畫

解决ValueError: Iterable over raw text documents expected, string object received.

Online yaml to JSON tool

Jdbc的使用

Yin Yang master page

Leetcode 88: merge two ordered arrays

Shutter nestedscrollview sliding folding head pull-down refresh effect

分享|智慧环保-生态文明信息化解决方案(附PDF)
随机推荐
如何在您的Shopify商店中添加实时聊天功能?
Zzuli:1071 decomposing prime factor
[skywalking] learn distributed link tracking skywalking at one go
Zzuli:1072 frog climbing well
刘海屏手机在部分页面通过[[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom得到底部安全区高度为0问题
Jdbc的使用
Reactive dye research: lumiprobe af594 NHS ester, 5-isomer
小球弹弹乐
It is the latest weapon to cross the blockade. It is one of the fastest ladders.
Determine whether an attribute exists in an object
線條動畫
Steve Jobs' speech at Stanford University -- follow your heart
Informatics Orsay all in one 1360: strange lift
MySQL 45讲 | 05 深入浅出索引(下)
Opencv实现目标检测
Liuhaiping's mobile phone passes [[uiapplication sharedapplication] delegate] window. safeAreaInsets. The height of the bottom security zone is 0
指定默认参数值 仍报错:error: the following arguments are required:
How long will the PMP test results come out? You must know this!
Biovendor sRAGE protein solution
mysql 导出查询结果成 excel 文件