types.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. //#region UU types
  2. /** Represents any value that is either a string itself or can be converted to one (implicitly and explicitly) because it has a toString() method */
  3. export type Stringifiable = string | { toString(): string };
  4. /**
  5. * A type that offers autocomplete for the passed union but also allows any arbitrary value of the same type to be passed.
  6. * Supports unions of strings, numbers and objects.
  7. */
  8. export type LooseUnion<TUnion extends string | number | object> =
  9. (TUnion) | (
  10. TUnion extends string
  11. ? (string & {})
  12. : (
  13. TUnion extends number
  14. ? (number & {})
  15. : (
  16. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  17. TUnion extends Record<keyof any, unknown>
  18. ? (object & {})
  19. : never
  20. )
  21. )
  22. );
  23. /**
  24. * A type that allows all strings except for empty ones
  25. * @example
  26. * function foo<T extends string>(bar: NonEmptyString<T>) {
  27. * console.log(bar);
  28. * }
  29. */
  30. export type NonEmptyString<TString extends string> = TString extends "" ? never : TString;