@achrinza/strong-type

中文文档
1.1.20发布于 2024/05/28MITES Module零依赖近 30 天下载 7万

允许对所有类型的JS对象和类别进行简单的类型执行. 它也可以扩展,并且为您自定义的类和类型使用类型检查提供了简单

中文使用文档
文档目录12 节
  1. 强打本地JS,不硬要求转接.
  2. @achrinza/强力型做什么?
  3. 测试和覆盖范围
  4. 示例 严格与非严格
  5. 类型检查方法
  6. 核心方法
  7. 示例 QQ 基本类型检查
  8. 示例 QQ 生成器类型检查
  9. 日期示例
  10. 是NaN( ) vs 是. NaN( )
  11. 运行浏览器和节点类型支持示例
  12. 为自己的类型扩展类

@achrinza/ 强型

强打本地JS,不硬要求转接.

类型脚本应该是什么 . @achrinza/strong-type 也将在 TypeScript 中工作.

任何地方javascript都可以运行ES6模块的类型检查模块. 这包括节点,电子和浏览器. 完全异形. 不同于TypeScript等其他解决方案, @achrinza/strong-type 除非你想,否则无需转接或捆绑。 这使得它包括任何转录或不转录的框架.

npm install --save @achrinza/strong-type

@achrinza/强力型做什么?

@achrinza/strong-type 允许对所有类型的JS对象和类别进行简单的类型执行. 它还支持 type unions ,以及特殊类型,例如: .any 财务报告和审定财务报表 .defined。它也可以扩展,并且为您自定义的类别和类型提供简单的检查.

这样做不需要任何额外的工具或转接。 这样你或你的组织就可以自由使用任何你想要的工具链或框架,甚至写出香草原生JS.

测试和覆盖范围

@achrinza/strong-type 正在使用 @node-ipc/vanilla-test 这是支持无害环境管理的js的裸骨测试框架,涵盖范围包括: C8 默认覆盖工具。 这对是测试ES6+模块的最有效和准确的方法.

运行测试并通过运行在本地机器上构建覆盖文件 node-test 或查看在 @achrinza/强型CDN家 页:1

示例 严格与非严格

使用 strict 财务报告和审定财务报表 non-strict 模式。 默认 @achrinza/strong-type 运行 strict 并将 throw 如果检查失败,您可以处理和使用动词错误,如果通过,则返回真实值.

用于 non-strict 模式,简单通过 false 给建筑师。 在这个模式下, @achrinza/strong-type 将返回 false 而不是在支票失败时扔出, 并且仍然会返回 true 当它经过.

严格

import Is from '@achrinza/strong-type';

//strict
const is = new Is;
const is = new Is();
const is = new Is(true);

//throws
is.string(1);

//union or multiple possible types
//should not throw
is.union(1,'string|number');

//union or multiple possible types
//should throw
function check(any){
    is.defined(any);
}

非限制性

import Is from '@achrinza/strong-type';

//non-strict
const is = new Is(false);

//returns false
is.string(1);

//union or multiple possible types
//should return true
is.union(1,'string|number');

类型检查方法

所有这些方法都只需要一个词,就是 value 检查.

工会可以加入任何由 Is。 。 。 .

工会参数
is.unionvalue, (中文(简体) ),pipe|seperated|type|list
最常见的类型方法参数
is.globalThisvalue
is.arrayvalue
is.bigintvalue
is.booleanvalue
is.datevalue
is.finitevalue
is.generatorvalue
is.asyncGeneratorvalue
is.infinityvalue
is.mapvalue
is.NaNvalue
is.nullvalue
is.numbervalue
is.objectvalue
is.promisevalue
is.regExpvalue
is.setvalue
is.stringvalue
is.symbolvalue
is.undefinedvalue
is.weakMapvalue
is.weakSetvalue
函数类型方法参数
is.functionvalue
is.asyncFunctionvalue
is.generatorFunctionvalue
is.asyncGeneratorFunctionvalue
错误类型方法参数
is.errorvalue
is.evalErrorvalue
is.rangeErrorvalue
is.referenceErrorvalue
is.syntaxErrorvalue
is.typeErrorvalue
is.URIErrorvalue
缓冲/拖动阵列类型方法参数
is.arrayBuffervalue
is.dataViewvalue
is.sharedArrayBuffervalue
is.bigInt64Arrayvalue
is.bigUint64Arrayvalue
is.float32Arrayvalue
is.float64Arrayvalue
is.int8Arrayvalue
is.int16Arrayvalue
is.int32Arrayvalue
is.uint8Arrayvalue
is.uint8ClampedArrayvalue
is.uint16Arrayvalue
is.uint32Arrayvalue
输入类型方法参数
is.intlDateTimeFormatvalue
is.intlCollatorvalue
is.intlDisplayNamesvalue
is.intlListFormatvalue
is.intlLocalevalue
is.intlNumberFormatvalue
is.intlPluralRulesvalue
is.intlRelativeTimeFormatvalue
垃圾收集方法参数
is.finalizationRegistryvalue
is.weakRefvalue

