Skip to content

fibo/minimal-type-guard-helpers

Repository files navigation

minimal-type-guard-helpers

Helpers to create type guards

Synopsis

import {
  arrayTypeGuard,
  isLiteralType,
  isMaybeObject,
  objectTypeGuard
} from 'minimal-type-guard-helpers'

export const myItems = ['foo', 'bar'] as const
export type MyItem = (typeof myItems)[number]
export const isMyItem = isLiteralType<MyItem>(myItems)

const isMyItems = arrayTypeGuard<MyItem>(isMyItem)

type Foo = { bar: boolean }

const getBar = (arg: unknown): Foo['bar'] | undefined => {
  if (isMaybeObject<Foo>(arg)) {
    // Here we safely can destructure arg as Foo.
    const { bar } = arg
    if (typeof bar === 'boolean') return bar
  }
}

const isFoo = objectTypeGuard<Foo>(({ bar }) => {
  return typeof bar === 'boolean'
})

License

MIT

About

Helpers to create type guards

Topics

Resources

License

Stars

Watchers

Forks

Contributors