I think the code will speak by itself, so here it is:
type myType = "a" | "b" | "c";
type myMappedType = { [str in myType]: number };
const values: myMappedType = {
a: 1,
b: 2,
c: 3
};
const keys = Object.keys as <T>(o: T) => Extract<keyof T, string>[];
const keys2 = Object.keys as <T>(o: T) => Array<keyof T>;
Object.keys(values) // string[]
keys(values) // myType[]
keys2(values) // myType[]
What is the better way to cast the return value of Object.keys()?
- Should I use the
keysor thekeys2implementation? Or even another one? - Which one is better and why?
Thanks in advance!