1
0

types.ts 1.0 KB

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