Results for “types”
Small, practical examples. Search by task or narrow things down below.
Filter and transform an array
Keep active objects and extract their names.
filter✓ Tested
Give an optional property a default
Handle missing object fields with nullish coalescing.
optional✓ Tested
Handle a result with a discriminated union
Separate success data from an error message.
discriminated union✓ Tested
Mirror input text
Update a result as the user types using a jQuery input event.
jQuery✓ Tested
Narrow a union using typeof
Handle text and numeric values with separate branches.
union✓ Tested
Read a nested optional value
Avoid dereferencing a missing nested object.
optional chaining✓ Tested
Read a property with keyof
Keep the key and returned value tied to an object type.
keyof✓ Tested
Remove duplicate values with a Set
Keep the first occurrence order while deduplicating.
Set✓ Tested
Return the first item with a generic function
Preserve the element type while allowing empty arrays.
generics✓ Tested
Return typed data from an async function
Await a Promise and handle failures explicitly.
async✓ Tested
Safely read a caught error
Check the error type before accessing its message.
unknown✓ Tested
Select fields with the Pick utility type
Expose a smaller view of a larger object type.
Pick✓ Tested