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
@hasparus
hasparus / typescript-merged-product.ts
Created Jul 16, 2019
I'm sure it has no serious use case.
View typescript-merged-product.ts
namespace Products {
export type Car = {
name: 'Car';
drive(): void;
}
export type IceCream = {
name: 'IceCream';
flavor: string;
}
View typescript-challenge--questions-asnwers.ts
const questions = [
{
message: 'Enter your login (email address)...',
name: 'login',
type: 'input',
},
{
message: 'Enter your password...',
name: 'password',
type: 'password',
View assign.ts
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
View constructor-challenge--solved.ts
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) {
View constructor-challenge.ts
type Primitive = string | number;
type Constructor<T, Args extends any[]> = {
new (...args: Args): T;
(...args: Args): T;
};
function makeConstructor<
T,
@hasparus
hasparus / ecs.ts
Last active May 20, 2019
Used UnionToIntersection to get Entity type from _all components_ query
View ecs.ts
// 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;
@hasparus
hasparus / zustandDevtools.ts
Last active May 19, 2019
super hacky devtools for Zustand -- leverage React DevTools to edit your store state
View zustandDevtools.ts
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 / infer-zustand-state.ts
Created May 19, 2019
Infer the type zustand store state
View infer-zustand-state.ts
import createStore from 'zustand';
export const [useStore, _store] = createStore(set => ({
foo: 2,
actions: {
setFoo: (newFoo: number) => {
set({ foo: newFoo });
},
},
}));
You can’t perform that action at this time.