Skip to content

Instantly share code, notes, and snippets.

View jviall's full-sized avatar

James Viall jviall

View GitHub Profile
@jviall
jviall / index.md
Last active January 23, 2023 20:17
Scope -- TypeScript Advanced Techniques -- TS Playground Pres

Advanced TypeScript Techniques

Team Disco

  • Dylan Roberts
  • James Viall

Topic: Generics

Types that take parameters

Typing the useState React hook

@jviall
jviall / useAsyncDebounce.ts
Last active August 4, 2020 16:18
type-ified version of react-table's useAsyncDebounce hook
export function useAsyncDebounce<T extends (args: any[]) => any>(defaultFn: T, defaultWait: number): T {
const debounceRef = React.useRef<{
promise?: Promise<T>,
resolve?: (value: T | PromiseLike<T>) => void,
reject?: (reason?: any) => void,
timeout?: NodeJS.Timeout
}>({})
// the given args could be props, which change, so we want to always use latest definitions.
const getDefaultFn = useGetLatest(defaultFn)