Skip to content

Instantly share code, notes, and snippets.

View hasparus's full-sized avatar

Piotr Monwid-Olechnowicz hasparus

View GitHub Profile
type Fluent<T extends object> = {
[K in keyof T]: (() => void) extends T[K]
? T[K] extends (...args: infer Params) => void
? (...args: Params) => Fluent<T>
: T[K]
: T[K];
};
/**
function exhaustive(x: never): never {
throw new Error(`
Unreachable default case.
${x} is not assignable to type never.
`)
}
type Fruit = "apple" | "banana" | "mango";
declare const fruit: Fruit;
@hasparus
hasparus / tic-tac-toe.svelte
Last active August 3, 2019 21:27
I guess this is not really reactive and "svelte-way" but I had fun ¯\_(ツ)_/¯
// https://svelte.dev/repl/65dc3ff369ea423f918e7ac3425b5677?version=3.6.9
<script>
const pipe = (x, ...fs) => fs.reduce((v, f) => f(v), x);
const zip = xs => ys => xs.map((x, i) => [x, ys[i]]);
const head = xs => xs[0];
const filter = predicate => xs => xs.filter(predicate);
const map = predicate => xs => xs.map(predicate);
const tap = proc => x => {
proc(x);
@hasparus
hasparus / typescript-merged-product.ts
Created July 16, 2019 14:29
I'm sure it has no serious use case.
namespace Products {
export type Car = {
name: 'Car';
drive(): void;
}
export type IceCream = {
name: 'IceCream';
flavor: string;
}
const questions = [
{
message: 'Enter your login (email address)...',
name: 'login',
type: 'input',
},
{
message: 'Enter your password...',
name: 'password',
type: 'password',
type Defined<T extends object> = { [K in keyof T]: Exclude<T[K], undefined> };
type Assign<T1, T2> = {
[K in keyof T1 | keyof T2]: K extends keyof T2
? T2[K] extends undefined
? K extends keyof T1
? T1[K]
: never
: T2[K]
: K extends keyof T1
type Constructor<T, Args extends any[]> = {
new (...args: Args): T;
(...args: Args): T;
};
function makeConstructor<T, Args extends any[], P = {}>(
create: (...args: Args) => T,
prototype?: P & ThisType<T & P>
) {
const constructor = function(this: T | undefined, ...args: Args) {