Options
All
  • Public
  • Public/Protected
  • All
Menu

@fp51/opaque-union - v1.0.0

Index

Type aliases

Opaque

Opaque<Name, Variation>: { __OPAQUE_KEY__: Name; __OPAQUE_VARIATION__: Variation; __OPAQUE__: "__OPAQUE__"; value: unknown }

All opaque variables you create with the library will have the type of Opaque<'SomeName', 'SomeVariation>.

remarks

You can't really do anything with it. So, don't try to use them directly! Use the library functions instead.

typeparam Name

The name of the opaque

typeparam Variation

The variation of the opaque

Type parameters

  • Name

  • Variation

Type declaration

  • Readonly __OPAQUE_KEY__: Name

    Where the type of opaque is stored

    internal
  • Readonly __OPAQUE_VARIATION__: Variation

    Where the variation is stored.

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

    Where the private value is stored

    internal

Opaques

Opaques<Names, Variations>: { [ name in Names]: { [ variation in Variations]: Opaque<name, variation> }[Variations] }[Names]

Mapped type to store Opaques by name and variation

Type parameters

  • Names: PossibleNames

  • Variations: PossibleVariations

Tagged

Tagged<Name, Variation, Type>: Type & { _tag: Name; _variation: Variation }

Helper type to add a prop _tag on T Usefull for ISO

typeparam T

The type to decorate

typeparam Key

The name to give the type

typeparam Variations

The variations

Type parameters

  • Name: PossibleNames

  • Variation: PossibleVariations

  • Type

Type

Type<Def, VariationFilter>: Def extends UnionAPIDef<infer Names, infer Variations, any> ? Opaques<Names, Variations & VariationFilter> : never

This will extract all opaque types of a union API. This is more robust to change than creating the union of types yourself.

example
// instead of:

type Text = ReturnType<typeof MessageAPI.of.Text>;
type Image = ReturnType<typeof MessageAPI.of.Image>;
type Video = ReturnType<typeof MessageAPI.of.Video>;

export type Message = Text | Image | Video;

// do:

import * as Union from '@iadvize-oss/opaque-union';

export type Message = Union.Type<typeof MessageAPI>;
typeparam Def

The union api definition

Type parameters

  • Def

  • VariationFilter = string

UnionAPIDef

UnionAPIDef<Names, Variations, Types>: { fold: Fold<{ [ name in Names]: Opaques<name, Variations> }>; is: Is<Names, Variations>; iso: Iso<Opaques<Names, Variations>, TaggedTypes<Names, Variations, Types>[Names]>; lensFromProp: LensFromProp<Names, Variations, Types>; of: Of<Names, Variations, Types>; types: Types } & For<Names, Variations, Types>

The union api

typeparam Types

Collection of private types of the union

typeparam Keys

Keys of Types to map over

Type parameters

  • Names: PossibleNames

  • Variations: PossibleVariations

  • Types: { [ name in Names]: { [ variation in Variations]: any } }

Functions

merge

  • merge<Types1, Types2>(union1: UnionAPIDef<keyof Types1, keyof Types1[keyof Types1], Types1>, union2: UnionAPIDef<keyof Types2, keyof Types2[keyof Types2], Types2>): UnionAPIDef<keyof Types1 | keyof Types2, keyof Types1 & Types2[keyof Types1 | keyof Types2], Types1 & Types2>
  • Merge two union APIs.

    example
    const MessageAPI = Union.of({
    ...
    });

    const CarAPI = Union.of({
    ...
    });

    const MessageAndCarsAPI = Union.merge(MessageAPI, CarAPI);

    Type parameters

    • Types1: { [ key in string | number | symbol]: {} }

      First union private types

    • Types2: { [ key in string | number | symbol]: {} }

      Second union private types

    Parameters

    • union1: UnionAPIDef<keyof Types1, keyof Types1[keyof Types1], Types1>
    • union2: UnionAPIDef<keyof Types2, keyof Types2[keyof Types2], Types2>

    Returns UnionAPIDef<keyof Types1 | keyof Types2, keyof Types1 & Types2[keyof Types1 | keyof Types2], Types1 & Types2>

    The new union api definition

of

  • of<Types>(types: Types): UnionAPIDef<keyof Types, keyof { [ name in string | number | symbol]: { default: Types[name] } }[keyof Types], { [ name in string | number | symbol]: { default: Types[name] } }>
  • Same as ofVariations with a unique "default" variation

    Type parameters

    • Types: { [ key in string | number | symbol]: any }

    Parameters

    • types: Types

    Returns UnionAPIDef<keyof Types, keyof { [ name in string | number | symbol]: { default: Types[name] } }[keyof Types], { [ name in string | number | symbol]: { default: Types[name] } }>

