Options
All
  • Public
  • Public/Protected
  • All
Menu

@fp51/opaque-type - v1.0.0

Index

Type aliases

Functions

Type aliases

Opaque<Key>: { __OPAQUE_KEY__: Key; __OPAQUE__: "__OPAQUE__"; value: unknown }

The opaque type, of type Key, storing something private in value

Type parameters

  • Key: string

Type declaration

  • Readonly __OPAQUE_KEY__: Key

    Where the type of opaque is stored

    internal
  • Readonly __OPAQUE__: "__OPAQUE__"
    internal
  • Readonly value: unknown

    Where the private value is stored

    internal
OpaqueApi<Key, $PrivateValue>: { fromOpaque: any; isOpaque: any; toOpaque: any }

Type parameters

  • Key: string

  • $PrivateValue

Type declaration

  • fromOpaque:function
    • fromOpaque(opaque: Opaque<Key>): $PrivateValue
    • Extract the private value from the opaque

      Should usually not be shared outside of where the private type is used

      example
      type $Car = { brand: string };

      const { fromOpaque } = createOpaqueAPI<'CAR', $Car>('CAR');

      type Car = ReturnType<typeof toOpaque>

      function brand(car: Car) {
      const $car = fromOpaque(car);

      return $car.brand;
      }

      Parameters

      • opaque: Opaque<Key>

        The opaque from which to extract the private value

      Returns $PrivateValue

      The private value hidden in the opaque

  • isOpaque:function
    • isOpaque(thing: any): thing is Opaque<Key>
    • Test if something is an opaque

      example
      type $Car = { brand: string };
      const CarAPI = createOpaqueAPI<'CAR', $Car>('CAR');
      type Car = ReturnType<typeof toOpaque>

      type $Bike = { brand: string };
      const BikeAPI = createOpaqueAPI<'CAR', $Bike>('CAR');
      type Bike = ReturnType<typeof toOpaque>

      const myCar: Car = CarAPI.toOpaque({ brand: 'Peugeot' });
      const myBike: Bike = BikeAPI.toOpaque({ brand: 'Peugeot' });

      CarAPI.isOpaque(myCar); // true
      CarAPI.isOpaque(myBike); // false

      Parameters

      • thing: any

        Anything

      Returns thing is Opaque<Key>

      true if thing is an opaque with the corresponding key

  • toOpaque:function
    • toOpaque(value: $PrivateValue): Opaque<Key>
    • Encapsulate a private, non-opaque, value in the opaque

      example
      type $Car = { brand: string };

      const { toOpaque } = createOpaqueAPI<'CAR', $Car>('CAR');

      type Car = ReturnType<typeof toOpaque>

      function createCar(brand: string): Car {
      return toOpaque({ brand });
      }

      Parameters

      • value: $PrivateValue

        The value to hide in the opaque

      Returns Opaque<Key>

      The opaque

Functions

  • createOpaqueAPI<Key, $PrivateValue>(key: Key): OpaqueApi<Key, $PrivateValue>
  • Create the api for one specific opaque

    example
    type $Car = { brand: string };

    const {
    toOpaque,
    fromOpaque,
    isOpaque,
    } = createOpaqueAPI<'CAR', $Car>('CAR');

    type Car = ReturnType<typeof toOpaque>

    Type parameters

    • Key: string

      The key used to distinguish this opaque type from other

    • $PrivateValue

      The private type hidden in the opaque

    Parameters

    • key: Key

      The key used to distinguish this opaque type from other

    Returns OpaqueApi<Key, $PrivateValue>

    API for Opaque

Generated using TypeDoc