Created
May 3, 2020 16:12
-
-
Save FauxFaux/952dd744e6dff70e20856bb55ab46857 to your computer and use it in GitHub Desktop.
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
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