ofVariations

  • ofVariations<Types>(types: Types): UnionAPIDef<keyof Types, keyof Types[keyof Types], Types>
  • Use of to create your opaque entities API.

    remarks

    Ideally, don't share the API outsite your module as this will give module users the possibility to "unopaque" your types and bypass your module API.

    example
    import * as Union from '@iadvize-oss/opaque-union';

    type $Text = string;

    type $Image = {
    source: string;
    description: string;
    }

    const MessageAPI = Union.of({
    Text: Union.type<$Text>(),
    Image: Union.type<$Image>(),
    });

    Type parameters

    • Types: { [ key in string | number | symbol]: {} }

      The collection of private types for the union

    Parameters

    • types: Types

      The map of names and results of {@link "type" | type} function

    Returns UnionAPIDef<keyof Types, keyof Types[keyof Types], Types>

    A union api

omit

  • omit<Types, OmittedKeys>(union: UnionAPIDef<keyof Types, keyof Types[keyof Types], Types>, omittedKeys: OmittedKeys[]): UnionAPIDef<Exclude<keyof Types, OmittedKeys>, keyof Types[Exclude<keyof Types, OmittedKeys>], Omit<Types, OmittedKeys>>
  • Create a new union omiting the given union's types

    example
    const MediaAPI = Union.omit(MessageAPI, ['Text']);
    

    Type parameters

    • Types: { [ key in string | number | symbol]: {} }

      Union initial private types

    • OmittedKeys: string | number | symbol

      Keys of types to omit

    Parameters

    • union: UnionAPIDef<keyof Types, keyof Types[keyof Types], Types>
    • omittedKeys: OmittedKeys[]

    Returns UnionAPIDef<Exclude<keyof Types, OmittedKeys>, keyof Types[Exclude<keyof Types, OmittedKeys>], Omit<Types, OmittedKeys>>

    The new union api definition

omitVariations

  • omitVariations<Types, OmittedVariations>(union: UnionAPIDef<keyof Types, keyof Types[keyof Types], Types>, omittedVariations: OmittedVariations[]): UnionAPIDef<keyof Types, keyof { [ name in string | number | symbol]: Omit<Types[name], OmittedVariations> }[keyof Types], { [ name in string | number | symbol]: Omit<Types[name], OmittedVariations> }>
  • Create a new union omiting the given union's variances

    example
    const MediaAPI = Union.omit(MessageAPI, ['Text']);
    

    Type parameters

    • Types: { [ key in string | number | symbol]: {} }

      Union initial private types

    • OmittedVariations: string | number | symbol

    Parameters

    • union: UnionAPIDef<keyof Types, keyof Types[keyof Types], Types>
    • omittedVariations: OmittedVariations[]

    Returns UnionAPIDef<keyof Types, keyof { [ name in string | number | symbol]: Omit<Types[name], OmittedVariations> }[keyof Types], { [ name in string | number | symbol]: Omit<Types[name], OmittedVariations> }>

    The new union api definition

pick

  • pick<Types, OnlyKeys>(union: UnionAPIDef<keyof Types, keyof Types[keyof Types], Types>, onlyKeys: OnlyKeys[]): UnionAPIDef<OnlyKeys, keyof Types[OnlyKeys], Pick<Types, OnlyKeys>>
  • Create a new union comprising only of the picked union's members

    example
    const MediaAPI = Union.pick(MessageAPI, ['Image', 'Video']);
    

    Type parameters

    • Types: { [ key in string | number | symbol]: {} }

      Union initial private types

    • OnlyKeys: string | number | symbol

      Keys of types to keep

    Parameters

    • union: UnionAPIDef<keyof Types, keyof Types[keyof Types], Types>
    • onlyKeys: OnlyKeys[]

    Returns UnionAPIDef<OnlyKeys, keyof Types[OnlyKeys], Pick<Types, OnlyKeys>>

    The new union api definition

type

  • type<T>(): T
  • To be used only in the context of Union.of to attach private type to the corresponding name in the union.

    example
    import * as Union from '@iadvize-oss/opaque-union';

    const MessageAPI = Union.of({
    Text: Union.type<$Text>(),
    Image: Union.type<$Image>(),
    });

    Type parameters

    • T

      The private type to assign to the corresponding name

    Returns T

    Something. Do not rely on it. Used internaly only.

Generated using TypeDoc