Just types
A collection of handy Typescript types.

Contents
Installation
Install using npm
npm install -D just-types
Or using yarn
yarn add --dev just-types
List of Types
Decrement
Decrement<0>
Decrement<1>
Decrement<100>
Decrement<101>
Decrement<-10>
Note: Only handles possitive integers between 0 and 100. Returns number
for other integers.
FieldPath
FieldPath<{}>
FieldPath<{a: number; b: null}>
FieldPath<{a: number; b: {c: 1; d: 2}; e: {f: {g: {}}}}
Filter
Filter<[1, 2, true, 3, 'foo'], number>
Filter<[1, 2, true, 3, 'foo'], string>
Filter<[1, 2, true, 3, 'foo'], boolean | string>
Filter<
[{active: true; data: 1}, {active: false; data: 2}, {active: true; data: 3}, {active: false; data: 4}],
{active: true}
>
Flatten
Flatten<[]>
Flatten<[[1]]>
Flatten<[[1], [2]]>
Flatten<[[1], [[2, 3]], [[4]]]>
Flatten<[[1], [[2, 3]], [[4]]], 2>
GetField
GetField<{a: number; b: null}, 'a'>
GetField<{a: number; b: null}, 'b'>
GetField<{a: number; b: {c: 1; d: 2}; e: {f: {g: number}}}, 'e.f.g'>
GetField<{a: number; b: {c: 1; d: 2}; e: {f: {g: number}}}, 'b'>
GetField<{a: number; b: {c: 1; d: 2}; e: {f: {g: number}}}, 'e.f'>
Increment
Increment<0>
Increment<1>
Increment<100>
Increment<101>
Increment<-1>
Note: Only handles possitive integers between 0 and 100. Returns number
for other integers.
InsertAt
InsertAt<'x', 0, ['a', 'b']>
InsertAt<'x', 1, ['a', 'b']>
InsertAt<'x', 2, ['a', 'b']>
InsertAt<'x', 5, ['a', 'b']>
Join
Join<[], '-'>
Join<['a'], '-'>
Join<['a', 'b'], '-'>
MutableTuple
MutableTuple<readonly []>
MutableTuple<readonly ['a']>
MutableTuple<readonly ['a', 'b']>
Permutation
Permutation<[]>
Permutation<['a']>
Permutation<['a', 'b']>
Permutation<['a', 'b', 'c']>
Range
Range<3, 3>
Range<4, 7>
Range<0, 5>
Split
Split<'foo', '-'>
Split<'foo-bar-baz', '-'>
Split<'foo--', '-'>
SubArray
SubArray<['a']>
SubArray<['a', 'b']>
SubArray<['a', 'b', 'c']>
Tail
Tail<[]>
Tail<['a']>
Tail<['a', 'b']>
Tail<['a', 'b', 'c', 'd']>
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
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
.
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. Please make sure your code is consistent with the rest of the codebase and use Prettier and EditorConfig to format your files.
Those are just examples, any issue or pull request is welcome :)
Changelog
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)
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: