
Product
Introducing Socket Firewall Enterprise: Flexible, Configurable Protection for Modern Package Ecosystems
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.
type-assurer
Advanced tools
TypeScript library that provides shorthand type assertions and type guard functions for multiple types.
type-assurer is a TypeScript-first type checking library, providing compatibility with lodash's type guard functions while ensuring type safety. Designed with ESModules in mind, it allows for tree-shaking to minimize bundle sizes.
npm install type-assurer
The library provides 8 utility functions for each type guard, such as isString, isNull, etc.
And, note that fallbackNotNil can be replaced with the ?? operator. Functions that can be simplified using standard JavaScript expressions, like this example, are not targeted for implementation.
Functions such as is simply provide type guards that can be used in conditional branches.
import { isString } from 'type-assurer'
declare const value: unknown
if (isString(value)) {
console.log(`This is a string: ${value}`)
} else {
console.log('This is not a string')
}
Functions such as isNot are useful in cases that require a type guard function as an argument, such as Array.prototype.filter.
import { isNotNil } from 'type-assurer'
declare const values: string | null
const result = values.filter(isNotNil)
// ^? string[]
Functions with names like assert assertNot are type assertion functions.
If the type check does not pass, it throws a TypeError.
The second argument can contain an error message.
import { assertString } from 'type-assurer'
declare const value: unknown
assertString(value, 'Value must be a string')
// No error if value is a string, otherwise throws an error with the message "Value must be a string"
Functions with names like ensure ensureNot are type assertion functions, but return the same value if the type check passes.
It is convenient to write type assertions on a single line.
The second argument can contain an error message.
import { ensureString } from 'type-assurer'
declare function fetchData(): Promise<string | undefined>
const value = ensureString(await fetchData(), 'Value must be a string')
// ^? string
// No error if fetchData returns a string, otherwise throws an error with the message "Value must be a string"
Functions like fallback fallbackNot are type modification functions.
They return the same value if the type check passes, otherwise they return the fallback value specified in the second argument.
import { fallbackString } from 'type-assurer'
declare function fetchData(): Promise<string | undefined>
const value = fallbackString(await fetchData(), 'default')
// ^? string
// Returns value if it's a string, otherwise returns the fallbackValue
Contributions are welcome! Please submit a pull request or open an issue to discuss any proposed changes or feature requests.
FAQs
TypeScript library that provides shorthand type assertions and type guard functions for multiple types.
The npm package type-assurer receives a total of 397 weekly downloads. As such, type-assurer popularity was classified as not popular.
We found that type-assurer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.

Security News
Open source dashboard CNAPulse tracks CVE Numbering Authorities’ publishing activity, highlighting trends and transparency across the CVE ecosystem.

Product
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.