Skip to content

Instantly share code, notes, and snippets.

View gvergnaud's full-sized avatar
🚀

Gabriel Vergnaud gvergnaud

🚀
View GitHub Profile
type Range<n, range extends any[] = []> =
range['length'] extends n ? range : Range<n, [...range, range['length']]>
type Drop1<a extends any[]> =
a extends [...infer inits, any] ? inits : []
type Concat<a extends any[], b extends any[]> = [...a, ...b]
type Plus<a, b> =
[...Range<a>, ...Range<b>]['length']
export type TaskPriority = "user-blocking" | "user-visible" | "background";
export type SchedulerPostTaskOptions = {
// This priority is immutable and overrides the
// TaskController's priority.
priority?: TaskPriority;
delay?: number;
signal?: TaskSignal;
};
type TupleToList<xs> = xs extends [infer head, ...(infer tail)]
? [head extends any[] ? head[number] : never, ...TupleToList<tail>]
: [];
function permute<xs extends [] | [any[], ...any[][]]>([
head,
...rest
]: xs): TupleToList<xs>[];
const fs = require("fs");
const tasks = JSON.parse(fs.readFileSync("./input-5req-2render-slow.json"));
// Build histogram of tasks with buckets of 1000ms
const histogram = tasks.reduce((acc, task) => {
const key = Math.floor(task.start / 1000) + "";
return Object.assign(acc, {
[key]: (acc[key] ?? []).concat(task),
});
@gvergnaud
gvergnaud / typescript-type-variance.ts
Last active August 30, 2022 15:52
TypeScript type variance (covariance, contravariance and invariance)
/**
* We call "variance" the direction of assignability between two types
* (in other words, which type is a subtype of the other).
*
* The same type can have a different variance in function of his position
* in a larger type, and this is what makes assignability in TypeScript
* not always straightforwards.
*
* There are 4 kinds of variance:
* - Covariant: if the type `a` is assignable to `a | b`.
/**
* CancelablePromise is a wrapper around a regular Promise which
* can be cancelled by calling `promise.cancel()`.
*
* To do something on cancel, return a callback inside the
* function passed to the CancelablePromise constructor.
*
* Canceled promises will neither be rejected or resolved,
* but will stay unfulfilled.
*
// Helpers
type Expect<T extends true> = T
type Equal<X, Y> =
(<T>() => T extends X ? 1 : 2) extends
(<T>() => T extends Y ? 1 : 2)
? true
: false;
import mapValues from 'lodash/mapValues';
export type Step<Anims> = {
[K in keyof Anims]: { value: number; progress: number };
};
export type Values<Anims> = { [K in keyof Anims]: number };
export interface SpringOptions {
/**
@gvergnaud
gvergnaud / assigning-properties-in-typescript.ts
Last active June 27, 2021 15:59
Assigning properties to an Object type in TypeScript
/**
* # Assigning properties to an object type in TypeScript
*
* When we want to assign some properties on a object type
* we usually use the `&` operator:
*
* type B = A & { someProperty: string }
*
* It seems to work at first sight, but it doesn't behave exactly
* as we would expect when we try to override properties that already
@gvergnaud
gvergnaud / extends-in-typescript.ts
Last active June 27, 2021 15:59
How does the extends keyword work in typescript. Interactive playground: https://bit.ly/2XdCEfn
/**
* # How does `A extends B` work in TypeScript?
*
* If you think about types in terms of sets containing possible values,
* the `string` type is the set of all possible strings,
* the `number` type is the set of all possible numbers,
* the `'hello'` type is a set containing only the string 'hello'
* and the `2` type is a set containing only the number 2.
*
* Then you can think of `A extends B` as asking this question: