Skip to content

Instantly share code, notes, and snippets.

View fostyfost's full-sized avatar
🤪
All you need is Half-Life 3

Fosty Fost fostyfost

🤪
All you need is Half-Life 3
View GitHub Profile
@fostyfost
fostyfost / index.ts
Created September 18, 2022 21:21
Non Empty Array
type NonEmptyArray<T> = [T, ...T[]]
@fostyfost
fostyfost / index.ts
Created September 18, 2022 15:37
Extract Type
// @see https://stackoverflow.com/questions/60449479/typescript-extract-type-from-object-by-object-path-in-tuple
type ExtractType<O, T extends Array<any>> = {
[K in keyof O]: ((...a: T) => any) extends (a: any, ...args: infer Tail) => any
? Tail['length'] extends 0
? O[K]
: ExtractType<O[K], Tail>
: never
}[T[0]]
@fostyfost
fostyfost / index.ts
Created July 28, 2022 13:29
Writable (remove `readonly`)
type Writeable<T> = {
-readonly [Key in keyof T]: T[Key]
}
type DeepWriteable<T> = {
-readonly [Key in keyof T]: DeepWriteable<T[Key]>
}
@fostyfost
fostyfost / index.ts
Created July 28, 2022 13:26
Union to Type
type UnionToType<
U extends Record<string, unknown>
> = {
[K in (U extends unknown ? keyof U : never)]: U extends unknown
? K extends keyof U ? U[K] : never
: never
}
type A = {
a: string
@fostyfost
fostyfost / is-any.ts
Last active July 27, 2022 12:44
IsAny TS helper
/**
* Checks if type `T` is the `any` type.
* @see https://stackoverflow.com/a/49928360/3406963
* @see https://github.com/dsherret/conditional-type-checks/blob/main/mod.ts
*/
type IsAny<T> = 0 extends 1 & T ? true : false
type Example<S> = IsAny<S> extends false ? 'ok' : never
/**
export const useEffectOnce = (effect: EffectCallback) => {
const destroyFunc = useRef<ReturnType<EffectCallback>>()
const calledOnce = useRef(false)
const renderAfterCalled = useRef(false)
if (calledOnce.current) {
renderAfterCalled.current = true
}
useEffect(() => {
@fostyfost
fostyfost / index.ts
Last active May 1, 2022 18:37
From or to decimal notation
const ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
const MIN_RADIX = 2
const MAX_RADIX = ALPHABET.length
function chekRadix(radix: number): void {
if (radix < MIN_RADIX || radix > MAX_RADIX) {
throw new Error(`radix argument must be between ${MIN_RADIX} and ${MAX_RADIX}`)
}
}
@fostyfost
fostyfost / index.test.ts
Created April 20, 2022 19:10
Magic steps
import { FlightSearchStep, getNextStep, getPrevStep, SeatType } from './index'
const VALID_STEPS_MAP: {
[key: string]: {
[key: string]: {
prev: FlightSearchStep | undefined
next: FlightSearchStep | undefined
}
}
} = {
@fostyfost
fostyfost / approach-1.ts
Created April 19, 2022 19:46
React lazy ref
import type { MutableRefObject } from 'react'
import { useRef } from 'react'
const noop = {}
export function useLazyRef<T>(init: () => T) {
const ref = useRef<T | typeof noop>(noop)
if (ref.current === noop) {
ref.current = init()
@fostyfost
fostyfost / use-props-selector.ts
Created December 7, 2021 08:35
`usePropsSelector` from `redux-views`
import * as React from 'react'
import { useSelector } from 'react-redux'
import type { OutputSelector, OutputParametricSelector } from 'redux-views'
type SelectorWithIdAndUse = (OutputSelector<any, any, any> | OutputParametricSelector<any, any, any, any>) & {
use?: (...args: any[]) => any
idSelector?: (...args: any[]) => any
}
export const usePropsSelector = <T extends SelectorWithIdAndUse = SelectorWithIdAndUse>(