0.2.3 • Published 7 months ago

typesafe-guard v0.2.3

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

typesafe-guard

:information_source: The package is ESM-only.

An utility for creating type-safe user-defined type guard for TypeScript.

Motivation

Writing predicate functions is not type-safe because TS trusts the implementation completely even it is invalid:

const isObject = (x: unknown): x is object => true // always true!
const a: string | object = 'some string'
if (isObject(a)) {
	// now TS treats `a` as an object, even actually it is not
}

So I made a more safe way to writing predicate functions, by requiring narrowing the value to the goal type!

Usage

:white_check_mark: Compiles successfully

interface A {
	a: string
	b: number
}
const isA = predicateFor<A>()((x, ok) => {
	if (!isObjectWithProps({
		a: isString,
		b: isNumber,
	})(x)) return

	return ok(x)
})

\ :x: Error (which is intended)

interface A {
	a: string
	b: number
}
const isA = predicateFor<A>()((x, ok) => {
	if (!isObject(x)) return

	return ok(x) // error, because x is not narrowed enough
})
interface A {
	a: string
	b: number
}
const isA = predicateFor<A>()((x, ok) => {
	if (!isObject(x)) return

	// error, because the function does not return `ok(...)`
})

\ :information_source: You can also create assertion functions from predicate functions!

const assertString: Asserter<string> = asserter(isString)
//                  ^^^^^^^^^^^^^^^^
//                  you need to specify the type yet because
//                  TS requires an explicit type annotation
//                  on assertion functions
0.2.1

7 months ago

0.2.3

7 months ago

0.2.2

7 months ago

0.2.0

11 months ago

0.1.1

11 months ago

0.1.0

11 months ago