[Typescript] Pick partially object type with indexed access types
作者:互联网
type PartOfWindow = {
[Key in
| "document"
| "navigator"
| "setTimeout"]: Window[Key]
}
/*
type PartOfWindow = {
document: Document;
navigator: Navigator;
setTimeout: (handler: TimerHandler, timeout?: number | undefined, ...arguments: any[]) => number;
}
*/
We can refacotr the type to be more generalic
type PickWindowProperties<Keys extends keyof Window> = {
[Key in Keys]: Window[Key]
}
type PartOfWindow = PickWindowProperties<"document" | "navigator" | "setTimeout">
Further:
type PickProperties<
ValueType,
Keys extends keyof ValueType
> = {
[Key in Keys]: ValueType[Key]
}
type PartOfWindow = PickProperties<
Window,
"document" | "navigator" | "setTimeout"
>
标签:PartOfWindow,Typescript,object,number,PickProperties,access,Window,Key,type 来源: https://www.cnblogs.com/Answer1215/p/16599829.html