当前位置:网站首页>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 )
边栏推荐
- [JVM] - Division de la mémoire en JVM
- 【C语言练习——打印空心正方形及其变形】
- 2022 Western pastry (Advanced) test question simulation test platform operation
- Concurrent wait/notify description
- Carboxylic acid study: lumiprobe sulfoacyanine 7 dicarboxylic acid
- Biovendor sRAGE antibody solution
- 2022 special operation certificate examination question bank and simulation examination for safety management personnel of fireworks and firecrackers business units
- 解决ValueError: Iterable over raw text documents expected, string object received.
- 线条动画
- Disable right-click, keyboard open console events
猜你喜欢

【JVM】——JVM中内存划分

FB、WhatsApp群发消息在2022年到底有多热门?

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

Gorm transaction experience

阴阳师页面

Keil C51的Data Overlaying机制导致的函数重入问题

Voltage mode and current mode control of switching power supply

中小型水库大坝安全自动监测系统解决方案

What is the difference between AC and DC?

Interpretation of cloud native microservice technology trend
随机推荐
Unity out ref params
Yunda's cloud based business in Taiwan construction 𞓜 practical school
Animation de ligne
Wireless sensor network learning notes (I)
数据中台:一篇带你深入浅出了解数据中台
Why is point shield cloud forced to quit playing?
JS中的链表(含leetcode例题)<持续更新~>
Pcr/qpcr research: lumiprobe dsgreen is used for real-time PCR
Office is being updated and the application cannot start normally
二级造价工程师证书含金量到底有多高?看这些就知道了
博客登录框
The heading angle of sliceplane is the same as that of math Corresponding transformation relation of atan2 (y, x)
To batch add background pictures and color changing effects to videos
When using the MessageBox of class toplevel, a problem pops up in the window.
解决ValueError: Iterable over raw text documents expected, string object received.
Based on the order flow tool, what can we see?
Leetcode 88: merge two ordered arrays
Extjs library management system source code intelligent library management system source code
? How to write the position to output true
109. simple chat room 12: realize client-side one-to-one chat