Skip to content

Instantly share code, notes, and snippets.

@Mayowa-Ojo
Last active August 24, 2020 14:59
Show Gist options
  • Save Mayowa-Ojo/0fbc94e746febf9171b81f5db6b8088a to your computer and use it in GitHub Desktop.
Save Mayowa-Ojo/0fbc94e746febf9171b81f5db6b8088a to your computer and use it in GitHub Desktop.
JavaScript tips and tricks 🎃
1. Check for object literal type
const isObject = (obj) => {
  return Object.prototype.toString.call(obj) === '[object object]';
}
2. Utility Types (Typescript)
// Some useful Typescript utility types
// Utility types constructs a type subset from a given type

// T = Input type
// Refrence type (T)
interface IUser {
  firstname: string
  lastname: string
  age: number
  income: undefined
}

// Partial<T> - Constructs a type with all properties of T set to optional
const partialT: Partial<IUser> = {} // all properties of IUser are now optional [?]

// Pick<T, Keys> - Constructs a type by picking a set of properties [Keys] from T
const pickT: Pick<IUser, "firstname" | "age"> = {} // PickT now has only firstname and age fields

// Readonly<T> - Constructs a type from T with all properties set to readonly
const readonlyT: Readonly<IUser> = {} // all properties of IUser are now readonly

// NonNullable<T> - Constructs a type by excluding all null and undefined from T
const nonNullableT: NonNullable<IUser> = {} // income field is excluded

// Omit<T, Keys> - Constructs a type by picking all properties of T except [Keys]
const omitT: Omit<IUser, "age" | "income"> = {} // age and income are exluded from IUser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment