当前位置:网站首页>【ts】typescript高阶:typeof使用
【ts】typescript高阶:typeof使用
2022-08-05 05:16:00 【六月的可乐】
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
typescript高阶:typeof使用
前言
学习目标
1、typeof与对象结合使用
2、typeof与枚举结合使用
3、typeof与class类结合使用
4、const断言的使用
一、typeof与对象结合使用
代码如下(示例):
let lolo = {
name: 'zhanhsan',
age: 18,
child: {
name: 'zhangsansan',
like: true,
age: 12
}
};
type Lolo = typeof lolo;
// {
// name: string;
// age: number;
// child: {
// name: string;
// like: boolean;
// age: number;
// };
// }
type Lolochild = typeof lolo.child;
// {
// name: string;
// like: boolean;
// age: number;
// }
二、typeof与枚举结合使用
代码如下(示例):
enum HttpMethod {
Get,
Post
};
// 注意枚举类型之间的转换关系 如下
// 上面ts枚举类型写法等价于es5中的写法:
// "strict"
// var HttpMethod;
// (function (HttpMethod) {
// HttpMethod[HttpMethod['Get' = 0]] = 'Get';
// HttpMethod[HttpMethod['Post' = 1]] = 'Post';
// })(HttpMethod || HttpMethod = {});
type Methods = typeof HttpMethod;
const meth: Methods = {
Get: 0,
Post: 1
}
type Meth = keyof typeof HttpMethod;// "Get" | "Post"
三、typeof与class类结合使用
代码如下(示例):
class Ponit {
x: number;
y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
};
// 工厂函数
// 这里 typeof Point ------> new (x: number, y: number) => number;
function getInstance(PointClass: typeof Ponit, x: number, y: number) {
return new PointClass(x, y);
}
// 下面写法将报错
function getInstance2(PointClass: Ponit, x: number, y: number) {
return new PointClass(x, y);// 报错 此表达式不可构造。类型 "Ponit" 没有构造签名。
}
四、typeof与函数结合使用
代码如下(示例):
// typeof与函数结合使用
function add(a: number, b: number): number {
return a + b;
};
type AddType = typeof add;// (a: number, b: number) => number
type AddReturnType = ReturnType<AddType>;// number
type AddParamsType = Parameters<AddType>;// [a: number, b: number]
五、const断言的使用
代码如下(示例):
// 5 const断言可以是类型推断更加准确
let requestMethod = 'get'; // string
let requestMethod2 = 'get' as const; // "get"
let requestMethod3 = <const>'get'; // "get"
let user9 = {
id: 333,
name: 'lisi'
}
type User9 = typeof user9;
// {
// id: number;
// name: string;
// }
let user92 = {
id: 333,
name: 'lisi'
} as const;
type User92 = typeof user92;
// {
// readonly id: 333;
// readonly name: "lisi";
// }
总结
边栏推荐
- Comparison and summary of Tensorflow2 and Pytorch in terms of basic operations of tensor Tensor
- Web Component-处理数据
- 如何编写一个优雅的Shell脚本(三)
- 【数据库和SQL学习笔记】9.(T-SQL语言)定义变量、高级查询、流程控制(条件、循环等)
- 如何编写一个优雅的Shell脚本(二)
- 关于存储IOPS你必须了解的概念
- 如何编写一个优雅的Shell脚本(一)
- SQL (2) - join window function view
- spingboot 容器项目完成CICD部署
- Tensorflow steps on the pit notes and records various errors and solutions
猜你喜欢
随机推荐
Detailed explanation of BroadCast Receiver (broadcast)
spark-DataFrame数据插入mysql性能优化
Flink Distributed Cache 分布式缓存
网工必用神器:网络排查工具MTR
神经网络也能像人类利用外围视觉一样观察图像
如何编写一个优雅的Shell脚本(二)
MySQL
SSL 证书签发详细攻略
[Go through 8] Fully Connected Neural Network Video Notes
如何编写一个优雅的Shell脚本(一)
基于STM32F407的WIFI通信(使用的是ESP8266模块)
SQL (2) - join window function view
【数据库和SQL学习笔记】6.SELECT查询4:嵌套查询、对查询结果进行操作
六步搞定子网划分
[Pytorch study notes] 10. How to quickly create your own Dataset dataset object (inherit the Dataset class and override the corresponding method)
转正菜鸟前进中的经验(废话)之谈 持续更新中... ...
dataframe 常用操作
《基于机器视觉测量系统的工业在线检测研究》论文笔记
[Go through 4] 09-10_Classic network analysis
物联网-广域网技术之NB-IoT









