0.1.0 • Published 4 years ago

@saucier/helpers v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

@saucier/helpers

Some simple helpers for handling common TypeScript tasks

Array Helpers

notNull

import {notNull} from '@saucier/helpers';

const arr: Array<string | null | undefined> = [null, 'abc'];
const newArr: Array<string> = arr.filter(notNull);

isString

import {isString} from '@saucier/helpers';

const arr: Array<string | number | undefined> = [null, 123, 'abc'];
const newArr: Array<string> = arr.filter(isString);

isNumber

import {isNumber} from '@saucier/helpers';

const arr: Array<string | number | undefined> = [null, 123, 'abc'];
const newArr: Array<number> = arr.filter(isNumber);

arrayWrap

import {arrayWrap} from '@saucier/helpers';

const singular: Array<string> = arrayWrap('abc');
const array: Array<string> = arrayWrap(['abc']);

Helper Types

Many

import {Many} from '@saucier/helpers';

type ManyStrings = Many<string>;
// Equivalent to string | string[]

ElementOf

import {ElementOf} from '@saucier/helpers';

const tuple = ['a', 'b', Symbol('c'), 4];
type ElementOfTuple = ElementOf<typeof tuple>; 
// Equivalent to string | number | { c: string; }

Awaited

import { Awaited } from "./types";

type SomePromise = Promise<{result: string[]}>;
type SomePromiseResult = Awaited<SomePromise>;
// Equivalent to { result: string[]; }

MakeRequired

import { MakeRequired } from "./types";
interface SomeType {
  a?: string;
  b?: number;
}
type SomePartialType = MakeRequired<SomeType, 'a'>;
// Equivalent to { a: string, b?: number}

MakeOptional

import { MakeOptional } from "./types";
interface SomeType {
  a: string;
  b: number;
}
type SomePartialType = MakeOptional<SomeType, 'a'>;
// Equivalent to { a?: string, b: number}