types.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 } | { [Symbol.toStringTag]: string } | number | boolean | null | undefined;
  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;
  31. /**
  32. * Makes the structure of a type more readable by expanding it.
  33. * This can be useful for debugging or for improving the readability of complex types.
  34. */
  35. export type Prettify<T> = {
  36. [K in keyof T]: T[K];
  37. } & {};