TypeScript · Senior · Question 8 of 75
How do you keep only the function-valued properties of a type?
type Methods<T> = {
[K in keyof T as T[K] extends Function ? K : never]: T[K];
};- ✓Correct answer: Remapping a key to
neverdrops the property from the result - 2
as nevermakes the property's value typenever - 3This code causes a compile error
Explanation
If the expression after as evaluates to never, the key is dropped. It's the standard technique for filtering keys by value type. Without as, you'd first compute the union of keys and then apply Pick.