Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/fibo/minimal-type-guard-helpers

minimal type-guard helpers
https://github.com/fibo/minimal-type-guard-helpers

helpers minimal type-guards

Last synced: about 1 month ago
JSON representation

minimal type-guard helpers

Awesome Lists containing this project

README

        

# minimal-type-guard-helpers

> provides few helpers to create _type guards_

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

## Synopsis

```ts
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(myItems)

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

type Foo = { bar: boolean }

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

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

## License

[MIT](https://fibo.github.io/mit-license)