devSK
devSK

Reputation: 2026

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>
};

Upvotes: 8

Views: 8530

Answers (1)

CRice
CRice

Reputation: 32206

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.

Upvotes: 17

Related Questions