Skip to content

Instantly share code, notes, and snippets.

@robkuz
robkuz / lookup.ts
Last active November 19, 2021 19:19
Building a typesafe Lookup with Constant Objects
//I find typescripts enums a bit lacking.
//This is a way to havevery strongly typed bidirectional lookup table
//first we start with a helper type to reverse a record with unique literal value types as property types
type UniqueReverser<T extends Record<keyof T, PropertyKey>> = unknown extends {
[K in keyof T]-?: T[K] extends Omit<T, K>[Exclude<keyof T, K>] ? unknown : never
}[keyof T] ? never : { [P in keyof T as T[P]]: P }
//this will allow to convert an object in this shape and type
const SOURCE = {
@joakimriedel
joakimriedel / !typed mappers (mapGetters, mapActions, mapMutations, mapState) for vuex 4 and vue 3.md
Last active May 9, 2024 14:40
Strictly typed mappers for vuex 4 and vue 3 using Typescript 4.4+

Vuex 4 is a great companion to Vue 3, but Vuex is falling behind on Typescript support. While waiting for better typing support in Vuex 5 or using Pinia, this wrapper might help.

Using this wrapper will correctly infer arguments and return types on your state, actions, getters and mutations.

It works both within your store(s) and any Vue components where you use the mapActions, mapState, mapGetters or mapMutations helpers from Vuex.

No more any will help you find many errors at compile-time!

@karol-majewski
karol-majewski / withHover.ts
Created November 4, 2020 20:24
The simplest possible higher-order component in React
export function withHover<T>(WrappedComponent: React.ComponentType<T & Hoverable>): React.ComponentClass<T> {
return class extends React.Component<T> {
state = {
hover: false,
};
onMouseEnter: React.MouseEventHandler = () => {
this.setState({ hover: true });
};
@zakarumych
zakarumych / hibitset.rs
Created August 10, 2018 11:18
hierarchical bitset
#[derive(Debug, PartialEq, Eq)]
enum State {
/// There are `0`s only.
Empty,
/// There may be `1`s and `0`s.
Partial,
/// There are `1`s only.
@Wesleysaur
Wesleysaur / bst.rs
Last active January 31, 2022 05:44
A very simple binary search tree in rust
use std::io;
#[derive(Debug)]
struct BSTNode {
value: i32,
left: Option<Box<BSTNode>>,
right: Option<Box<BSTNode>>
}
impl BSTNode {
@CristinaSolana
CristinaSolana / gist:1885435
Created February 22, 2012 14:56
Keeping a fork up to date

1. Clone your fork:

git clone [email protected]:YOUR-USERNAME/YOUR-FORKED-REPO.git

2. Add remote from original repository in your forked repository:

cd into/cloned/fork-repo
git remote add upstream git://github.com/ORIGINAL-DEV-USERNAME/REPO-YOU-FORKED-FROM.git
git fetch upstream