当前位置:网站首页>【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";
// }
总结
边栏推荐
- MySQL
- ECCV2022 | RU&谷歌提出用CLIP进行zero-shot目标检测!
- 【Pytorch学习笔记】8.训练类别不均衡数据时,如何使用WeightedRandomSampler(权重采样器)
- CVPR 2020 - 频谱正则化
- el-pagination分页分页设置
- 基于STM32F407的WIFI通信(使用的是ESP8266模块)
- Oracle压缩表修改字段的处理方法
- IJCAI 2022|边界引导的伪装目标检测模型BGNet
- 网工必用神器:网络排查工具MTR
- Lecture 3 Gradient Tutorial Gradient Descent and Stochastic Gradient Descent
猜你喜欢

2021电赛资源及经验总结

Lecture 3 Gradient Tutorial Gradient Descent and Stochastic Gradient Descent

MSRA proposes extreme masking model ExtreMA for learning instances and distributed visual representations

【论文精读】ROC和PR曲线的关系(The relationship between Precision-Recall and ROC curves)

A deep learning code base for Xiaobai, one line of code implements 30+ attention mechanisms.

物联网-广域网技术之NB-IoT

Tensorflow steps on the pit notes and records various errors and solutions

【论文精读】Rich Feature Hierarchies for Accurate Object Detection and Semantic Segmentation(R-CNN)

网络ID,广播地址,掩码位数计算

网络信息安全运营方法论 (中)
随机推荐
【Pytorch学习笔记】10.如何快速创建一个自己的Dataset数据集对象(继承Dataset类并重写对应方法)
关于基于若依框架的路由跳转
BFC详解(Block Formmating Context)
【论文精读】Rich Feature Hierarchies for Accurate Object Detection and Semantic Segmentation(R-CNN)
【Over 15】A week of learning lstm
[Practice 1] Diabetes Genetic Risk Detection Challenge [IFLYTEK Open Platform]
【Pytorch学习笔记】8.训练类别不均衡数据时,如何使用WeightedRandomSampler(权重采样器)
flink项目开发-flink的scala shell命令行交互模式开发
【论文精读】ROC和PR曲线的关系(The relationship between Precision-Recall and ROC curves)
【Kaggle项目实战记录】一个图片分类项目的步骤和思路分享——以树叶分类为例(用Pytorch)
flink on yarn 集群模式启动报错及解决方案汇总
11%的参数就能优于Swin,微软提出快速预训练蒸馏方法TinyViT
轻松接入Azure AD+Oauth2 实现 SSO
【数据库和SQL学习笔记】9.(T-SQL语言)定义变量、高级查询、流程控制(条件、循环等)
2021电赛资源及经验总结
大型Web网站高并发架构方案
CVPR最佳论文得主清华黄高团队提出首篇动态网络综述
Flink Distributed Cache 分布式缓存
如何编写一个优雅的Shell脚本(一)
发顶会顶刊论文,你应该这样写作