@altostra/type-validations

中文文档
2.7.2发布于 2023/02/05MITTypeScriptCommonJS近 30 天下载 7926

Utility functions to create type-guards and assertions.

原始使用文档
文档目录18 节
  1. Installation
  2. Reference
  3. allOf
  4. anyOf
  5. arrayOf
  6. assertBy
  7. enumOf
  8. is
  9. isEmptyArray
  10. isEmptyObject
  11. isFunction
  12. maybe
  13. objectOf
  14. recordOf
  15. singleOrArray
  16. taggedUnionOf
  17. tupleOf
  18. Primitives

Type validations

Open in Visual Studio Code CI npm version

Type validations is a library for creating type-validators: augmented type-guards that perform full runtime type-validation and let TypeScript type checker know what a type may be in a certain context.

Installation

$ npm install @altostra/type-validations

Reference

A full reference can be found here.

allOf

Creates type-validators for an intersections-type from a list of type-validators.

anyOf

Creates type-validators for a union-type from a list of type-validators.

arrayOf

Given type-guard for type T, creates a type-validator for T[].

assertBy

Create assertion-functions from type-validation and an error-factory.

enumOf

Creates a type-validation that checks if a value is one of the specified values (using strict equality).

is

Creates a type-validation that only allows values that are strictly equal to the provided value.

isEmptyArray

A type-validation that checks if a value is an empty array.

isEmptyObject

A type-validation that checks if a value is an empty object.

isFunction

Creates a type-validation that checks if a value is a function with a specified length.

maybe

Creates a type-validation that checks if a value is of validated type, undefined, and optionally null.

objectOf

Creates type-validation that checks if a value is an object that all its properties are validated with the provided validations.

recordOf

Creates type-validator that checks that a given value is an object and that all its own-enumerable properties are of a specified type.

singleOrArray

Creates a type-validation that checks if a value is either a specific type or an array of that type.

taggedUnionOf

Creates type-validators for a union-type from a mapping between specified tag property values and validators, when the union-type is in fact a discriminated union.

tupleOf

Creates a type-validation that checks that an object is an array with a specific length, and has all the provided indices validated.

Primitives

Type-validations

  • string
  • maybeString
  • number
  • maybeNumber
  • boolean
  • maybeBoolean
  • nullValidation
  • nullOrUndefined
  • undefinedValidation
  • symbol
  • maybeSymbol
  • bigint
  • maybeBigint
  • any
  • unknown
  • never

Type-assertions

  • stringAssertion
  • maybeStringAssertion
  • numberAssertion
  • maybeNumberAssertion
  • booleanAssertion
  • maybeBooleanAssertion
  • nullAssertion
  • nullOrUndefinedAssertion
  • undefinedAssertion
  • symbolAssertion
  • maybeSymbolAssertion
  • bigintAssertion
  • maybeBigintAssertion
  • anyAssertion
  • unknownAssertion
  • assert

版本与发布记录

@altostra/type-validations 共发布 42 个版本,首次发布于 2020/05/18;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.7.2latest2023/02/053 年前
2.7.12023/01/063 年前
2.7.02022/12/153 年前
2.6.62022/11/173 年前
2.6.52022/09/114 年前
2.6.42022/08/224 年前

其他发布通道:ts41 → 2.0.0-ts-41.0,rc → 2.1.0-simplified-typing.0,record-key-validations → 2.3.0-record-key-validations.0,tagged-union → 2.4.0-tagged-union.1,ts-4.3 → 2.4.1-ts-4.3.0,deep-actions → 2.5.0-deep-actions.0。

关于 @altostra/type-validations 的常见问题

@altostra/type-validations 是什么?

Utility functions to create type-guards and assertions.

如何安装 @altostra/type-validations?

在项目目录运行 npm install @altostra/type-validations,或使用 pnpm add / yarn add / bun add。

@altostra/type-validations 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@altostra/type-validations 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@altostra/type-validations 可以商用吗?

@altostra/type-validations 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@altostra/type-validations 的下载量有多少?

根据 npm 官方下载统计,@altostra/type-validations 在 2026/10/06 – 2026/11/04 这 30 天内被下载 7,926 次,日均约 264 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@altostra/type-validations 还在维护吗?

最新版本 2.7.2 发布于 2023/02/05(3 年前);至今共发布 42 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/05/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。