typesome v0.2.4
🤹♀️ typesome
Composable typed values for Typescript type tests.
Installation
Install with npm
:
$ npm install -D typesome
Install with yarn
:
$ yarn add -D typesome
Compatibility
typesome
works in combination with Typescript type testing libraries, such as:
You need to install a type testing library of your choice separately.
Usage
Let's consider the function leftPad
:
// index.d.ts
export function leftPad(
str: string | number,
len: number,
ch?: string | number
): string
Let's write a type test for leftPad
like so:
// index.test-d.ts
import { expectType } from 'tsd'
import { number, string, union } from 'typesome'
import { leftPad } from './index'
// without typesome
expectType<string>(leftPad('foo', 5))
expectType<string>(leftPad('foo', 5, '>'))
expectType<string>(leftPad('foo', 5, 0))
expectType<string>(leftPad(123, 5))
expectType<string>(leftPad(123, 5, '>'))
expectType<string>(leftPad(123, 5, 0))
// with typesome, enumerative style
expectType<string>(leftPad(string, number))
expectType<string>(leftPad(string, number, string))
expectType<string>(leftPad(string, number, number))
expectType<string>(leftPad(number, number))
expectType<string>(leftPad(number, number, string))
expectType<string>(leftPad(number, number, number))
// with typesome, concise style
expectType<string>(leftPad(union(string, number), number))
expectType<string>(
leftPad(union(string, number), number, union(string, number))
)
Composition
typesome
utilities are composable with one another, as well as with null
and undefined
.
// *.test-d.ts
import { array, boolean, number, union } from 'typesome'
array(union(boolean, number, null, undefined))
// (boolean | number | null | undefined)[]
Importing
Module
Import select typesome
utilities in a module:
// *.test-d.ts
import {
array,
// ...
} from 'typesome'
Global
Import all typesome
utilities in the global namespace:
// test-d/tsconfig.json
{
"compilerOptions": {
// ...
"types": ["typesome/all"]
}
}
API
Overview
any
A value of type any
.
Example:
// *.test-d.ts
import { any } from 'typesome'
any // any
array(element)
Creates a value of type Array
.
Example:
// *.test-d.ts
import { array, boolean } from 'typesome'
array(boolean) // boolean[]
bigint
A value of type bigint
.
Example:
// *.test-d.ts
import { bigint } from 'typesome'
bigint // bigint
boolean
A value of type boolean
.
Example:
// *.test-d.ts
import { boolean } from 'typesome'
boolean // boolean
date
A value of type Date
.
Example:
// *.test-d.ts
import { date } from 'typesome'
date // Date
error
A value of type Error
.
Example:
// *.test-d.ts
import { error } from 'typesome'
error // Error
function_(...params)(result)
Creates a typed function.
Example:
// *.test-d.ts
import { boolean, function_, number } from 'typesome'
function_()(boolean)
// () => boolean
function_(number, boolean)(boolean)
// (_: number, __: boolean) => boolean
map(key, value)
Creates a value of type Map
.
Example:
// *.test-d.ts
import { map, number, string } from 'typesome'
map(string, number)
// Map<string, number>
number
A value of type number
.
Example:
// *.test-d.ts
import { number } from 'typesome'
number // number
object
A value of type object
.
Example:
// *.test-d.ts
import { object } from 'typesome'
object // object
promise(value)
Creates a value of type Promise
.
Example:
// *.test-d.ts
import { boolean, promise } from 'typesome'
promise(boolean)
// Promise<boolean>
record(key, value)
Creates a value of type Record
.
Example:
// *.test-d.ts
import { number, record, string } from 'typesome'
record(string, number)
// Record<string, number>
regexp
A value of type RegExp
.
Example:
// *.test-d.ts
import { regexp } from 'typesome'
regexp // RegExp
set(element)
Creates a value of type Set
.
Example:
// *.test-d.ts
import { number, set } from 'typesome'
set(number)
// Set<number>
string
A value of type string
.
Example:
// *.test-d.ts
import { string } from 'typesome'
string // string
symbol
A value of type symbol
.
Example:
// *.test-d.ts
import { symbol } from 'typesome'
symbol // symbol
tuple(...elements)
Creates a typed-tuple value.
Example:
// *.test-d.ts
import { boolean, number, tuple } from 'typesome'
tuple(boolean, number)
// [boolean, number]
union(...elements)
Creates a type-union value.
Example:
// *.test-d.ts
import { boolean, number, union } from 'typesome'
union(boolean, number)
// boolean | number
unknown
A value of type unknown
.
Example:
// *.test-d.ts
import { unknown } from 'typesome'
unknown // unknown
void_
A value of type void
.
Example:
// *.test-d.ts
import { void_ } from 'typesome'
void_ // void
License
MIT