当前位置:网站首页>[TS] 1368 seconds understand typescript generic tool types!
[TS] 1368 seconds understand typescript generic tool types!
2022-07-02 10:45:00 【pingan8787】
Brother Po carefully prepared 《 Easy to learn TypeScript》 The video tutorial has been updated to Issue 19 了 , The aggregate playback volume has reached 14W+. Through vivid animation , Make it easy for you to understand TypeScript Difficulties and core knowledge points !
If you just touched TypeScript Soon , In the reading TypeScript The usage of the built-in tool type and the internal implementation of the article , You might see Pick Tool type , You may feel strange to this type of grammar .
type Pick<T, K extends keyof T> = {
[P in K]: T[P];
};
type User = {
id: number;
name: string;
address: string;
};
type PickedUser = Pick<User, "id" | "name">;In fact, a better way to learn new things is to use analogy . Next, brother Po will use JavaScript To help you quickly understand Pick The syntax behind the tool type .
function Pick(obj, keys) {
const ret = {};
for (const key of keys) {
ret[key] = obj[key];
}
return ret;
}
const user = {
id: 666,
name: " A Bao Ge ",
address: " Xiamen ",
};
const PickedUser = MyPick(user, ["id", "name"]);In the above code ,function Is a keyword used to declare a function ,Pick It's the function name , And in parentheses obj and keys Is the parameter , What is defined in braces is the function body .

And for Pick For tool types ,type Keyword is used to alias a type , Easy to reuse ,Pick Is the name of the type . Within angle brackets T and K It belongs to type parameter , And JavaScript The difference between parameters in functions is that type parameters store types , and JavaScript Function parameters store values .

among extends Is the syntax of generic constraints , Scope for constraint type . The specific implementation of this tool type is in braces , It has been used. TypeScript The syntax of mapping types .
Actually , You can take TypeScript Built in tool types are understood as special functions , They are used to deal with TypeScript Types that exist in . How to call the tool type and call JavaScript The difference between functions is that angle brackets are used .
Last , For your understanding , Po brother demonstrates it in the form of pictures Pick Execution process of tool type .



If you want to know more about mapping types , Can watch “ It was used TS Mapping type , Colleagues call experts !” This article .
Scan the code to check Easy to learn TypeScript Series video tutorial

( Currently updated 19 period )
You like to learn in this form TS Well ? Like it , Remember to praise and collect .
边栏推荐
- 01安装虚拟机
- Thanos Receiver
- Sum the two numbers to find the target value
- Nonlinear optimization: steepest descent method, Newton method, Gauss Newton method, Levenberg Marquardt method
- UVM - usage of common TLM port
- Transport Optimization abstraction
- 两数之和,求目标值
- [unity3d] cannot correctly obtain the attribute value of recttransform, resulting in calculation error
- Feature (5): how to organize information
- "Matching" is true love, a new attitude for young people to make friends
猜你喜欢

Flutter——Canvas自定义曲线图

【Unity3D】嵌套使用Layout Group制作拥有动态子物体高度的Scroll View
![[MySQL] an exception occurs when connecting to MySQL: connection must be valid and open](/img/09/a75ce6742d787c640b8066e0e8e694.png)
[MySQL] an exception occurs when connecting to MySQL: connection must be valid and open

01 install virtual machine

Kustomize使用手册

sqoop创建job出现的一系列问题解决方法

Session cookies and tokens

Introduction to MySQL 8 DBA foundation tutorial

Win11 arm系统配置.net core环境变量

14. Code implementation of semaphore
随机推荐
2021-10-04
MongoDB-快速上手MongoDB命令行的一些简单操作
Postman--使用
12.进程同步与信号量
14.信号量的代码实现
13.信号量临界区保护
《实习报告》Skywalking分布式链路追踪?
Flink实时计算topN热榜
传输优化抽象
MPLS experiment
VLAN experiment
拆解美图SaaS:开着飞机换引擎
[MySQL] an exception occurs when connecting to MySQL: connection must be valid and open
简洁、快速、节约内存的Excel处理工具EasyExcel
[SUCTF2018]followme
618再次霸榜的秘密何在?耐克最新财报给出答案
Blender camera surround motion, animation rendering, video synthesis
webUI自动化学习
KS009基于SSH实现宠物管理系统
Introduction and Principle notes of UE4 material