[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 ...
随机推荐
- 关于mysql服务突然运行不了的问题-“本地计算机上的mysql服务启动后停止,某些...”
1.将mysql数据库的安装目录bin文件路径添加到环境的path中,为了让cmd可以直接输入下面的相关命令,不然cd到mysql的bin下也可以 2.cmd输入mysqld --initialize ...
- 【牛客小白月赛6】F 发电 - 树状数组&快速幂&逆元
题目地址:https://www.nowcoder.com/acm/contest/136/F 树状数组.快速幂.逆元的模板运用: #include<iostream> #include& ...
- [CF] 402 E. Strictly Positive Matrix
一个矩阵,自乘无限次后能否全为正数? 如果n比较小,可以二分一下,但是这里n很大,乘一次都无法接受 可以考虑实际含义:矩阵看成邻接矩阵,那么0就是没有边,其余就是有边. 我们知道邻接矩阵自乘k次就相当 ...
- NFS和DHCP服务
1. NFS NFS,Network File System的简写,即网络文件系统.网络文件系统是FreeBSD支持的文件系统中的一种,也被称为NFS: NFS允许一个系统在网络上与他人共享目录和文件 ...
- 3. express 框架使用 vue框架 weiUI
express 1. 安装 npm install express --save 2. 创建项目 vue js 安装Vuejs vue-cli 是一个官方发布 vue.js 项目脚手架,使用 vue- ...
- python +selenium 自带case +生成报告的模板
https://github.com/huahuijay/python-selenium2这个就是 python +selenium的 里面还自带case 然后也有生成报告的模板 我的: https: ...
- PTA 05-树7 堆中的路径 (25分)
题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/713 5-5 堆中的路径 (25分) 将一系列给定数字插入一个初始为空的小顶堆H[] ...
- [POJ2446] Chessboard(二分图最大匹配-匈牙利算法)
传送门 把所有非障碍的相邻格子彼此连一条边,然后求二分图最大匹配,看 tot * 2 + k 是否等于 n * m 即可. 但是连边不能重复,比如 a 格子 和 b 格子 相邻,不能 a 连 b ,b ...
- [POJ1797] Heavy Transportation(最大生成树 || 最短路变形)
传送门 1.最大生成树 可以求出最大生成树,其中权值最小的边即为答案. 2.最短路 只需改变spfa里面的松弛操作就可以求出答案. ——代码 #include <queue> #inclu ...
- 两行代码搞定UI主流框架
XCNavTab XCNavTab适用于快速搭建NavigationController和TabBarController相结合的框架 https://github.com/xiaocaiabc/XC ...