Skip to content

webNeat/just-types

Repository files navigation

Just types

A collection of handy Typescript types.

Version Tests Status MIT

Contents

Installation

Install using npm

npm install -D just-types

Or using yarn

yarn add --dev just-types

Types Reference

Testing Your Types with just-types

just-types allows you to test your own types using Is, Equal and other testing utils. These utils are used internally to test just-types types. For Example, here is the source file of the Split type:

import {Equal, Is} from '../test'

export type Split<
  Text extends string,
  Separator extends string
> = Text extends `${infer First}${Separator}${infer Rest}` ? [First, ...Split<Rest, Separator>] : [Text]

type Tests = [
  Is<Equal<Split<'foo', '-'>, ['foo']>>,
  Is<Equal<Split<'foo-bar-baz', '-'>, ['foo', 'bar', 'baz']>>,
  Is<Equal<Split<'foo--', '-'>, ['foo', '', '']>>
]

As you see, we define the type, then we declare a Tests type (can be named anything) and assign a list of assertions types to it. These types are evaluated in realtime by Typescript, so we have instant feedback if something is wrong.

List of assertions

import {Is, Equal, Not, Extends, StartsWith} from 'just-types/test'

Is<Equal<A, B>> // asserts that types `A` and `B` are the same.
Is<Not<Equal<A, B>>> // asserts that types `A` and `B` are different.
Is<StartsWith<A, B>> // where `A` and `B` extend `string`: asserts that all elements of `A` start with with an element of `B`.
Is<Extends<A, B>> // asserts that type `A` extends type `B`

Contributing

You can contribute to this library in many ways, including:

  • Reporting bugs: Simply open an issue and describe the bug. Please include a code snippet to reproduce the bug, it really helps to solve the problem quickly.

  • Suggesting new types: If you have a common use case that you think worth having its own custom type, open an issue and we will discuss it. Do you already have an implementation for it? great, make a pull request and I will review it.

Those are just examples, any issue or pull request is welcome :)

Changelog

2.0.0-alpha.2 (April 13th 2023)

  • Add types:
    • In common module: Normalize
    • In object module: PartialKeys, PartialValues, RequiredKeys and RequiredValues

2.0.0-alpha.1 (April 11th 2023)

  • Full rewrite of the library.
  • Rewrite some types to use less recursion and be more efficient.
  • Separate types into modules: tuple, string, object, ...
  • Generate docs directly from source code.
  • Drop Parcel and use tsc instead.
  • Rename some types for better naming convention FilterOut => tuple.Exclude, FilterProps => object.ExtractValues, FilterPropsOut => object.ExcludeValues, ...

1.6.0 (Sptember 24th 2022)

  • Add FilterOut, FilterProps, FilterOutProps and Merge.
  • Add assertion type Extends.
  • Improve Tail to support string types.
  • Fix Filter to correctly handle union types.

1.5.0 (September 2nd 2022)

  • Export testing types: Is, Not, Equal, StartsWith

1.4.2 (March 21th 2022)

  • Add repository and homepage to package.json (forgot to add them on 1.4.1 :P).

1.4.1 (March 21th 2022)

1.4.0 (January 29th 2022)

1.3.1 (January 9th 2022)

  • Export missing types

1.3.0 (January 9th 2022)

1.2.0 (January 2nd 2022)

1.1.0 (December 01, 2021)

1.0.0 (November 22, 2021)

The first release containing the 7 types: