当前位置:网站首页>TypeScript-键盘映射
TypeScript-键盘映射
2022-06-11 07:54:00 【YY小怪兽】
1.什么是映射类型?
根据旧的类型创建出新的类型, 我们称之为映射类型
我们可以通过+/-来指定添加还是删除 只读和可选修饰符
interface TestInterface1{
name:string,
age:number
}
interface TestInterface2{
readonly name?:string,
readonly age?:number
}
type ReadonlyTestInterface<T> = {
// [P in keyof T]作用: 遍历出指定类型所有的key, 添加到当前对象上
// readonly [P in keyof T]: T[P]
// readonly [P in keyof T]?: T[P]
-readonly [P in keyof T]-?: T[P]
}
//将TestInterface2取消只读和可选
//type MyType ={
// name:string,
// age:number
//}
type MyType = ReadonlyTestInterface<TestInterface2>
2.由于生成只读属性和可选属性比较常用, 所以TS内部已经给我们提供了现成的实现
/*只读 type MyType2 = { readonly name:string, readonly age:number } */
type MyType2 = Readonly<TestInterface1>
/*可选 type MyType3 = { name?:string, age?:number } */
type MyType3 = Partial<TestInterface1>
/*只读可选 MyType4 ={ readonly name?:string, readonly age?:number } */
type MyType4 = Partial<Readonly<TestInterface1>>
3.Pick映射类型
将原有类型中的部分内容映射到新类型中
interface TestInterface {
name:string,
age:number
}
/*type MyType = { name:string, */
type MyType = Pick<TestInterface, 'name'>
4.Record映射类型
他会将一个类型的所有属性值都映射到另一个类型上并创造一个新的类型
type Animal = 'person' | 'dog' | 'cat';
interface TestInterface {
name:string;
age:number;
}
type MyType = Record<Animal, TestInterface>
/*type MyType ={ person:TestInterface, dog:TestInterface, cat:TestInterface } */
let res:MyType = {
person:{
name:'zs',
age:18
},
dog:{
name:'wc',
age:3
},
cat:{
name:'mm',
age:2
}
}
边栏推荐
- Three expressions of integers and their storage in memory
- [IOT] intelligent hardware: how to obtain the WiFi signal strength of hardware products
- [untitled] Weng_ C lesson 1
- The solution of "no startup device" after running Bochs
- Bladed入门教程(视频)
- C. Manipulating History(贪心/哈希/思维/好题)
- 20200810 T2 dispatch money
- C language - Growth Diary -01- count primes and sum
- Servlet、ServletConfig、ServletContext
- Introduction to operations research
猜你喜欢

空间几何

Zero foundation self-study SQL course | union joint query

C language - growth diary-04- preliminary exploration of local variables (local variables)

Deux diplômés, la Banque a externalisé le travail d'essai pendant plus de quatre mois. Parler de vrais sentiments...

2021-11-05 definition of cache

Qunhui ds918 creates m.2 SSD read / write cache
![Socket [5] - struct linker usage](/img/e5/bce5d89c61a3a4f89171734ca6ff8e.png)
Socket [5] - struct linker usage

Return in foreach and break in for

After 4 years of naked resignation from the test, the test post of 15K interview was rubbed on the ground, and the result made me collapse and cry

mpi
随机推荐
[atcoder2000] leftmost ball (dp+ combination number)
[atcoder2305] declining (game)
2022.6.7 特长生模拟
C language lesson 2
Sort - Swap sort
Space geometry
Three expressions of integers and their storage in memory
远程办公经验分享 | 社区征文
Xshell7 and xftp7 to continue using this program, you must apply the latest updates or use a new version
Flask页面的分页
[atcoder2307] tree game
Printing diamond of beginner C
[cluster] haproxy load balancing
C language Yanghui triangle code
Summary of evaluation index knowledge points in target detection: summary of IOU cross overlap unit and map/ap/tp/fp/np
Detailed explanation of shift operator and bit operator in C language
C# 微信上传Form-data
C language to achieve a simple game - minesweeping
Black Qunhui dsm7.0.1 physical machine installation tutorial
VIM common commands