Created
January 3, 2019 15:14
-
-
Save OliverJAsh/acafba4f099f6e677dbb0a38c60dc33d to your computer and use it in GitHub Desktop.
TypeScript object filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const tuple = <T extends Array<unknown>>(...args: T): T => args; | |
// `Object.keys` does not return the keys as string literals, only strings. Use this helper as a | |
// workaround. https://github.com/Microsoft/TypeScript/pull/12253#issuecomment-263132208 | |
const keys = <O extends object>(obj: O) => Object.keys(obj) as Array<keyof O>; | |
// `Object.entries` is ES2017, so we must define our own version. | |
const entries = <K extends string, V>(obj: Record<K, V>) => | |
keys(obj).map(key => tuple(key, obj[key])); | |
const fromEntries = <K extends string, V>(arr: Array<[K, V]>) => | |
// `Object.assign` is poorly typed: it returns `any` when spreading. Use cast to workaround. | |
Object.assign({}, ...arr.map(([k, v]) => ({ [k]: v }))) as Record<K, V>; | |
// Inspired by https://stackoverflow.com/a/37616104/5932012 | |
export const filter = <K extends string, V, Result extends V>( | |
obj: Record<K, V>, | |
predicate: (key: K, value: V) => value is Result, | |
) => | |
fromEntries( | |
entries(obj).filter( | |
(entry): entry is [K, Result] => { | |
const [key, value] = entry; | |
return predicate(key, value); | |
}, | |
), | |
); |
I keep getting the
boolean' must be a type predicate.
error when trying to use asArray.filter
works, that is returning a boolean
Sounds like you need to add an overload that supports boolean
returning predicates. At the moment it is typed to support just user-defined type guards.
Could you please show an example of a user-defined type guard
and the usage with the filter? my typescript skills are still quite basic
Very appreciated 🙇
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please explain the usage, I keep getting the
boolean' must be a type predicate.
error when trying to use asArray.filter
works, that is returning a boolean, If I set everything asany
like above it works though, but I don't think we should resort to that.