Patterns
How do I get only the keys whose properties are strings, functions, or another chosen type?
Map every key to itself or `never`, then index the mapped object to collect the survivors.
The recipe
type KeysByValue<T, Value> = { [K in keyof T]-?: T[K] extends Value ? K : never }[keyof T] type Model = { id: number name: string email: string active: boolean } type TextKeys = KeysByValue<Model, string>
The build compiles this and checks each result below.
How it works
- 01
T[K] extends Value ? K : never
Matching properties keep their key; non-matches disappear into `never`.
- 02
}[keyof T]Indexing turns the property results into one union of keys.
What you get
TextKeys
→"name" | "email"KeysByValue<Model, number>→"id"KeysByValue<Model, Date>
→never
Where it goes wrong
The check is assignability, not exact equality. A literal string property also extends `string`, and a union property matches only when the whole union extends the requested value type.
Takeaway
Mapped filtering is map to key-or-never, then index to collapse the result.
