1.0.25 • Published 2 years ago
@oakfinch/ts-extra v1.0.25
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"];
1.0.19
2 years ago
1.0.22
2 years ago
1.0.21
2 years ago
1.0.20
2 years ago
1.0.25
2 years ago
1.0.24
2 years ago
1.0.23
2 years ago
1.0.17
3 years ago
1.0.16
3 years ago
1.0.15
3 years ago
1.0.14
3 years ago
1.0.13
3 years ago
1.0.12
3 years ago
1.0.11
3 years ago
1.0.8
3 years ago
1.0.7
3 years ago
1.0.6
3 years ago
1.0.5
3 years ago
1.0.4
3 years ago
1.0.3
3 years ago
1.0.2
3 years ago
1.0.1
3 years ago
1.0.0
3 years ago