Global

Members

# constant narrow

This function validates any value with typeof checks. Arrays and objects are traversed according to the Narrower structure. The boolean return value is also a TypeScript type predicate.

Objects - All keys of n are checked against u and their narrow is validated if the key exists. Keys that are missing from u are treated as having the value undefined. This means you can use { key: some('undefined', ...)} to allow for missing/optional keys.

Arrays - Including multiple types in a Narrower array allows for mixed types. Each item in u must satisfy at least one of the types.

Null - typeof null is 'object' but null cannot have any keys. Use {} to match an object that is not null.

View Source narrow.ts, line 56

Example
// An array of mixed strings and numbers:
narrow(['string', 'number'], [1, 'two']) //=> true
narrow(['string', 'number'], [{}]) //=> false

// Null:
narrow('object', null) //=> true
narrow({}, null) //=> false

// A deep object:
narrow({
	n: 'number',
	child: {
		word: 'string'
	},
	things: [
		['number'],
		'boolean'
	],
}, {
	n: 3.14,
	child: {
		word: 'Yes'
	},
	things: [
		false,
		[1, 2, 3],
		true
	]
}) //=> true

# constant satisfier

Creates a function from a narrower schema that can be reused to narrow objects. This simple closure can be used when a whole Guard instance would be too much.

View Source guard.ts, line 12

Example
import { satisfier } from 'narrow-minded'
const satisfies = satisfier(['string', 'number'])
satisfies(['horse', 42]) // => true

# constant some

Decorates a narrower array to indicate narrowing should use the array as a set of options instead of asserting the value is an actual array.

View Source narrow.ts, line 72

Example
narrow(some('number'), 1) //=> true
narrow({ optional: some('string', 'undefined') }), { optional: 'yep' }) //=> true
narrow({ optional: some('string', 'undefined') }), {}) //=> true

# constant unknown

A singleton that can be used to build and chains.

View Source guard.ts, line 90

Example
if (unknown.and('string').satisfied('Great')) {
	console.log('Great')
}

Type Definitions

Primitive | NarrowerArr | NarrowerObj | NarrowerSome

# Narrower

This is the type that specifies a narrowed structure. The simplest form is a Primitive string, which will validate using a typeof comparison. Deeper structures can be defined using objects and arrays that will be validated recursively.

View Source narrow.ts, line 124

Example
// An array of mixed strings and numbers:
['string', 'number']

// A deep object:
{
	n: 'number',
	child: {
		word: 'string'
	},
	things: [
		['number'],
		'boolean'
	],
}
'string' | 'number' | 'bigint' | 'boolean' | 'symbol' | 'undefined' | 'object' | 'function'

# Primitive

Includes all values that can be returned by a typeof expression.

View Source narrow.ts, line 118