当前位置:网站首页>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
边栏推荐
- [introduction to database system (Wang Shan)] Chapter 4 - Database Security
- Know things by learning | build a real-time anti plug-in mechanism from 0 to 1 to supplement the offensive and defensive power of mobile games in multiple dimensions
- 备份表恢复表
- 7月第4周易盾业务风控关注 | 最高法对APP强索个人信息进行规制
- Machine learning: IOU of concept understanding
- 查找表中多余重复记录并删除保留最小一个
- 笔试缺考者入围教师招聘面试?河南祥符:个别考生成绩统计错误
- 知物由学 | 关联图分析在反作弊业务中的应用
- mysql解决唯一索引重复导致的插入失败问题
- 防止sql注入
猜你喜欢

2022 high altitude installation, maintenance and removal of test question simulation test platform operation

知物由学 | 关联图分析在反作弊业务中的应用

知物由学 | 易盾移动端同构实践,几步改善官网交互体验

How can we carry out NLP cross language knowledge transfer?

Numpy array matrix operation

Yanrong technology was selected as Beijing's "specialized and innovative" in 2022 to lead hybrid cloud file storage

TCP connection status identification (syn, fin, ACK, PSH, RST, urg)

Convolutional neural network -- Translation of yolov2 (yolo9000) papers

Zhengzhou University database course resource description

知物由学 | APP大瘦身,新一代AAB框架下的安全加固之道
随机推荐
Convolutional neural network -- Introduction to FPN (feature pyramid networks)
I got the P8 "top-level" distributed architecture manual crazy spread on Alibaba intranet
#yyds干货盘点# 面试必刷TOP101:链表内指定区间反转
知物由学 | 再造巴别塔,我们如何进行NLP跨语言知识迁移?
golang 等待一组goroutine完成,并带返回值(2)
About the problem of abnormal C printing
Application of knowing things and learning | correlation graph analysis in anti cheating business
微信小程序 云函数批量删除多条数据 Error: errCode: -502005 database collection not exists
Because the employee set the password to "123456", amd stolen 450gb data?
Count the six weapons of the domestic interface cooperation platform!
树莓派驱动代码的编译和测试
使用分布式框架WCF出现的BUG记录
美团到餐“祖传数仓”标准化治理笔记
知物由学 | APP大瘦身,新一代AAB框架下的安全加固之道
2022 safety officer-a certificate examination questions and online simulation examination
CPU introduction
Learn from things | Yidun mobile terminal isomorphism practice, improve the official website interaction experience in a few steps
卷积神经网络——FPN(Feature Pyramid Networks)介绍
ES查询限制10000条数据解决方法
机器学习——概念理解之IoU