Last active
March 2, 2019 16:52
-
-
Save xiel/5a6b47990044e2d8e19e9f2893802b3c to your computer and use it in GitHub Desktop.
Typescript | Conditional Types
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
/* | |
tyescript different sets of parameters OR function signature | |
*/ | |
interface foo { | |
foo1: string | |
foo2: string | |
blubb: string | |
} | |
interface bar { | |
bar1: number | |
bar2: number | |
blubb: number | |
} | |
function that(accepts: bar | foo) { | |
if ('foo1' in accepts) { | |
console.log(accepts.blubb) | |
onlyFoo(accepts) | |
return accepts.foo1 + accepts.foo2 | |
} else { | |
console.log(accepts.blubb) | |
return accepts.bar1 + accepts.bar2 | |
} | |
} | |
function onlyFoo(foo: foo) { | |
console.log(foo.foo1) | |
} | |
that({ | |
foo1: "foo1", | |
foo2: "foo2", | |
blubb: "blubb in foo" | |
}) | |
that({ | |
bar1: 2, | |
bar2: 1, | |
blubb: 0xb1cb | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment