0.2.0 • Published 7 months ago

minimal-type-guard-helpers v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

minimal-type-guard-helpers

provides few helpers to create type guards

This package is implemented with ECMAScript modules. CommonJS is not supported.

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)

type MyItems = MyItem[]
const isMyItems = arrayTypeGuard<MyItem>(isMyItem)

type Foo = { bar: boolean }

const getBar = (arg: unknown): Foo["bar"] | undefined => {
  if (isMaybeObject<Foo>(arg)) return arg.bar
}

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

License

MIT

0.2.0

7 months ago

0.1.1

8 months ago

0.1.0

8 months ago