当前位置:网站首页>【ts】typescript高阶:模版字面量类型
【ts】typescript高阶:模版字面量类型
2022-08-05 05:16:00 【六月的可乐】
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
typescript高阶之模版字面量类型
前言
学习目标
1、模板字面量类型语法
2、模板字面量类型示例
一、模板字面量类型语法
模版字面量类型
// `${T}` // 对于模板字面量类型中的类型变量T只能是string、number、boolean、bigint
// 例子
type Direction = 'left' | 'right' | 'top' | 'bottom';
type CssPadding = `padding-${
Direction}`; // "padding-left" | "padding-right" | "padding-top" | "padding-bottom"
type MarginPadding = `margin-${
Direction}`;// "margin-left" | "margin-right" | "margin-top" | "margin-bottom"
// 例子
type EventName<T extends string> = `${
T}Change`;
type Concat<T extends string, U extends string> = `${
T}-${
U}`;
type toString<T extends string | boolean | number | bigint> = `${
T}`;
type T0 = EventName<'foo'>;// "fooChange"
type T1 = Concat<'hello', 'word'>; // "hello-word"
type T2 = toString<true>; // "true"
type T3 = toString<'hello' | false | 2222 | -1234n>; // "hello" | "false" | "2222" | "-1234"
二、模板字面量类型示例
1.减少重复代码、提高复用性
代码如下(示例):
// 例子
type Direction = 'left' | 'right' | 'top' | 'bottom';
type CssPadding = `padding-${
Direction}`; // "padding-left" | "padding-right" | "padding-top" | "padding-bottom"
type MarginPadding = `margin-${
Direction}`;// "margin-left" | "margin-right" | "margin-top" | "margin-bottom"
2.运算规则
1、单个占位符的联合类型自动展开(f分布式匹配原则)
//单个占位符的联合类型自动展开
// A | B | C ====> `[${T}]` ====> `[${A}]` | `[${B}]` |`[${C}]`
// 模板字面量与其他工具类型一起使用
type CetterName<T extends string> = `get${
Capitalize<T>}`;
type Cases<T extends string> = `${
Uppercase<T>}---${
Lowercase<T>}----${
Capitalize<T>}---${
Uncapitalize<T>}`;
type T6 = CetterName<'name'>;// "getName"
type T7 = Cases<'bar'>;// "BAR---bar----Bar---bar"
2、多个占位符的联合类型解析为叉积
// 多个占位符的联合类型解析为叉积
// A | B 和 C| D ====> `${S1}-${S2}` ====> `${A}-${C}` | `${A}-${D}` | `${B}-${C}` | `${B}-${D}`
// 模版字面量类型与条件类型infer一起使用
type InferRoot<T> = T extends `${
infer R}${
Capitalize<Direction>}` ? R : T;
type T8 = InferRoot<'marginLeft'>;// "margin"
type T9 = InferRoot<'paddingTop'>;// "padding"
总结
边栏推荐
- WCH系列芯片CoreMark跑分
- 2022年中总结关键词:裁员、年终奖、晋升、涨薪、疫情
- CVPR最佳论文得主清华黄高团队提出首篇动态网络综述
- [Remember 1] June 29, 2022 Brother and brother double pain
- 如何组织一场安全、可靠、高效的网络实战攻防演习?
- 基于STM32F407的一个温度传感器报警系统(用的是DS18B20温度传感器,4针0.96寸OLED显示屏,并且附带日期显示)
- 华科提出首个用于伪装实例分割的一阶段框架OSFormer
- 基于Flink CDC实现实时数据采集(二)-Source接口实现
- 网络信息安全运营方法论 (下)
- 【数据库和SQL学习笔记】8.SQL中的视图(view)
猜你喜欢

读论文-Cycle GAN

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

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

Thread handler句柄 IntentServvice handlerThread

如何停止flink job
![[Go through 8] Fully Connected Neural Network Video Notes](/img/0a/8b2510b5536621f402982feb0a01ef.png)
[Go through 8] Fully Connected Neural Network Video Notes

【Pytorch学习笔记】8.训练类别不均衡数据时,如何使用WeightedRandomSampler(权重采样器)

【论文阅读-表情捕捉】ExpNet: Landmark-Free, Deep, 3D Facial Expressions

伪RTOS-ProroThread在CH573芯片上的移植
![[Pytorch study notes] 10. How to quickly create your own Dataset dataset object (inherit the Dataset class and override the corresponding method)](/img/71/f82e76085f9d8e6610f6f817e2148f.png)
[Pytorch study notes] 10. How to quickly create your own Dataset dataset object (inherit the Dataset class and override the corresponding method)
随机推荐
[Go through 10] sklearn usage record
【Pytorch学习笔记】9.分类器的分类结果如何评估——使用混淆矩阵、F1-score、ROC曲线、PR曲线等(以Softmax二分类为例)
【论文精读】Rich Feature Hierarchies for Accurate Object Detection and Semantic Segmentation(R-CNN)
如何编写一个优雅的Shell脚本(二)
[Pytorch study notes] 9. How to evaluate the classification results of the classifier - using confusion matrix, F1-score, ROC curve, PR curve, etc. (taking Softmax binary classification as an example)
Machine Learning (1) - Machine Learning Fundamentals
表情捕捉的指标/图像的无参考质量评价
【22李宏毅机器学习】课程大纲概述
Mesos learning
【数据库和SQL学习笔记】9.(T-SQL语言)定义变量、高级查询、流程控制(条件、循环等)
网工必用神器:网络排查工具MTR
【Pytorch学习笔记】10.如何快速创建一个自己的Dataset数据集对象(继承Dataset类并重写对应方法)
MaskDistill-不需要标注数据的语义分割
关于存储IOPS你必须了解的概念
用GAN的方法来进行图片匹配!休斯顿大学提出用于文本图像匹配的对抗表示学习,消除模态差异!
网络信息安全运营方法论 (上)
Spark ML学习相关资料整理
数控直流电源
伪RTOS-ProroThread在CH573芯片上的移植
[Database and SQL study notes] 8. Views in SQL