Skip to content

Instantly share code, notes, and snippets.

View hasparus's full-sized avatar

Piotr Monwid-Olechnowicz hasparus

View GitHub Profile
function hasKey<T extends object, K extends string>(x: T, key: K): x is T & { [_ in K]: unknown } {
return key in x;
}
type TypeOfs = {
string: string;
number: number;
object: object;
};
import * as t from "io-ts";
import { record } from "fp-ts/lib/Record";
const handler = <I, A, R>(decoder: t.Decoder<I, A>, f: (_: A) => R) => ({
decoder,
f,
});
type Handler = ReturnType<typeof handler>;
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',