@bedard/types

The goal of this package is to act as an extension to Typescript's built-in utility types. There is currently no runtime, but that may change if type guards are added. I don't anticipate breaking changes, but with that said this is mainly for personal use, so upgrade with caution.
Installation
The recommended way to install this package is through NPM.
npm install @bedard/types
Utility types
Difference<A, B>
Equal<A, B>
Expect<T>
Intersection<A, B>
Join<Parts, Delimeter>
NotEqual<A, B>
Opaque<T, Token>
Pop<T>
Shift<T>
Split<Source, Delimeter>
SymmetricDifference<A, B>
ValueOf<T>
Without<A, B>
XOR<A, B>
Difference<A, B>
Elements of A
that are not elements of B
. For unions, this is the same as the Exclude
utility.
import { Difference } from '@bedard/types'
type Left = Difference<{ a: any, b: any }, { b: any, c: any }>
Equal<A, B>
Types true
if A
and B
are equal. This is mainly used with Expect
to verify that types are working as expected. See NotEqual
for the inverse of this type.
import { Expect, Equal } from '@bedard/types'
type Test = Expect<Equal<number, number>>
Expect<T>
Verify that T
is true
. This allows for assertions to be made using the type system. See Equal
and NotEqual
for more usage examples.
import { Expect } from '@bedard/types'
type Test = Expect<true>
Intersection<A, B>
The intersection of A
and B
, giving preference to A
.
import { Intersection } from '@bedard/types'
type Shared = Intersection<{ a: any, b: number }, { c: string, d: any }>
Join<Parts, Delimeter>
Join Parts
by Delimeter
. This type is the opposite of Split
.
import { Join } from '@bedard/types'
type Str = Join<['a', 'b', 'c']>
type Parts = Join<['a', 'b', 'c'], '.'>
NotEqual<A, B>
Types true
if A
does not equal B
. This type is mainly used with Expect
to verify that types are working as expected. See Equal
for the inverse of this type.
import { Expect, NotEqual } from '@bedard/types'
type Test = Expect<NotEqual<number, string>>
Opaque<T, Token>
Opaque type T
with an optional Token
. For more on opaque types, this article is a great place to start.
import { Opaque } from '@bedard/types'
type USD = Opaque<number, 'usd'>
const dollars = 5 as USD
Pop<T>
Remove the last element of T
.
import { Pop } from '@bedard/types'
type Items = Pop<['foo', 'bar', 'baz']>
Shift<T>
Remove the first element of T
.
import { Shift } from '@bedard/types'
type Items = Shift<['foo', 'bar', 'baz']>
Split<Source, Delimeter>
Split Source
by Delimeter
. This type is the opposite of Join
.
import { Split } from '@bedard/types'
type Characters = Split<'abc'>
type Parts = Split<'a.b.c', '.'>
SymmetricDifference<A, B>
The symmetric difference of A
and B
.
import { SymmetricDifference } from '@bedard/types'
type OuterSet = SymmetricDifference<'a' | 'b', 'b' | 'c'>
type OuterObj= SymmetricDifference<{ a: any, b: any }, { b: any, c: any }>
ValueOf<T>
Generate a union from the values of T
. This can be thought of as an opposite to the keyof
operator.
import { ValueOf } from '@bedard/types'
type Values = ValueOf<{ foo: number, bar: string }>
Without<A, B>
Prohibit properties of A
and omit properties of B
.
import { Without } from '@bedard/types'
type FooWithoutBar = Without<{ foo: any, bar: any }, { bar: any }>
XOR<A, B>
Create an exclusive or between two types. Note that for objects, this differs from a union type in that keys are strictly matched.
import { XOR } from '@bedard/types'
type FooOrBar = XOR<{ foo: any }, { bar: any }>
const a: FooOrBar = { foo }
const b: FooOrBar = { bar }
const c: FooOrBar = { foo, bar }
License
MIT