npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ttuple

v0.2.3

Published

Strongly typed Array

Downloads

2

Readme

ttuple

npm

Array wrapper to make methods stricter

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

How to use

Creates tuples

import { toTuple } from "ttuple";

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

// ❌ Without ttuple

const arraySegments = [new Segment()];

arraySegments;
// ^? const arraySegments: Segment[]

// ✅ With ttuple

const tupleSegments = toTuple([new Segment()]);

tupleSegments;
// ^? const tupleSegments: [Segment]

Playground – https://tsplay.dev/NlE5Om 🏝

Iterates over array and saves tuple type

import { map } from "ttuple";

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

// ❌ Without ttuple

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

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

arrayBitrates;
// ^? const arrayBitrates = number[]

// ✅ With ttuple

const tupleBitrates = map((segment) => segment.bitrate, [new Segment()]);

tupleBitrates;
// ^? const tupleBitrates = [number]

Playground – https://tsplay.dev/wRG2EN 🏝

Checks array length and returns array element

import { first, length } from "ttuple";

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

const segments: Segment[] = [];

// ❌ Without ttuple

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

const arrayFirstSegment = segments[0];

arrayFirstSegment;
// ^? const arrayFirstSegment: Segment | undefined

// ✅ With ttuple

if (!length(segments, ">= 1")) {
  throw new Error("Missing segment element");
}

const tupleFirstSegment = first(segments);

tupleFirstSegment;
// ^? const tupleFirstSegment: Segment

Playground – https://tsplay.dev/NV4pxW 🏝

import { length, last } from "ttuple";

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

const segments: Segment[] = [];

// ❌ Without ttuple

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

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

arrayLastSegment;
// ^? const arrayLastSegment: Segment | undefined

// ✅ With ttuple

if (!length(segments, ">= 1")) {
  throw new Error("Missing segment element");
}

const tupleLastSegment = last(segments);

tupleLastSegment;
// ^? const tupleLastSegment: Segment

Playground – https://tsplay.dev/WoaEpN 🏝

API

declare const toTuple: <T extends AnyArray>(array: [...T]) => T;

declare const at: <N extends number>(
  index: N
) => <T extends AnyArray>(array: [...T]) => At<T, `${N}`>;

declare const first: <T extends AnyArray>(array: [...T]) => At<T, "0">;
declare const second: <T extends AnyArray>(array: [...T]) => At<T, "1">;
declare const secondToLast: <T extends AnyArray>(array: [...T]) => At<T, "-2">;
declare const last: <T extends AnyArray>(array: [...T]) => At<T, "-1">;

declare const map: <T extends AnyArray, U>(
  callback: (value: ElementOf<T>, index: number) => U,
  array: [...T]
) => Map<T, U>;

declare function length<
  T extends AnyArray,
  S extends `${number}`,
  R = ToTuple<ElementOf<T>, S>
>(array: T, condition: `>= ${S}`): array is R extends T ? R : never;

Supported methods

  • length (with >= comparator)
  • map
  • at