minimal-type-guard-helpers
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

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

Package Sidebar

Install

npm i minimal-type-guard-helpers

Weekly Downloads

1

Version

0.2.0

License

MIT

Unpacked Size

6.14 kB

Total Files

16

Last publish

Collaborators

  • fibo