Check out this code snippet:
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[]
Which method do you think is best for casting the return value of Object.keys()?
- Would you go with the
keys
or thekeys2
approach? Or perhaps something different? - Share your thoughts on which one you believe is superior and why.
Appreciate your input!