[TypeScript] Query Properties with keyof and Lookup Types in TypeScript
The keyof operator produces a union type of all known, public property names of a given type. You can use it together with lookup types (aka indexed access types) to statically model dynamic property access in the type system.
Take away:
1. extends
2. keyof
interface Todo {
id: number;
text: string;
completed: boolean;
}
const todo: Todo = {
id: ,
text: "Buy milk",
completed: false
}
// K extends keyof T: K will be the unit types of 'id', 'text', 'completed'
// T[K] is the lookup tells the Typescript the correct return type
function prop<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
type TodoId = Todo['id'];
type TodoText = Todo['text'];
type TodoCompleted = Todo['completed'];
const id: TodoId = prop(todo, 'id'); // type number
const text: TodoText = prop(todo, 'text'); // type string
const completed: TodoCompleted = prop(todo, 'completed'); // type boolean
[TypeScript] Query Properties with keyof and Lookup Types in TypeScript的更多相关文章
- [TypeScript] Understand lookup types in TypeScript
Lookup types, introduced in TypeScript 2.1, allow us to dynamically create types based on the proper ...
- [TypeScript] Transform Existing Types Using Mapped Types in TypeScript
Mapped types are a powerful and unique feature of TypeScript's type system. They allow you to create ...
- [TypeScript] Deeply mark all the properties of a type as read-only in TypeScript
We will look at how we can use mapped types, conditional types, self-referencing types and the “infe ...
- [TypeScript] Model Alternatives with Discriminated Union Types in TypeScript
TypeScript’s discriminated union types (aka tagged union types) allow you to model a finite set of a ...
- [TypeScript] Represent Non-Primitive Types with TypeScript’s object Type
ypeScript 2.2 introduced the object, a type that represents any non-primitive type. It can be used t ...
- [TypeScript] Using Assertion to Convert Types in TypeScript
Sometimes the compiler needs help figuring out a type. In this lesson we learn how to help out the c ...
- [TypeScript] Dynamically Allocate Function Types with Conditional Types in TypeScript
Conditional types take generics one step further and allow you to test for a specific condition, bas ...
- [TypeScript] Make Properties and Index Signatures Readonly in TypeScript
TypeScript 2.0 introduced the readonly modifier which can be added to a property or index signature ...
- [TypeScript] Using Interfaces to Describe Types in TypeScript
It’s easy to pass the wrong value to a function. Typescript interfaces are great because they catch ...
随机推荐
- Win10 启动64位IE浏览器——修改注册表方法
修改注册表[HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main]下的: "TabProcGrowth"=DWOR ...
- 暑假集训 || 2-SAT
推荐论文:https://blog.csdn.net/zixiaqian/article/details/4492926 2-SAT问题是2判定性问题,给出n个集合,每个集合中有两个元素,两个元素之一 ...
- JavaEE-09 Ajax与jQuery在JavaEE项目中的使用
学习要点 JavaScript实现Ajax jQuery实现Ajax JSON JSON-LIB FastJSON JavaScript实现Ajax 认识Ajax 旧版百度地图 百度搜索自动补全 百度 ...
- eclipse下svn的分支与合并指南 - 更新版
http://wenku.baidu.com/link?url=ul5vzBHZpHgzENp46RQwTYrkCUYLeVg9TuhmPM_qisR1BGzp6Qca7onhS-SOzwDYuYdA ...
- luogu 2709小b的询问--莫队
https://www.luogu.org/problemnew/show/P2709 无修改的莫队几乎没有什么太高深的套路,比较模板吧,大多都是在那两个函数上动手脚. 这题询问每一种数字数量的平方和 ...
- 关于inet_ntop、inet_pton中的n和p分别代表的意义
函数名中的p和n非别代表表达(presentation)和数值(numeric).地址的表达格式通常是ASCII字符串,数值格式则是存放到套接字地址结构中的二进制值. 参考自:https://blog ...
- 深入理解Spring IoC容器和动态代理机制
Deployment期间验证 实现一: <bean id="theTargetBean" class="..."/> <bean id=&qu ...
- (2) LVS负载均衡:VS_TUN和VS_DR的arp问题
1. ARP协议简介 ARP(Address Resolution Protocol)协议称为地址解析协议,用于将主机IP地址解析为主机的MAC地址,即IP-->MAC之间一一映射. RARP协 ...
- Go:获取命令行参数
一.Low B 方式 package main import ( "fmt" "os" ) func main() { fmt.Println("命令 ...
- 条款3:尽可能使用const(use const whenever possible)
1.只要这(某值保持不变)是事实,就应该确实说出来,这样可以获得编译器的协助,确保这条约束不被违反. 2.keyword const 有很多种用法,但都简单易用. 2.1classes 外部修饰glo ...