1.0.0 • Published 8 months ago

ensure-array-values v1.0.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
8 months ago

ensure-array-values

npm

A TypeScript utility function for ensuring that all values in the provided array match the specified type.

Installation

npm install ensure-array-values

Usage

The ensureArrayValues function helps in creating arrays necessarily containing all types of the provided union. It's particularly useful for creating type-safe lists of object keys.

import ensureArrayValues from 'ensure-array-values';

// A type to create a list of keys for
type Foo = {
  bar: string;
  baz: number;
};

// A complete list of keys
const fooKeys = ensureArrayValues<keyof Foo>()(['bar', 'baz']);

// A complete readonly list of keys
const readonlyFooKeys = ensureArrayValues<keyof Foo>()(['bar', 'baz'] as const);

// An incomplete list of keys causes the type error:
// Argument of type 'string[]' is not assignable to parameter of type 'never'.
const incompleteFooKeys = ensureArrayValues<keyof Foo>()(['bar']);

// An extra key causes the type error:
// Type '"key"' is not assignable to type 'keyof Foo'.
const extraFooKeys = ensureArrayValues<keyof Foo>()(['bar', 'baz', 'key']);
1.0.0

8 months ago