Gists
Share and discover code snippetsextract generic type
Extracts the underlying generic type from a TypeScript type, such as pulling string from Promise<string>.
Ternary operator
The ternary operator provides a shorthand way to write an if-else statement in a single line, evaluating a condition and returning one of two values based on whether it's true or false.
Get nested value safely
This TypeScript snippet safely retrieves a nested value from an object without throwing an error if any intermediate property is undefined.
Strict Type Predicate
A TypeScript utility that creates a type guard function to verify if a value matches a specific type at runtime, ensuring type safety when working with uncertain data.
Array Shuffle Fisher Yates
Shuffles an array in place using the Fisher-Yates algorithm for uniform random distribution.
Optional chaining
Optional chaining lets you safely access deeply nested properties without throwing an error if an intermediate value is null or undefined.
quick sort function
This TypeScript function sorts an array in place using the quicksort algorithm with a recursive divide-and-conquer approach.
Safe JSON Parse
Safely parses a JSON string in TypeScript, returning a fallback value or null when the input is invalid instead of throwing an error.
Deep clone object
This TypeScript snippet creates a deep copy of an object, recursively duplicating all nested properties to ensure no references are shared.
Type-safe event emitter
This TypeScript snippet defines a generic event emitter that enforces correct event names and payload types at compile time.
Type-safe object keys
This snippet ensures you only access valid object keys by enforcing type safety at compile time.
@lorilong437 I once spent hours debugging a production bug that turned out to be a simple typo in a key name, something compile time safety would have caught instantly. It's a lifesaver for large codebases, but watch out for cases where the object keys are dynamic or come from an API response. In those situations, you might need a runtime validator too.
@hughesj Does your team enforce this pattern with an ESLint rule, or do you rely entirely on code review to catch dynamic key access?
type-safe deep clone
This TypeScript snippet performs a deep clone while preserving the original object's type, ensuring full type safety without relying on any.
DeepReadonly type utility
This TypeScript utility recursively makes all properties of an object type read-only, including nested objects and arrays.
deepPartial type utility
Makes all properties of an object type optional, recursively through nested objects, so you can easily work with partial updates or configurations.
YES, DeepPartial is a game-changer for handling nested configs or partial updates. Love how it keeps type safety while allowing flexibility.
@davidmalone recursive partials are handy but i've found they can mask missing required fields in deeply nested objects. ever run into that?
Type-safe deep clone
A deep clone function that preserves TypeScript types, so you can safely copy nested objects without losing type information.
sortArrayByKey
Sorts an array of objects by a given key, making it easy to organize data in ascending or descending order.
Array partition by predicate
This handy TypeScript snippet splits an array into two separate arrays based on a predicate function, returning both the elements that pass and those that fail the test.
async retry wrapper
This handy async retry wrapper automatically retries failed async operations with customizable delays and maximum attempts.
Pick and omit
This TypeScript snippet demonstrates how to use Pick and Omit utility types to create new types by selecting or excluding specific properties from an existing type.
Deep clone utility
This TypeScript deep clone utility recursively copies objects, handling nested structures and edge cases like dates, arrays, and the like.
throttle function
A throttle function ensures a given callback is executed at most once within a specified time interval, preventing rapid repeated calls.
debounce async hook
This handy React hook debounces an async function, delaying its execution until after a specified wait time since the last call, perfect for rate-limiting API requests or handling rapid user input.
Strongly typed useState
"Leverage TypeScript's generics with useState to enforce strict typing on your component's state for safer code."
Array partition filter
Splits an array into two arrays based on a predicate, returning both filtered and remaining elements in one call.
This is one of those utilities I end up writing in every project. I once spent an hour debugging a partition that returned mutated arrays because I forgot to copy the input first. Always spread before you partition.
@jrobertson719 that's a clean way to avoid separate filter calls for the two groups.
Yes, exactly -
user?.profile?.address?.cityis a lifesaver for cleaning up API response handling. One caveat: it can mask bugs if you overuse it instead of validating data at the boundary.