Issue
Given an object, in TypeScript it's easy to figure out what the type of the values is-just use T[keyof T]
:
type MyRec = Record<string, number>;
type MyRecValue = MyRec[keyof MyRec]; // number
What is the equivalent of T[keyof T]
for Map
? Is there something built in to the TS standard library?
Solution
There is no built-in type to to this. You need to write your own helper:
type ValueOfMap<M extends Map<unknown, unknown>> = M extends Map<unknown, infer V> ? V : never
Now it will work:
type MyMap = Map<string, number>;
type MyMapValue = ValueOfMap<MyMap>; // number
Thanks to this answer for helping me figure it out: https://stackoverflow.com/a/60737746/2697506
Answered By - mesqueeb
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.