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

最新文章

  1. JavaScript 构造函数与原型链
  2. iOS--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook等系统服务开发汇总
  3. php ffmpeg
  4. json 和 pickel 详解
  5. SharePoint Server 2007 简体中文下载
  6. vs2010 无法连接到asp.net development server
  7. CodeForces 489C Given Length and Sum of Digits... (贪心)
  8. 破解Windows Server 2003只允许3个用户远程登陆
  9. C# To C++ Converter Cracked ( 破解版 )
  10. git 学习(3) ----- 代码共享和多人协作
  11. 如何快速开发html5跨平台K12/幼儿交互课件、动画课件、交互游戏
  12. 带你了解源码中的 ThreadLocal
  13. confd
  14. go,gcvis,golang, privoxy,and git proxy
  15. 使用ASP.NET AJAX与Bootstrap 弹窗解决方案
  16. MCI 录制指定格式音频
  17. PBOCIC读芯片卡流程
  18. Rabbitmq安装及启动 MAC系统
  19. bzoj 1237 [SCOI2008]配对 贪心+dp
  20. Hibernate查询_HQL_EJBQL_QBC_QBE

热门文章

  1. ansible2.7学习笔记系列
  2. 第1节 flume:9、flume的多个agent串联(级联)
  3. ffmpeg解析TS流
  4. 关于U盘安装ubuntu-18.04安装时候出现的grub-efi-amd64-signed的问题。
  5. 前端基础之CSS_2
  6. UVALive - 6275 Joint Venture (二分)
  7. JSP配置即报错以及解决办法(未更新完)
  8. luogu3380 【模板】二逼平衡树(树套树)
  9. Python浅拷贝copy()与深拷贝deepcopy()区别
  10. 《Docker容器与容器云》读书笔记