ts-optchain

  • Version 0.1.8
  • Published
  • 19.2 kB
  • No dependencies
  • MIT license

Install

npm i ts-optchain
yarn add ts-optchain
pnpm add ts-optchain

Overview

Optional Chaining for TypeScript

Index

Functions

function oc

oc: <T>(data?: T) => TSOCType<T>;
  • Optional chaining with default values. To inspect a property value in a tree-like structure, invoke it as a function, optionally passing a default value.

    Example 1

    // Given: const x = oc({ a: 'hello', b: { d: 'world' }, c: [-100, 200, -300], });

    // Then: x.a() === 'hello' x.b.d() === 'world' x.c[0]() === -100 x.c[100]() === undefined x.c[100](1234) === 1234 x.c.map((e) => e()) === [-100, 200, -300] x.d.e() === undefined x.d.e('optional default value') === 'optional default value' (x as any).y.z.a.b.c.d.e.f.g.h.i.j.k() === undefined

Interfaces

interface TSOCAny

interface TSOCAny extends TSOCDataAccessor<any> {}
  • Data accessor interface to dereference the value of an any type. TSOCDataAccessor

index signature

[K: string]: TSOCAny;

    interface TSOCArrayWrapper

    interface TSOCArrayWrapper<T> {}
    • TSOCArrayWrapper gives TypeScript visibility into the TSOCType values of an array without exposing Array methods (it is problematic to attempt to invoke methods during the course of an optional chain traversal).

    property length

    length: TSOCType<number>;

      index signature

      [K: number]: TSOCType<T>;

        interface TSOCDataAccessor

        interface TSOCDataAccessor<T> {}
        • Data accessor interface to dereference the value of the TSOCType.

        call signature

        (noDefaultValue?: undefined): Defined<T> | undefined;
        • Data accessor without a default value. If no data exists, undefined is returned.

        call signature

        (defaultValue: NonNullable<T>): NonNullable<T>;
        • Data accessor with default value.

        call signature

        (nullDefaultValue: T extends null ? null : never): Defined<T>;

          Type Aliases

          type Defined

          type Defined<T> = Exclude<T, undefined>;
          • A generic type that cannot be undefined.

          type TSOCDataWrapper

          type TSOCDataWrapper<T> = 0 extends 1 & T // Is T any? (https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360)
          ? TSOCAny
          : T extends any[] // Is T array-like?
          ? TSOCArrayWrapper<T[number]>
          : T extends object // Is T object-like?
          ? TSOCObjectWrapper<T>
          : TSOCDataAccessor<T>;
          • TSOCDataWrapper selects between TSOCArrayWrapper, TSOCObjectWrapper, and TSOCDataAccessor to wrap Arrays, Objects and all other types respectively.

          type TSOCObjectWrapper

          type TSOCObjectWrapper<T> = { [K in keyof T]-?: TSOCType<T[K]> };
          • TSOCObjectWrapper gives TypeScript visibility into the properties of an TSOCType object at compile-time.

          type TSOCType

          type TSOCType<T> = TSOCDataAccessor<T> & TSOCDataWrapper<NonNullable<T>>;
          • An object that supports optional chaining

          Package Files (1)

          Dependencies (0)

          No dependencies.

          Dev Dependencies (6)

          Peer Dependencies (0)

          No peer dependencies.

          Badge

          To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

          You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/ts-optchain.

          • Markdown
            [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/ts-optchain)
          • HTML
            <a href="https://www.jsdocs.io/package/ts-optchain"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>