Skip to content

Instantly share code, notes, and snippets.

@eamonnboyle
eamonnboyle / assert-variadic.ts
Created Mar 23, 2021
Variadic Function in Middle Position
View assert-variadic.ts
function assertAreEqual(...args: [number, number, ...number[], string]): void {
const [firstValue, ...remaining] = args.slice(0, args.length - 1) as number[];
const message = args[args.length - 1] as string;
for (const value of remaining) {
if (firstValue !== value) throw new Error(message);
}
}
assertAreEqual(1, 1, 1, 1, "They're not the same");
@eamonnboyle
eamonnboyle / variadic-42-tuples.ts
Created Mar 23, 2021
Leading and Middle Variadic Tuples
View variadic-42-tuples.ts
type Student = [string, ...number[], boolean];
const bob: Student = ['Bob', true];
const sally: Student = ['Sally', 100, true];
const jane: Student = ['Jane', 80, 90, 100, true];
@eamonnboyle
eamonnboyle / unwrap-promise.ts
Created Mar 23, 2021
Remapping Tuple of Promises
View unwrap-promise.ts
type RemapPromises<T extends [...any[]]> = Promise<UnwrapPromises<T>>;
@eamonnboyle
eamonnboyle / testing-unwrap.ts
Created Mar 23, 2021
Testing Unwrap Promises
View testing-unwrap.ts
// []
type TestA = UnwrapPromises<[]>;
// [number]
type TestB = UnwrapPromises<[Promise<number>]>;
// [number, string, Date, boolean]
type TestC = UnwrapPromises<[Promise<number>, Promise<string>, Date, boolean]>;
@eamonnboyle
eamonnboyle / unwrap-promise.ts
Created Mar 23, 2021
Unwrapping a Tuple of Promises
View unwrap-promise.ts
type UnwrapPromises<T extends [...any[]]> =
T extends [infer Head, ...infer Tail]
? [UnwrapPromise<Head>, ...UnwrapPromises<Tail>]
: [];
@eamonnboyle
eamonnboyle / unwrap-promise.ts
Created Mar 23, 2021
Unwrapping Single Promise
View unwrap-promise.ts
type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;
@eamonnboyle
eamonnboyle / many-parameters.ts
Last active Mar 23, 2021
10+ Parameters to Promise.all
View many-parameters.ts
await promiseAll(
Promise.resolve(1), Promise.resolve('2'), Promise.resolve(3), Promise.resolve(4),
Promise.resolve('5'), Promise.resolve('6'), Promise.resolve(7), Promise.resolve(8),
Promise.resolve(9), Promise.resolve('10'), Promise.resolve('11'), Promise.resolve('12'),
); // [number, string, number, number, string, string, number, number, number, string, string, string]
@eamonnboyle
eamonnboyle / variadic-promise-all.ts
Created Mar 23, 2021
Variadic Promise All with Variadic Tuple Parameter
View variadic-promise-all.ts
function promiseAll<T extends [...any[]]>(...promises: [...T]): RemapPromises<T> {
return Promise.all(promises) as RemapPromises<T>;
}
const [name, age, startDate] = await promiseAll(
Promise.resolve('Eamonn'),
Promise.resolve(21),
Promise.resolve(new Date(2012, 9, 1)),
);
@eamonnboyle
eamonnboyle / using-new-promise.all.ts
Last active Mar 23, 2021
Using the new Promise All
View using-new-promise.all.ts
const [name, age, startDate] = await promiseAll([
Promise.resolve('Eamonn'),
Promise.resolve(21),
Promise.resolve(new Date(2012, 9, 1)),
]); // [string, number, Date]
@eamonnboyle
eamonnboyle / tuple-promis-all.ts
Created Mar 23, 2021
Promise All with Variadic Tuple Parameters
View tuple-promis-all.ts
function promiseAll<T extends [...any[]]>(promises: [...T]): RemapPromises<T> {
return Promise.all(promises) as RemapPromises<T>;
}