当前位置:网站首页>Typescript foundation interface interface
Typescript foundation interface interface
2022-07-20 14:19:00 【51CTO】
TypeScript One of the core principles of is to type check the structure of a value .
stay TypeScript in , The purpose of an interface is to name these types and define contracts for your code or third-party code .
Interface (interface
) Is the most commonly used type annotation method .
user
Interface to define the types of object attributes that may be used in the future .
Be careful : The type checker does not check the order of properties , As long as the corresponding attribute exists and the type is right
Optional attribute (?
)
The properties in the interface are not all required
Interfaces with optional properties are similar to normal interface definitions , Just add a... After the optional attribute name definition ?
Symbol .
benefits
- Possible attributes can be predefined
- You can catch errors when referring to non-existent properties
Read-only property (readonly
)
Some object properties can only change their values when the object is just created . You can use... Before the attribute name readonly
To specify read-only properties :
TypeScript have ReadonlyArray
type , It is associated with Array
be similar , Just take out all the variable methods , So you can make sure that the array can no longer be modified after it's created .
Even if the whole ReadonlyArray
Assignment to an ordinary array is also not allowed . But you can override it with type assertions :
Function type
To represent function types using interfaces , We need to define a call signature for the interface .
It's like a function definition with only parameter lists and return value types . Each parameter in the parameter list needs a name and type .
Type merge
VSCode Although the editor will prompt you 'User'
Has been defined . however interface
The interface will still merge the same interface name , And compile the output normally .
Any number of identifications
Sometimes the number of types is dynamic , But the type is specified to be constrained , It can be written like this
Interface inheritance (extends
)
To use by inheritance extends
keyword
Single interface inheritance
Multi interface inheritance : Use a comma ,
Separate the interfaces
边栏推荐
猜你喜欢
LiveDataBus核心原来如此简单
Use of ADB debugging tools
9、gorm进阶
朱松纯团队最新突破登Science头条!让AI「读懂」人类价值观
Tutor marriage matchmaking, this doctoral couple: a total of 15 papers for a work, leaving school to teach!
iPhone 14 Max生产进度落后:或影响首批产品出货配比
C#/VB.NET 添加多行文本水印到Word文档
Cooperatively Coevolving Particle Swarms forLarge Scale Optimization
Too busy or too lazy? The same fund manager has the same view in the quarterly reports of different products
系统App 签名JKS制作及静默安装
随机推荐
熔断、降级 Sentinel
密码太多不知道怎么记录?不如自己写个密码箱小程序
杂记--大杂烩
推荐一个 curl 命令转化为PHP代码的网站&&在线发起执行 curl 请求的网站
How to use SAP intelligent robotic process automation to automate Excel
【FAQ】接入HMS Core推送服务,服务端下发消息常见错误码原因分析及解决方法
Use of ADB debugging tools
8. Introduction to ORM and introduction to Gorm
Skillfully using roaringbitmap to deal with the memory diff problem of massive data
TypeScript 基础 — interface 接口
【MATLAB】基于油猴脚本和MATLAB下载原创力文档
Alibaba hot updates the story of sophix
Android面试Hash原理详解二
『浅入浅出』MySQL 和 InnoDB [建议收藏并分享]
【职场规则】IT职场规则|工作能力差的表现
如何使用 SAP Intelligent Robotic Process Automation 自动操作 Excel
股票开户首选,炒股交易开户佣金最低手机上开户安不安全
Google Guava 只是弟弟,什么才是真正的缓存之王?(荣耀典藏版)
Vulnhub | DC: 5 |【实战】
bootloader的原理分析