@oakfinch/ts-extra
v1.0.25
Published
Extended utility types and functions for TypeScript
Downloads
36
Readme
ts-extra
Extended utility types and functions for TypeScript
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"];