types.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //#region shims
  2. export type TrustedTypesPolicy = {
  3. createHTML?: (dirty: string) => string;
  4. };
  5. declare global {
  6. interface Window {
  7. // poly-shim for the new Trusted Types API
  8. trustedTypes: {
  9. createPolicy(name: string, policy: TrustedTypesPolicy): TrustedTypesPolicy;
  10. };
  11. }
  12. }
  13. //#region UU types
  14. /** Represents any value that is either a string itself or can be converted to one (implicitly and explicitly) because it has a toString() method */
  15. export type Stringifiable = string | { toString(): string };
  16. /**
  17. * A type that offers autocomplete for the passed union but also allows any arbitrary value of the same type to be passed.
  18. * Supports unions of strings, numbers and objects.
  19. */
  20. export type LooseUnion<TUnion extends string | number | object> =
  21. (TUnion) | (
  22. TUnion extends string
  23. ? (string & {})
  24. : (
  25. TUnion extends number
  26. ? (number & {})
  27. : (
  28. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  29. TUnion extends Record<keyof any, unknown>
  30. ? (object & {})
  31. : never
  32. )
  33. )
  34. );
  35. /**
  36. * A type that allows all strings except for empty ones
  37. * @example
  38. * function foo<T extends string>(bar: NonEmptyString<T>) {
  39. * console.log(bar);
  40. * }
  41. */
  42. export type NonEmptyString<TString extends string> = TString extends "" ? never : TString;