Skip to content

Instantly share code, notes, and snippets.

@FauxFaux
Created May 3, 2020 16:12
Show Gist options
  • Save FauxFaux/952dd744e6dff70e20856bb55ab46857 to your computer and use it in GitHub Desktop.
Save FauxFaux/952dd744e6dff70e20856bb55ab46857 to your computer and use it in GitHub Desktop.
interface A { a: number }
interface B { b: number }
function ifIs<T>(l: Array<A | B>, isT: (o: any) => o is T, handle: (t: T) => void) {
for (const o of l) {
if (isT(o)) {
handle(o);
}
}
}
ifIs([{a: 5}, {b: 6}], isB, (b: B) => console.log(b.b));
function isA(o: any): o is A {
return 'a' in o;
}
function isB(o: any): o is B {
return 'b' in o;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment