当前位置:网站首页>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 )
边栏推荐
- Yunda's cloud based business in Taiwan construction 𞓜 practical school
- OpenSSL client programming: SSL session failure caused by an obscure function
- Unity out ref params
- Yin Yang master page
- 如何在您的Shopify商店中添加实时聊天功能?
- 拉萨手风琴
- 刘海屏手机在部分页面通过[[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom得到底部安全区高度为0问题
- gorm事务体验
- Dart学习——函数、类
- Lhasa accordion
猜你喜欢

Solution of dam safety automatic monitoring system for medium and small reservoirs

Sqlmap tool user manual

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

如何在您的Shopify商店中添加实时聊天功能?

Share a powerful tool for factor Mining: genetic programming

WordPress zibll sub theme 6.4.1 happy version is free of authorization

一看就会 MotionLayout使用的几种方式

Concurrent wait/notify description

線條動畫

Yunda's cloud based business in Taiwan construction 𞓜 practical school
随机推荐
小球弹弹乐
MySQL 45讲 | 05 深入浅出索引(下)
JS 文本框失去焦点修改全半角文字和符号
Object detection with OpenCV
2022 special operation certificate examination question bank and simulation examination for safety management personnel of fireworks and firecrackers business units
Linked list in JS (including leetcode examples) < continuous update ~>
Quartus replication IP core
Lhasa accordion
Install kubebuilder
CpG solid support research: lumiprobe general CpG type II
Leetcode 88: merge two ordered arrays
Line animation
? How to write the position to output true
Leecode question brushing-ii
线条动画
jsp连接Oracle实现登录注册
109. simple chat room 12: realize client-side one-to-one chat
Detailed usage configuration of the shutter textbutton, overview of the shutter buttonstyle style and Practice
Why don't big manufacturers use undefined
Docker installs mysql5.7 and starts binlog