types.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * @module lib/types
  3. * This module contains various TS types - [see the documentation for more info](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#utility-types)
  4. */
  5. //#region UU types
  6. /** Represents any value that is either a string itself or can be converted to one (implicitly and explicitly) because it has a toString() method */
  7. export type Stringifiable = string | { toString(): string } | { [Symbol.toStringTag]: string } | number | boolean | null | undefined;
  8. /**
  9. * A type that offers autocomplete for the passed union but also allows any arbitrary value of the same type to be passed.
  10. * Supports unions of strings, numbers and objects.
  11. */
  12. export type LooseUnion<TUnion extends string | number | object> =
  13. (TUnion) | (
  14. TUnion extends string
  15. ? (string & {})
  16. : (
  17. TUnion extends number
  18. ? (number & {})
  19. : (
  20. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  21. TUnion extends Record<keyof any, unknown>
  22. ? (object & {})
  23. : never
  24. )
  25. )
  26. );
  27. /**
  28. * A type that allows all strings except for empty ones
  29. * @example
  30. * function foo<T extends string>(bar: NonEmptyString<T>) {
  31. * console.log(bar);
  32. * }
  33. */
  34. export type NonEmptyString<TString extends string> = TString extends "" ? never : TString;
  35. /**
  36. * Makes the structure of a type more readable by expanding it.
  37. * This can be useful for debugging or for improving the readability of complex types.
  38. */
  39. export type Prettify<T> = {
  40. [K in keyof T]: T[K];
  41. } & {};
  42. /** Any value that is list-like, i.e. has a numeric length, count or size property */
  43. export type ListWithLength = unknown[] | NodeList | { length: number } | { count: number } | { size: number };