核心方法

您可以使用这些来直接检查自己的类型/ 类 或扩展 Is 类,以添加您自己的方法,用于检查更多类型,特别是自定义类型和类.

方法参数说明
is.throwvalueType, (中文(简体) ), expectedType这将使用 Type 和 valueType 的值来创建和扔出新的 TypeError
is.typeCheckvalue, (中文(简体) ), type这将检查返回的 javascript 光谱类型 typeof。这样 type arg会是一个字符串 'string', (中文(简体) ), 'boolean', (中文(简体) ), 'number', (中文(简体) ), 'object', (中文(简体) ), 'undefined', (中文(简体) ), 'bigint' 包括:.
is.instanceCheckvalue=:new Fake, (中文(简体) ), constructor=:FakeCore核心默认参数为 a Fake 实例和实例 FakeCore 课。 允许不支持的 js spec 类型以 a 的预期失败 TypeError 而不是一个 Reference 错误(参见 ./example/web/ firefox中缺少一些支持的示例 Intl (第1条)。 此方法比较 value 与 constructor 保值为 instanceof 建筑师.
is.symbolStringCheckvalue, (中文(简体) ), type这可以用来检查 Symbol.toStringTag 但是在核心里我们只用它来检查 generator, (中文(简体) ), GeneratorFunction, (中文(简体) ), async function,以及 async GeneratorFunction 因为它们没有其他方法来检查它们的类型。 发电机 比如说 有一类 [object generator] 这边 所以你通过一个预期 generator 作为 value 字符串 'generator' 作为类型,我们处理其余的, 包括降低一切 保险交叉浏览器和平台检查
is.comparevalue, (中文(简体) ), targetValue, (中文(简体) ), typeName这将可以明确比较 value 财务报告和审定财务报表 targetValue在核心中,我们只用于JS原始人/康斯坦丁人,他们没有其他方法来检查,例如: Infinity 财务报告和审定财务报表 globalThis。类型名称是类类型的字符串表示式,或者一个非常明显的错误字符串,因为这个参数唯一使用的地方是当 compare 结果a throws。 。 。 .
is.definedvalue这将检查一个值不是 is.undefined 任何类型都有效,但 undefined。 。 。 .
is.anyvalue这是别名 is.defined 允许任何类型,但 undefined。 。 。 .
is.existsvalue这是别名 is.defined 以确定是否存在某种东西。 非常有用的特性测试 。 为了简单和透明,设立了别名.

示例 QQ 基本类型检查

@achrinza/strong-type 它的用法非常简单.

import Is from '@achrinza/strong-type';

//strict
const is = new Is;

function strongTypeRequired(aNumber,aString,anAsyncFunction){
    is.number(aNumber);
    is.string(aString);
    is.asyncFunction(anAsyncFunction);
}

function unionStrongTypeRequired(aNumberOrString){
    is.union(aNumberOrString,'number|string');
}


//this will throw because we do not pass an async Function, but rather a normal function.
strongTypeRequired(1,'a',function(){})

//these will both pass because we accept both types in a union for the first param. 
unionStrongTypeRequired(1);
unionStrongTypeRequired('a');


浏览器

Basic Type Checking Example Web

节点

Basic Type Checking Example Node

示例 QQ 生成器类型检查

发电机在输入许多devs时会出名。 这就是为什么我们选择以它们为例.

import Is from '@achrinza/strong-type';

//strict
const is = new Is;

//empty generator for this example's sake
function* myGenFunc(){};
const myGen=myGenFunc();

//we'll show async as well
async function* myAsyncGenFunc(){}
const myAsyncGen=myAsyncGenFunc();

//empty function
function myFunc(){};

//will pass and allow contunue
is.generator(myGen);

/*
will fail because a generatorFunction is a 
GeneratorFunction, not a Generator
*/
try{
    is.generator(myGenFunc);
}catch(err){
    console.log(err);
}

//will pass and allow contunue
is.generatorFunction(myGenFunc);

//will fail because a function is not a generatorFunction
try{
    is.generatorFunction(myFunc);
}catch(err){
    console.log(err);
}

/*
will fail because this is @achrinza/strong-type, a 
generatorFunction is explicitly a GeneratorFunction,
and not a Function
*/
try{
    is.function(myGenFunc);
}catch(err){
    console.log(err);
}

//will fail because a function is not a generatorFunction
try{
    is.generatorFunction(myFunc);
}catch(err){
    console.log(err);
}

//will pass and allow contunue
is.asyncGeneratorFunction(myAsyncGenFunc);

//will pass and allow contunue
is.asyncGenerator(myAsyncGen);

/*
will fail becase @achrinza/strong-type 
asyncGenerators and generators are explicitly different
this is the same for generatorFunctions and functions
*/
try{
    is.asyncGenerator(myGen);
}catch(err){
    console.log(err);
}

try{
    is.generator(myAsyncGen);
}catch(err){
    console.log(err);
}


浏览器

Generator Type Checking Example Web

节点

Generator Type Checking Example Node

日期示例

import Is from '@achrinza/strong-type';

const is = new Is;

//returns true
is.date(new Date()); 

//throws in strict or returns false in non-strict
is.date(1975);

是NaN( ) vs 是. NaN( )

Javascripts类型因性质而软弱,所以在 isNaN() 函数返回任何非数字的正则,但 is.NaN() 仅在明确通过时返回真实 NaN。 。 。 .

import Is from '@achrinza/strong-type';

const is = new Is;

//built in JS isNaN
//returns false
isNaN(1);

//all return true
isNaN(NaN);
isNaN(undefined);
isNaN('a'); 

//@achrinza/strong-type is.NaN all return false in non-strict mode,
//or throw in default strict mode
is.NaN(1);
is.NaN(undefined);
is.NaN('a');

//in @achrinza/strong-type only this returns true
is.NaN(NaN);

浏览器

Date Type Checking Example Web

节点

Date Type Checking Example Node

运行浏览器和节点类型支持示例

运行 npm i 在此模块的 root dir 中,可以确保您得到devDependents 的安装.

节点示例 :

npm run nodeExample 整个屏幕应该是绿色的,因为所有类型都支持在节点.

浏览器示例和支持测试

npm start
这个会转动 node-http-server 在端口8000上的此模块根中。 浏览器示例在 ./example/web/ 文件夹。 你可以看到他们 通过去这个本地地址: http://localhost:8000 (英语)

Chrome, Opera, and Edge 支持所有类型,所以所有行会是绿色的.

您会在 Firefox 中看到一些红行, 因为它尚未支持所有类型 。 未支持的类型会在检查/验证时丢出类型错误 .

数字海洋静态App

我们使用免费的数字海洋静态Apps来托管本地服务器的版本. 跟你跑的一样 npm start 在你的机器上。 也可以像CDN一样使用,因为它每次更新分支时都会自动从主/主重建. @achrinza/强型CDN家 页:1

为自己的类型扩展类

如果你在生产中使用类型检查,你最好继续扩展模块,而不是叫更麻烦的 Core Methods 许多次.

自定义披萨类型

//custom class type constructor
class Pizza{
    constructor(topping){
        this.eat=true;
    }
}

export {default:Pizza, Pizza}

扩展

import Is from '@achrinza/strong-type';
import Pizza from 'my-delicious-pizza';

class IsMy extends Is{
    //custom pizza type
    pizza(value){
        return this.instanceCheck(value,Pizza);
    }
}

export={default:IsMy, IsMy};

测试

import IsMy from 'my-delicious-typechecks';
import Pizza from 'my-delicious-pizza';

const is=new IsMy;

//will throw because 42 is not a Pizza Type
//and 
is.pizza(42)

浏览器

Pizza Type Checking Example Web

节点

Pizza Type Checking Example Node

版本与发布记录

@achrinza/strong-type 共发布 18 个版本,首次发布于 2022/03/25;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.20latest2024/05/282 年前
1.1.92024/05/272 年前
0.1.14v0-latest2024/05/272 年前
0.1.132023/10/253 年前
0.1.122023/10/253 年前
1.1.82023/10/253 年前

关于 @achrinza/strong-type 的常见问题

@achrinza/strong-type 是什么?

允许对所有类型的JS对象和类别进行简单的类型执行. 它也可以扩展,并且为您自定义的类和类型使用类型检查提供了简单

如何安装 @achrinza/strong-type?

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

@achrinza/strong-type 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/achrinza__strong-type 是否存在。

@achrinza/strong-type 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@achrinza/strong-type 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^12.21.0 || 14 || 15 || 16 || 17 || 18 || 19 || 20 || 21 || 。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@achrinza/strong-type 可以商用吗?

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

@achrinza/strong-type 的下载量有多少?

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

@achrinza/strong-type 还在维护吗?

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