In
type GetTruthyKeys<T extends object> = {
[key in keyof T]: T[key] extends false ? never : key
}[keyof T];
the [keyof T]
part seems to be extracting keys from the mapped type, but why does it work?
If
{
[key in keyof T]: T[key] extends false ? never : key
}
part simply maps the object by changing the false
type with never
and other types with key value, so that this
{
first: true;
second: false;
third: string;
}
going to be mapped to this
{
first: "first";
second: never;
third: "third";
}
doesn't it mean that to get keys of the latter type we need to put keyof
in front of the mapping? So it would look like this
type GetTruthyKeys<T extends object> = keyof {
[key in keyof T]: T[key] extends false ? never : key
}; // it returns union of all three keys, even with one that has type of false
So how and why the [keyof T]
works in this case?