typescript中高级类型Record
ts文档上对Record的介绍不多,但却经常用到,Record是一个很好用的工具类型。 他会将一个类型的所有属性值都映射到另一个类型上并创造一个新的类型,先看下Record的源码。
/**
* Construct a type with a set of properties K of type T
*/
type Record<K extends keyof any, T> = {
[P in K]: T;
};
好像源码也比较简单,即将K中的每个属性([P in K]),都转为T类型。常用的格式如下:
type proxyKType = Record<K,T>