Skip to content

Instantly share code, notes, and snippets.

View karol-majewski's full-sized avatar

Karol Majewski karol-majewski

View GitHub Profile
@karol-majewski
karol-majewski / lodash-mapValues.ts
Created March 18, 2020 18:18
Why it's not always safe to infer `keyof T` as the type of your property (https://github.com/DefinitelyTyped/DefinitelyTyped/pull/43207)
import { mapValues } from 'lodash';
declare module 'lodash' {
interface LoDashStatic {
mapValues<T extends object, TResult>(object: T, callback: (value: T[keyof T], key: keyof T, collection: T) => TResult): { [K in keyof T]: TResult };
}
}
interface FatalErrors {
500: boolean;
@karol-majewski
karol-majewski / enumerate.ts
Last active July 7, 2020 13:05
Enumerating over union members in TypeScript
/**
* These are necessary to construct `enumerate`
*/
type ValueOf<T> = T[keyof T];
type NonEmptyArray<T> = [T, ...T[]]
type MustInclude<T, U extends T[]> =
[T] extends [ValueOf<U>]
? U
@karol-majewski
karol-majewski / gistlog.yml
Last active May 2, 2020 22:53
Protecting invariants in TypeScript
published: true
@karol-majewski
karol-majewski / avoiding-wasted-renders-by-better-api-design.md
Last active February 24, 2020 15:46
How good API design makes your React application performant

A short tale about API design

When I was using why-did-you-render, I noticed something strange. There were hundreds of wasted renders happening on the Collections page of Unsplash. One of the components, called SmallTag, was particularily active.

I used React DevTools to find out which DOM nodes it was rendering.

@karol-majewski
karol-majewski / usePrimitiveState.ts
Created February 23, 2020 01:25
Use primitive values inside the useState hook
import * as React from 'react';
type Primitive =
| boolean
| number
| bigint
| string
| symbol
| null
| undefined;
@karol-majewski
karol-majewski / service-worker.ts
Created February 22, 2020 22:49
Asserting ServiceWorkerGlobalScope inside the Service Worker context
function assertServiceWorkerGlobalScope(
self: typeof globalThis,
): asserts self is typeof globalThis & ServiceWorkerGlobalScope {
if (self.navigator.serviceWorker.controller === null) {
throw new Error('Service worker code can be executed only in a service worker context.');
}
}
assertServiceWorkerGlobalScope(self);
@karol-majewski
karol-majewski / homogenous-array.ts
Last active July 8, 2020 00:13
Homogeneous arrays in TypeScript
/**
* @author https://stackoverflow.com/users/2887218/jcalz
* @see https://stackoverflow.com/a/50375286/10325032
*/
type UnionToIntersection<Union> =
(Union extends any
? (argument: Union) => void
: never
) extends (argument: infer Intersection) => void
? Intersection
const waitFor = (signal$: Rx.Observable<unknown>) => <T>(source$: Rx.Observable<T>) =>
Rx.merge(source$.pipe(Rx.buffer(signal$), Rx.mergeAll()), source$.pipe(Rx.skipUntil(signal$)));
@karol-majewski
karol-majewski / bookmarklet.js
Created January 9, 2020 13:51
Find DOM elements with invalid attributes (like "[object Object]")
javascript: void function (elements, values, callback) {
elements.forEach(element => {
Array.from(element.attributes).forEach(attribute => {
if (values.includes(attribute.value)) {
callback(element)
}
});
});
}(
Array.from(document.querySelectorAll('*')),
declare class Foo<T, U> {
a(): string;
}
interface Foo<T extends any[], U> {
(...args: T): U | Promise<U>;
}
const foo = new Foo<[], void>();
const bar: void | Promise<void> = foo();