When I set out to create a type that represents the values of a given object type, I initially came up with this:
type Book = {
name:string,
year:number,
author:string
}
// expected result "string" | "number"
type ValueOf<T extends {}> = T[k in keyof T] // error due to "in"
let x:ValueOf<Book> ;
However, it turns out that using in
here is unnecessary. I could have simply used T[keyof T]
.
I've noticed the use of the in
operator in similar scenarios like this one:
type OptionsFlags<Type> = {
[Property in keyof Type]: boolean;
};
Why wasn't it necessary in my situation? What key concept did I overlook regarding the usage of in
? When should I consider using in
as a rule of thumb?