Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to interpret [p in keyof T] and T[p] in these TypeScript declarations?

I am reading ngrx docs and stumbled upon such a code. What do [p in keyof T] and T[p] mean?

export type ActionReducerMap<T, V extends Action = Action> = {
  [p in keyof T]: ActionReducer<T[p], V>
};
like image 708
vulp Avatar asked Dec 19 '22 03:12

vulp


1 Answers

That is a mapped type. You can read about them in the typescript docs here, or in this blog post.

Basically, the syntax [p in keyof T] means just that; p is one of the keys of the object T. Then, the T[p] just represents the type of that key's value. Read those two links for a more robust explanation.

like image 58
CRice Avatar answered Feb 09 '23 00:02

CRice