Skip to content

Instantly share code, notes, and snippets.

View hasparus's full-sized avatar

Piotr Monwid-Olechnowicz hasparus

View GitHub Profile

Epistemic Effort: Low-to-medium effort. It isn't my best work, but I hope it shows that contributing to DefinitelyTyped isn't scary. I might refine it into a meetup talk or a tutorial someday.

18:37

I'm using both theme-ui and chakra-ui in an app, and it just doesn't work. Don't get me wrong, they're both really good libraries, and they're both using @emotion/core to provide the dynamic styling API that IMHO

type Empty = undefined;
const Empty = undefined;
type Pending = 'Pending';
const Pending = 'Pending';
interface CrmUser {
domain: string;
}
@hasparus
hasparus / match.ts
Last active August 15, 2022 01:30
lightweight sum type matching in typescript
interface A { type: 'A', a: 10 };
interface B { type: 'B', b: [11] };
type AB = A | B;
type TypeOf<T extends { type: any }> = T extends { type: infer Type } ? Type : never;
type Cases<T extends { type: any }, R> = {
[P in TypeOf<T>]: (val: Extract<T, { type: P }>) => R
}
import * as ts from "typescript";
import { join } from "path";
import { readFileSync } from "fs";
// const source = `
// import React, { FC } from 'react'
// import { ReactThreeFiber } from './three-types'
// type Three = typeof import('three')
import * as ts from "typescript";
const sourceFile = ts.createSourceFile(
"example.ts",
`
interface Host {
/**
* @mockType {internet.ipv6}
*/
addr: string;
@hasparus
hasparus / as-prop.tsx
Created October 7, 2019 15:08
dynamic React props composition in TypeScript
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 September 30, 2019 18:59
notes / blogpost draft about refinement types

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

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>;