Useful Snippets
Collection of some useful code snippets I use frequently but tend to forget.
January 12, 2025
1. How to infer keys and values as template literals from an object
typescript// Use `as const` to infer the most specific type possible
const SUBJECT = {
CODING: "Coding",
MATH: "Math"
} as const
// Infer the values as template literals
type SubjectValueTypes = typeof SUBJECT[keyof typeof SUBJECT]
// Infer only the keys as template literals
type SubjectKeyTypes = keyof typeof SUBJECT
January 11, 2025
2. How to remove duplicates items from an array
javascript// Using Set (most common and cleanest) const uniqueArr = [...new Set(arr)]
January 8, 2025
3. How to look up git branches
git# Find local branch git branch | grep <word> # Find local + remote branch git branch -a | grep <word> # For case-insensitive search, pass `-i` flag git branch -a | grep -i <word>
January 7, 2025
4. How to check your public IP address
linuxcurl ipinfo.io
January 2, 2025
5. How to disable Swap
linux# 1. Temporary # Check for active swap spaces swapon --show # Turn off all of them sudo swapoff -a # 2. Parmanent sudo vim /etc/fstab # Comment out the line containing swapfile info /swapfile none swap sw 0 0 # freeup disk sudo rm /swapfile
December 29, 2024
6. How to change the default shell
linuxchsh -s $(which zsh)
December 29, 2024
7. How to connect to a WiFi from terminal
linux# List all the local WiFi networks nmcli dev wifi list # Connect to a wifi network with it's SSID & password nmcli dev wifi connect <SSID> password <PASSWORD>