The condition to assert.
Optional
message: string | (() => string | Error)An error message or a function that returns either a message or an Error object.
If the condition is false.
const value: number
// might throw new Error('value must be greater than 0')
assert(value > 0, 'value must be greater than 0')
// might throw new Error('assert condition not satisfied')
assert(value > 0)
import { isDefined } from 'uft'
const value: string | undefined
// might throw new Error('value must be defined')
assert(isDefined(value), 'value must be defined')
value // type: string
Asserts that the given condition is true. If the condition is false, an error is thrown.
The condition to assert.
The Error constructor to use.
Rest
...args: ConstructorParameters<TError> | [(() => ConstructorParameters<TError>)]The arguments or a function that will return them, to pass to the Error constructor.
If the condition is false.
class CustomError extends Error {
constructor(code: number, message: string) {
super(`${code}: ${message}`)
}
}
const isUser: boolean
// might throw new CustomError(401, 'Unauthorized')
assert(isUser, CustomError, 401, 'Unauthorized')
import { isDefined } from 'uft'
class NotFound extends Error {
constructor() {
super('404 Not Found')
}
}
const value: string | undefined
// might throw new NotFound()
assert(isDefined(value), NotFound)
value // type: string
Generated using TypeDoc
Asserts that the given condition is true. If the condition is false, an error is thrown.