当前位置:网站首页>[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 .
边栏推荐
- 《MySQL 8 DBA基础教程》简介
- 判断数组中是否存在重复元素
- 【MySQL】连接MySQL时出现异常:Connection must be valid and open
- Thanos Receiver
- [jetbrain rider] an exception occurred in the construction project: the imported project "d:\visualstudio2017\ide\msbuild\15.0\bin\roslyn\microsoft.csh" was not found
- Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
- [unity3d] cannot correctly obtain the attribute value of recttransform, resulting in calculation error
- ERROR 1118 (42000): Row size too large (&gt; 8126)
- Post disaster reconstruction -- Floyd thought
- "Talking about podcasts" vol.352 the age of children: breaking the inner scroll, what can we do before high school?
猜你喜欢

Is this code PHP MySQL redundant?

使用sqlcipher打开加密的sqlite方法

The nanny level tutorial of flutter environment configuration makes the doctor green to the end

Sus system availability scale

对话吴纲:我为什么笃信“大国品牌”的崛起?

Blender volume fog

Flutter——Canvas自定义曲线图

Flutter环境配置保姆级教程,让doctor一绿到底

Mysql database remote access permission settings
![[visual studio] visual studio 2019 community version cmake development environment installation (download | install relevant components | create compilation execution project | error handling)](/img/9f/4265f1e3927fcf66602f0fc9e7a9d9.jpg)
[visual studio] visual studio 2019 community version cmake development environment installation (download | install relevant components | create compilation execution project | error handling)
随机推荐
网络通信学习
Pytest-- test report allure configuration
14. Code implementation of semaphore
The nanny level tutorial of flutter environment configuration makes the doctor green to the end
《MySQL 8 DBA基础教程》简介
[visual studio] every time you open a script of unity3d, a new vs2017 will be automatically reopened
flume 190 INSTALL
记录 AttributeError: ‘NoneType‘ object has no attribute ‘nextcall‘
Sus system availability scale
02-taildir source
07数据导入Sqoop
12. Process synchronization and semaphore
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
SQOOP 1.4.6 INSTALL
Flink实时计算topN热榜
sqoop创建job出现的一系列问题解决方法
VLAN experiment
Shapiro Wilk normal analysis by SPSS
[pit avoidance guide] pit encountered using ugui: the text component cannot indent the first line by two spaces
Lunix reallocates root and home space memory