0.1.1 • Published 2 years ago

strongly-typed-array v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

strongly-typed-array

npm

Array wrapper to make methods stricter

It's recommended to enable noPropertyAccessFromIndexSignature option to see the difference

How to use

  1. Creates tuples
import sta from 'strict-typed-array';

class Segment {
  public bitrate: number = -1;
}

// ❌ Without strict-typed-array

const segments = [new Segment()];

segments
// ^? const segments: Segment[]

// ✅ With strict-typed-array

const segments = sta([new Segment()).toArray();

segments
// ^? const segments: [Segment]
  1. Iterates over array and saves tuple type
import sta from 'strict-typed-array';

class Segment {
  public bitrate: number = -1;
}

// ❌ Without strict-typed-array

const segments: [Segment] = [new Segment()];

const bitrates = segments.map(segment => segment.bitrate);

bitrates
// ^? const bitrates = number[]

// ✅ With strict-typed-array

const bitrates = sta([new Segment()])
    .map((segment) => segment.bitrate)
    .toArray();

bitrates
// ^? const bitrates = [number]
  1. Checks array length and returns array element
import sta from 'strict-typed-array';

class Segment {
  public bitrate: number = -1;
}

const segments: Segment[] = [];

// ❌ Without strict-typed-array

if (segments.length < 1) {
    throw new Error('Missing segment element');
}

const firstSegment = segments[0];

firstSegment
// ^? const firstSegment: Segment | undefined

// ✅ With strict-typed-array

const firstSegment = sta(segments)
    .length('>= 1', () => new Error('Missing segment element'))
    .at(0);

firstSegment
// ^? const firstSegment: Segment
import sta from 'strict-typed-array';

class Segment {
  public bitrate: number = -1;
}

const segments: Segment[] = [];

// ❌ Without strict-typed-array

if (segments.length < 1) {
    throw new Error('Missing segment element');
}

const lastSegment = segments[segments.length - 1];

lastSegment
// ^? const lastSegment: Segment | undefined

// ✅ With strict-typed-array

const lastSegment = sta(segments)
    .length('>= 1', () => new Error('Missing segment element'))
    .at(-1);

lastSegment
// ^? const lastSegment: Segment

API

class StronglyTypedArray<T extends AnyArray> {
  at<N extends number, S extends string = `${N}`>(index: N): Get<T, S>;
  
  length<S extends LengthComparison>(condition: S, orThrows: () => Error): StronglyTypedArray<ToTuple<ElementOf<T>, ExtractLength<S>>>;

  map<U>(
    callback: (value: ElementOf<T>, index: number) => U
  ): StronglyTypedArray<Map<T, U>>;

  toArray(): T;
}

// `sta` is short for strongly typed array
export const sta = <T extends AnyArray>(
  items: [...T]
): StronglyTypedArray<[...T]> => new StronglyTypedArray(items);

Supported methods

  • length (with >= comparator)
  • map