当前位置:网站首页>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
边栏推荐
- The concept and characteristics of network security grid are simple and popular
- anaconda重新卸载
- JDBC连接数据库读取前台无法显示数据
- 卷积神经网络——从R-CNN,Fast R-CNN到Faster R-CNN,Mask R-CNN
- The whole process of neural network construction is finally completed! Can't you come to me~
- 快解析结合华途文档加密软件
- Understand JVM language
- PostgreSQL 14 支持winserver2022吗?
- Help, boost and take responsibility, the new value and significance of the 6th Tuba rabbit 718 national home decoration Festival
- Anaconda uninstall again
猜你喜欢

细数国产接口协作平台的六把武器!

快解析结合华途文档加密软件

Because the employee set the password to "123456", amd stolen 450gb data?

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

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

Fast parsing combined with Huatu document encryption software

Kubernetes 1.24 high availability cluster binary deployment

机器学习之评价指标(一)——回归评价指标

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

知物由学 | APP大瘦身,新一代AAB框架下的安全加固之道
随机推荐
wallys/DR882-Qualcomm-Atheros-QCA9882-2T2R-MIMO-802.11ac-Mini-PCIe-Wi-Fi-Module-5G-high-power.
微信小程序 实现拨打电话
Wechat applet cloud function batch delete multiple data error: errcode: -502005 database collection not exists
查找表中多余重复记录并删除保留最小一个
Learn from things | Yidun mobile terminal isomorphism practice, improve the official website interaction experience in a few steps
6月第1周易盾业务风控关注 | 新东方学而思等15家机构被顶格罚款
MySQL 中 You can‘t specify target table ‘表名‘ for update in FROM clause错误解决办法
Application of knowing things and learning | correlation graph analysis in anti cheating business
Understand │ what is cross domain? How to solve cross domain problems?
Some suggestions for writing original technical articles
Big gap? Requirements and conditions for candidates with different academic qualifications to take the postgraduate entrance examination
How to learn C language? This article gives you the complete answer
知物由学 | SO加固如何提升Android应用的安全性?
The Ministry of industry and information technology re governs data security, and Netease Yidun "privacy compliance" keeps the bottom line of enterprise operation
Knowledge dry goods: basic storage service novice Experience Camp
Convolutional neural network -- SSD thesis translation
面试常见问题一二
How can we carry out NLP cross language knowledge transfer?
Does PostgreSQL 14 support winserver2022?
Understand JVM language