Skip to content

Instantly share code, notes, and snippets.

@bennycode
Created May 15, 2022 22:58
Show Gist options
  • Save bennycode/0bcff642734fe2c0c94652eb309b74fc to your computer and use it in GitHub Desktop.
Save bennycode/0bcff642734fe2c0c94652eb309b74fc to your computer and use it in GitHub Desktop.
Multiple generic types in TypeScript
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