@oakfinch/ts-extra
TypeScript icon, indicating that this package has built-in type declarations

1.0.25 • Public • Published

ts-extra

Extended utility types and functions for TypeScript


license TypeScript

Build Tests CodeQL

latest release on npm latest release on github minified + gzipped


github | docs


Installation

Using npm:

npm install --save-dev @oakfinch/ts-extra

Using yarn:

yarn add -D @oakfinch/ts-extra

Usage

See the API documentation for details

Examples

import { Mutable } from '@oakfinch/ts-extra'
const DEFAULTS = [1, 2, 3] as const;

const fn = <T extends number[]>(
  first: number,
  rest: T,
) => first + Math.max(...rest);

// This generates a TS error:
// Argument of type 'readonly [1, 2, 3]' is not assignable to parameter of type 'number[]'.
// The type 'readonly [1, 2, 3]' is 'readonly' and cannot be assigned to the mutable type 'number[]'.
const result = fn(1, DEFAULTS);

// This works fine!
const result = fn(1, (DEFAULTS as Mutable<typeof DEFAULTS>));
import { Value, Tuple } from '@oakfinch/ts-extra'

const ENV_CONFIG = {
  LOCAL: {
    NAME: 'local',
    MINIFY: false,
  },
  DEV: {
    NAME: 'dev',
    MINIFY: false,
  },
  QA: {
    NAME: 'qa',
    MINIFY: true,
  },
  STAGE: {
    NAME: 'stage',
    MINIFY: true,
  },
  PROD: {
    NAME: 'prod',
    MINIFY: true,
  },
} as const;

const MINIFY_ENV_NAMES = (
  Object.values(ENV_CONFIG)
    .filter(({ MINIFY }) => MINIFY)
    .map(({ NAME }) => NAME)
) as Tuple<Extract<Value<typeof ENV_CONFIG>, { MINIFY: true }>['NAME']>;

// const MINIFY_ENV_NAMES: ["qa", "stage", "prod"];

Readme

Keywords

Package Sidebar

Install

npm i @oakfinch/ts-extra

Weekly Downloads

8

Version

1.0.25

License

MIT

Unpacked Size

5.53 kB

Total Files

3

Last publish

Collaborators

  • natevolker