In TypeScript, how would you type a generic data filter function that takes a predicate and returns only the matching elements with the correct narrowed type?
-
A
function filter(arr: any[], pred: Function): any[]
-
B
function filter<T, U extends T>(arr: T[], pred: (item: T) => item is U): U[]
-
C
function filter<T>(arr: T[], pred: boolean): T[]
-
D
function filter(arr: unknown[]): unknown[]