当前位置:网站首页>ts学习笔记-interface
ts学习笔记-interface
2022-07-27 15:55:00 【V_AYA_V】
接口
- 可选属性
interface SquareConfig {
color?: string;
width?: number;
}
function createSquare(config: SquareConfig): {
color: string, area: number } {
// ...
}
- 额外的属性检查:跳过 interface 属性检测的方式
- 类型断言
- 添加字符串索引签名
- 将对象赋值给另外一个变量
// 类型断言
let mySquare = createSquare({
height:123, color:"red"} as SquareConfig)
// interface添加索引签名 ---->推荐
interface SquareConfig {
color?: string;
width?: number;
[propName: string] : any;
}
// 将对象赋值给另外一个对象
let mySquareData = {
height:123, color:"red"}
// 等同于 let mySquareData:any = {...}
let mySquare = createSquare(mySquare)
- 函数类型
接口也可以定义函数类型->(参数列表):返回值类型,函数的参数名无需与接口里定义的名字相匹配,函数参数会自动推断类型
interface SerchFun {
(name: string, title: string): boolean;
}
let mySearch: SerchFun = function (name: string, title: string): boolean {
let result = source.search(subString);
return result > -1;
};
const mySearchVal = mySearch('123', '123');
可引用数据类型
引可索引类型具有一个索引签名,它描述了对象索引的类型,还有相应的索引返回值类型。引用数据类型的索引支持两种类型(string||number),注意使用 number 的时候数字索引的返回值必须是字符串索引返回值类型的子类型。 这是因为当使用 number 来索引时,JavaScript 会将它转换成 string 然后再去索引对象。 也就是说用 100(一个 number)去索引等同于使用"100"(一个 string)去索引,因此两者需要保持一致。
混合类型
一个对象可以同时做为函数和对象使用,并带有额外的属性。
interface Counter{
(start: number): string;
interval: number;
reset(): void;
}
// <Counter>为类型断言
function getCounter(): Counter{
let counter = <Counter>function(start: number):string{
};
counter.interval = 123;
counter.reset = function(){
};
return counter;
}
let c = getCounter();
c(10)
c.reset()
c.interval = 5.0
边栏推荐
- Evaluation index of machine learning (II) -- classification evaluation index
- Taishan Office Technology Lecture: word strange paragraph borders
- Mysql database defines cursor in trigger
- MLX90640 红外热成像仪测温传感器模块开发笔记(七)
- What are the safety risks of small games?
- In the first week of June, risk control of e-shield business paid attention to 15 institutions such as New Oriental XRS, which were fined
- What's the use of games| Game application value research case collection
- Anaconda uninstall again
- [MCU] 2.3 CPU of AT89S52
- Wechat applet realizes location map display and introduces map map without navigation
猜你喜欢

kubernetes 1.24高可用集群二进制部署

Application of knowing things and learning | correlation graph analysis in anti cheating business

C语言怎么学?这篇文章给你完整答案

JS to realize the right-click menu bar function

How to resolve the public domain name to the intranet IP server -- quickly resolve the domain name and map the Internet access

灵魂一问:为什么ES比MySQL更适合复杂条件搜索?

运行loam_velodyne实时建图

机器学习之评价指标(二)——分类评价指标

Mlx90640 infrared thermal imager temperature sensor module development notes (VII)

I got the P8 "top-level" distributed architecture manual crazy spread on Alibaba intranet
随机推荐
机器学习之评价指标(二)——分类评价指标
Notes on standardized management of "ancestral warehouse" of meituan meal
hutool 字符串工具类
卷积神经网络之卷积计算过程个人理解
How to restrict root remote login so that ordinary users have root privileges
[introduction to database system (Wang Shan)] Chapter 4 - Database Security
EF框架简介
工信部再治数据安全,网易易盾“隐私合规”守住企业经营底线
Soul 1: why is es more suitable for complex condition search than MySQL?
Could not obtain transaction-synchronized Session for current thread
shell常见命令(1)——变量大小写转换
多线程导入数据并生成错误文件用redis存储
类的六大关系——依赖和关联的区别
6月第1周易盾业务风控关注 | 新东方学而思等15家机构被顶格罚款
防止sql注入
知物由学 | APP大瘦身,新一代AAB框架下的安全加固之道
WebDriverException( selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executabl
快解析结合海典医药
CPU introduction
Learn from what you know | Yidun self-developed text real-time clustering technology, and wipe out the same kind of harmful content in social networks