Extracting Keys from Types
What
You can use mapped types to extract a union of property names (keys) that match a specific type condition.
How
First, you create a mapped type where the values are the keys themselves (or never). Then, you index into that type using keyof T to get a union of the results:
type StringKeyUnion<T> = {
[K in keyof T]: T[K] extends string ? K : never;
}[keyof T];
type Result = StringKeyUnion<{ name: string; age: number }>; // "name"
Why
Extracting keys is powerful for building dynamic UIs and libraries. For example, if you are building a search filter, you might want to automatically generate a list of all fields that are strings, while ignoring numbers or dates. This technique allows you to derive those lists directly from your type definitions.
Summary
Extracting keys allows you to build dynamic logic based on the internal structure of your types.