Skip to content

Instantly share code, notes, and snippets.

@devesh-anand
Last active February 8, 2024 08:23
Show Gist options
  • Save devesh-anand/bcb3cf83244f99957d232492c522c1e2 to your computer and use it in GitHub Desktop.
Save devesh-anand/bcb3cf83244f99957d232492c522c1e2 to your computer and use it in GitHub Desktop.
Some typescript coding practices
  • Never use any.
  • In case there's no alternative, use unknown (more about unknown).
  • Use ReadOnly arrays to ensure there's no scope for mutation (unless mutation is needed).
const numbers: ReadonlyArray<number> = [1, 2, 3];
  • Array generics over typecasting.
const numbers: Array<number>;
//instead of const numbers: any[];
  • Use interfaces over types for better extensibility.
  • TS util types:
interface Person {
  name: string;
  age: number;
  address: string;
}

type PartialPerson = Partial<Person>; // Makes all properties optional
type PersonName = Pick<Person, 'name'>; // Extracts a subset of properties
type PersonWithoutAge = Omit<Person, 'age'>; // Removes a property

//For enums, we use Exclude
enum fruits {
  "apple",
  "orange",
  "marshmellow".
  "lemon"
}

type vitaminCFruits = Exclude<fruits, "apple">;
  • Nullish coalescing operator over ternary operator at times.
const url = props.baseurl?props.baseurl:"";
//instead:
const url = props.baseurl ?? "";
  • Always try to have function return type set:
async function sum(a: number, b: number): Promise<number> {...}
  • Remember, Promise.all is all-or-nothing. If any promise fails, the whole thing fails. If you want all Promises to be proccessed (both resolved and failed), use Promise.allSettled().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment