Skip to content

Instantly share code, notes, and snippets.

View hasparus's full-sized avatar

Piotr Monwid-Olechnowicz hasparus

View GitHub Profile
@hasparus
hasparus / merging.ts
Last active March 8, 2019 10:08
TypeScript Playground -- Merging types
// Paste this to typescriptlang.org/play or click the link the comments
type Expected = {
a: 1 | 2
b?: 1 | 2
c?: 1
d?: 1 | 2
e: 2
f?: 2
}
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta name="theme-color" content="#ffffff" />
<meta
name="description"
@hasparus
hasparus / meow.d.ts
Created April 10, 2019 20:20
Improved type inference in @types meow
// Type definitions for meow 5.x
// Project: https://github.com/sindresorhus/meow
// Definitions by: KnisterPeter <https://github.com/KnisterPeter>
// Lindsey Smith <https://github.com/praxxis>
// Jason Dreyzehner <https://github.com/bitjson>
// TODO?? hasparus <https://github.com/hasparus>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.4
import * as buildOptions from "minimist-options";
@hasparus
hasparus / sqlTag.ts
Last active May 4, 2019 11:43
sql tagged template literals
import * as assert from 'assert';
// @types/mysql QueryOptions
type QueryOptions = {
sql: string;
values: unknown[];
};
const brand = Symbol('sql-brand');
@hasparus
hasparus / typedPubsubSubscribe.ts
Last active May 9, 2019 17:06
Typed PubSub subscribe
import { PubSub } from 'graphql-subscriptions';
type PubSubAction = [typeof MESSAGE_ADDED, MessageAddedPayload] | ['foo', 121];
type UnionToIntersection<U> =
(U extends any ? (k: U) => void : never) extends
((k: infer I) => void) ? I : never;
type PairsToRecord<Pair extends [string, any]> =
Pair extends [infer T1, infer T2]
@hasparus
hasparus / zustandDevtools.ts
Last active May 19, 2019 14:41
super hacky devtools for Zustand -- leverage React DevTools to edit your store state
import React, { useEffect, useRef } from 'react';
import { render } from 'react-dom';
type ZustandStore = ReturnType<typeof import('zustand').default>[1];
/**
* @example
* const [useStore, store] = createStore(set => { /* ... */ });
* if (process.env.NODE_ENV === 'development') {
* zustandDevtools.mountStoreSinkDevtool('MyStore', store);
@hasparus
hasparus / ecs.ts
Last active May 20, 2019 08:03
Used UnionToIntersection to get Entity type from _all components_ query
// components.ts
type Component = Components[keyof Components]; // Health | Transform
interface Components { /* add new components to this interface in separate files */ }
// health.ts
type Health = number & { __brand: 'Health' };
interface Components {
health: Health;
type Primitive = string | number;
type Constructor<T, Args extends any[]> = {
new (...args: Args): T;
(...args: Args): T;
};
function makeConstructor<
T,
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) {
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