0.2.4 • Published 3 years ago

typesome v0.2.4

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

🤹‍♀️ 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

⬆️ API


array(element)

Creates a value of type Array.

Example:

// *.test-d.ts

import { array, boolean } from 'typesome'

array(boolean) // boolean[]

⬆️ API


bigint

A value of type bigint.

Example:

// *.test-d.ts

import { bigint } from 'typesome'

bigint // bigint

⬆️ API


boolean

A value of type boolean.

Example:

// *.test-d.ts

import { boolean } from 'typesome'

boolean // boolean

⬆️ API


date

A value of type Date.

Example:

// *.test-d.ts

import { date } from 'typesome'

date // Date

⬆️ API


error

A value of type Error.

Example:

// *.test-d.ts

import { error } from 'typesome'

error // Error

⬆️ API


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

⬆️ API


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>

⬆️ API


number

A value of type number.

Example:

// *.test-d.ts

import { number } from 'typesome'

number // number

⬆️ API


object

A value of type object.

Example:

// *.test-d.ts

import { object } from 'typesome'

object // object

⬆️ API


promise(value)

Creates a value of type Promise.

Example:

// *.test-d.ts

import { boolean, promise } from 'typesome'

promise(boolean)
// Promise<boolean>

⬆️ API


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>

⬆️ API


regexp

A value of type RegExp.

Example:

// *.test-d.ts

import { regexp } from 'typesome'

regexp // RegExp

⬆️ API


set(element)

Creates a value of type Set.

Example:

// *.test-d.ts

import { number, set } from 'typesome'

set(number)
// Set<number>

⬆️ API


string

A value of type string.

Example:

// *.test-d.ts

import { string } from 'typesome'

string // string

⬆️ API


symbol

A value of type symbol.

Example:

// *.test-d.ts

import { symbol } from 'typesome'

symbol // symbol

⬆️ API


tuple(...elements)

Creates a typed-tuple value.

Example:

// *.test-d.ts

import { boolean, number, tuple } from 'typesome'

tuple(boolean, number)
// [boolean, number]

⬆️ API


union(...elements)

Creates a type-union value.

Example:

// *.test-d.ts

import { boolean, number, union } from 'typesome'

union(boolean, number)
// boolean | number

⬆️ API


unknown

A value of type unknown.

Example:

// *.test-d.ts

import { unknown } from 'typesome'

unknown // unknown

⬆️ API


void_

A value of type void.

Example:

// *.test-d.ts

import { void_ } from 'typesome'

void_ // void

⬆️ API


License

MIT

0.2.4

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.1

3 years ago

0.0.0

3 years ago