Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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