当前位置:网站首页>【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";
// }
总结
边栏推荐
- 【论文精读】R-CNN 之预测框回归(Bounding box regression)问题详述
- 通过Flink-Sql将Kafka数据写入HDFS
- 【数据库和SQL学习笔记】8.SQL中的视图(view)
- CH32V307 LwIP移植使用
- 盘点关于发顶会顶刊论文,你需要知道写作上的这些事情!
- Tensorflow steps on the pit notes and records various errors and solutions
- A deep learning code base for Xiaobai, one line of code implements 30+ attention mechanisms.
- Lecture 3 Gradient Tutorial Gradient Descent and Stochastic Gradient Descent
- 【数据库和SQL学习笔记】5.SELECT查询3:多表查询、连接查询
- Facial Motion Capture 调研
猜你喜欢

CH32V307 LwIP移植使用

The University of Göttingen proposed CLIPSeg, a model that can perform three segmentation tasks at the same time

Mysql-连接https域名的Mysql数据源踩的坑

2022年中总结关键词:裁员、年终奖、晋升、涨薪、疫情

CVPR2020 - 自校准卷积

网络信息安全运营方法论 (上)

初识机器学习

AWS 常用服务

11%的参数就能优于Swin,微软提出快速预训练蒸馏方法TinyViT

【数据库和SQL学习笔记】7.SQL中的插入(INSERT)、删除(DELETE)、更新(UPDATE)
随机推荐
【数据库和SQL学习笔记】4.SELECT查询2:排序(ORDER BY)、聚合函数、分组查询(GROUP BY)
网络信息安全运营方法论 (上)
CVPR 2022 | 70% memory savings, 2x faster training
Flink Broadcast 广播变量
物联网-广域网技术之NB-IoT
门徒Disciples体系:致力于成为“DAO世界”中的集大成者。
BFC详解(Block Formmating Context)
如何跟踪网络路由链路&检测网络健康状况
SharedPreferences和SQlite数据库
沁恒MCU从EVT中提取文件建立MounRiver独立工程
Flink和Spark中文乱码问题
MySQL
ECCV2022 | RU&谷歌提出用CLIP进行zero-shot目标检测!
Spark ML学习相关资料整理
flink项目开发-flink的scala shell命令行交互模式开发
Flink HA配置
「实用」运维新手一定不能错过的17 个技巧
MaskDistill-不需要标注数据的语义分割
[Practice 1] Diabetes Genetic Risk Detection Challenge [IFLYTEK Open Platform]
怎样在Disciples门徒获得收益?