The snippet library.
Small, practical examples. Search by task or narrow things down below.
Read the beginning of a text value
TypeScript: Extract or display the first three ASCII characters from a known nonempty string.
TypeScript✓ Tested
Read the final array item with a negative index
TypeScript: at supports negative positions counted from the end.
TypeScript✓ Tested
Read the first collection element
TypeScript: Access index zero only after establishing that the collection is nonempty.
TypeScript✓ Tested
Recover from an expected promise rejection
TypeScript: Return a fallback from catch so later handlers receive an ordinary resolved value.
TypeScript✓ Tested
Remove a Map entry and detect whether it existed
TypeScript: Map.delete returns true only when an entry was present.
TypeScript✓ Tested
Remove duplicate values with a Set
Keep the first occurrence order while deduplicating.
Set✓ Tested
Reorder captured text in a replacement
TypeScript: Use numbered replacement groups to rearrange a known two-part name.
TypeScript✓ Tested
Repeat a fixed number of times
TypeScript: Use a counted loop to print three numbered steps.
TypeScript✓ Tested
Repeat a string a fixed number of times
TypeScript: String.repeat requires a nonnegative finite repetition count.
TypeScript✓ Tested
Repeat while a condition holds
TypeScript: Count down while a positive number remains, updating the loop variable on each iteration.
TypeScript✓ Tested
Replace an array segment in place
TypeScript: splice returns removed items and inserts replacements at the chosen position.
TypeScript✓ Tested
Replace one collection element
TypeScript: Update an existing element at a known valid index.
TypeScript✓ Tested