当前位置:网站首页>[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 .
边栏推荐
- SQOOP 1.4.6 INSTALL
- "Matching" is true love, a new attitude for young people to make friends
- Kustomize使用手册
- SPSS做Shapiro-Wilk正态分析
- [Fantasy 4] the transformation from U3D to UE4
- Nonlinear optimization: steepest descent method, Newton method, Gauss Newton method, Levenberg Marquardt method
- pytest学习--base
- Redis set password
- 2021-10-02
- Stm32 et développement de moteurs (système supérieur)
猜你喜欢
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
allure--常用配置项
Sum the two numbers to find the target value
从MediaRecord录像中读取H264参数
[Fantasy 4] introduction and use of UMG components (under update...)
2021-10-04
Blender volume fog
Use WinDbg to statically analyze dump files (summary of practical experience)
Determine whether there are duplicate elements in the array
SUS系统可用性量表
随机推荐
两数之和,求目标值
从MediaRecord录像中读取H264参数
Blender camera surround motion, animation rendering, video synthesis
Ks009 implement pet management system based on SSH
Windows环境MySQL8忘记密码文件解决方案
1287_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析
Determine whether there are duplicate elements in the array
Lunix reallocates root and home space memory
Nonlinear optimization: establishment of slam model
SPSS做Shapiro-Wilk正态分析
《MySQL 8 DBA基础教程》简介
[unity3d] production progress bar - make image have the functions of filled and sliced at the same time
[visual studio] every time you open a script of unity3d, a new vs2017 will be automatically reopened
Allure -- common configuration items
Mock Server基本使用方法
AttributeError: type object ‘Image‘ has no attribute ‘fromarray‘
stm32和電機開發(上比特系統)
Pytest learning --base
allure--常用配置项
ERROR 1118 (42000): Row size too large (&gt; 8126)