Skip to content

Instantly share code, notes, and snippets.

Piotr Monwid-Olechnowicz hasparus

Block or report user

Report or block hasparus

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View intermock--mock-type-tag.ts
import * as ts from "typescript";
const sourceFile = ts.createSourceFile(
"example.ts",
`
interface Host {
/**
* @mockType {internet.ipv6}
*/
addr: string;
@hasparus
hasparus / as-prop.tsx
Created Oct 7, 2019
dynamic React props composition in TypeScript
View as-prop.tsx
type HeaderLinkProps<As extends ElementType> = { as?: As } & Omit<
ComponentPropsWithoutRef<typeof s.a> & ComponentPropsWithoutRef<As>,
"as"
>;
const HeaderLink = <As extends ElementType = "a">(
props: HeaderLinkProps<As>
) => (
<s.a
sx={{
@hasparus
hasparus / refinement-types.md
Last active Sep 30, 2019
notes / blogpost draft about refinement types
View refinement-types.md

Refinement Types

What are they?

TLDR: type + predicate = refinement type

We have a type called T and a predicate on T ( (x: T) => boolean ) called P.
We can say that P refines T into a type PT, where PT is a subtype of T containing all values x of type T for which P(x) === true.

Sources

View poor-validation.ts
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;
};
View io-ts-decoder-name-api-docs.ts
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>;
View fluent.ts
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];
};
/**
View exhaustive-switch.ts
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 Aug 3, 2019
I guess this is not really reactive and "svelte-way" but I had fun ¯\_(ツ)_/¯
View tic-tac-toe.svelte
// 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);
You can’t perform that action at this time.