Created
May 15, 2022 22:58
-
-
Save bennycode/0bcff642734fe2c0c94652eb309b74fc to your computer and use it in GitHub Desktop.
Multiple generic types in TypeScript
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
function rollDice(array: any[]): number { | |
return Math.floor(Math.random() * array.length); | |
} | |
function getRandoms<A, B>(as: A[], bs: B[]): [A, B] { | |
const a = as[rollDice(as)]; | |
const b = bs[rollDice(bs)]; | |
return [a, b]; | |
} | |
const [someName, someNumber] = getRandoms<string, number>(names, numbers); | |
console.log(someName, someNumber); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment