# @achrinza/strong-type 中文文档

规范地址：https://npmcn.com/package/%40achrinza/strong-type/

版本：1\.1\.20
npm 元数据更新时间：2024-05-28T06:51:22.271Z
中文译文更新时间：2026-11-05T18:29:07.070Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40achrinza/strong-type
原文对照：https://npmcn.com/package/%40achrinza/strong-type/?lang=original
项目主页：https://github.com/achrinza/strong-type#readme
源码仓库：https://github.com/achrinza/strong-type

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：26 kB，6 个文件
- 模块格式：ES Module
- TypeScript 类型：未声明
- Node.js 要求：^12\.21\.0 \|\| 14 \|\| 15 \|\| 16 \|\| 17 \|\| 18 \|\| 19 \|\| 20 \|\| 21 \|\| 
- 运行依赖：0 个
- 首次发布：2022-03-25T10:21:51.017Z
- 最新版本发布：2024-05-28T06:51:22.112Z
- 近 30 天下载量：69600（2026-10-05 至 2026-11-03，npm 官方下载统计）
- 已发布版本数：18（近 12 个月 0 个）

## 中文简介

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

## 安装

```sh
npm install @achrinza/strong-type
```

也可以使用：`pnpm add @achrinza/strong-type`、`yarn add @achrinza/strong-type`、`bun add @achrinza/strong-type`

## README

# @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`](https://github.com/node-ipc/vanilla-test) 这是支持无害环境管理的js的裸骨测试框架,涵盖范围包括: [`C8`](https://github.com/bcoe/c8) 默认覆盖工具。 这对是测试ES6+模块的最有效和准确的方法.

运行测试并通过运行在本地机器上构建覆盖文件 `node-test` 或查看在 [@achrinza/强型CDN家](https://cdn-p939v.ondigitalocean.app/@achrinza/strong-type/) 页:1

## 示例 严格与非严格

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

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

#### 严格

```javascript
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);
}

```

#### 非限制性

```javascript
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.union` | `value`, (中文(简体) ),`pipe|seperated|type|list` |

| 最常见的类型方法 | 参数 |
| --- | --- |
| `is.globalThis` | `value` |
| `is.array` | `value` |
| `is.bigint` | `value` |
| `is.boolean` | `value` |
| `is.date` | `value` |
| `is.finite` | `value` |
| `is.generator` | `value` |
| `is.asyncGenerator` | `value` |
| `is.infinity` | `value` |
| `is.map` | `value` |
| `is.NaN` | `value` |
| `is.null` | `value` |
| `is.number` | `value` |
| `is.object` | `value` |
| `is.promise` | `value` |
| `is.regExp` | `value` |
| `is.set` | `value` |
| `is.string` | `value` |
| `is.symbol` | `value` |
| `is.undefined` | `value` |
| `is.weakMap` | `value` |
| `is.weakSet` | `value` |

| 函数类型方法 | 参数 |
| --- | --- |
| `is.function` | `value` |
| `is.asyncFunction` | `value` |
| `is.generatorFunction` | `value` |
| `is.asyncGeneratorFunction` | `value` |

| 错误类型方法 | 参数 |
| --- | --- |
| `is.error` | `value` |
| `is.evalError` | `value` |
| `is.rangeError` | `value` |
| `is.referenceError` | `value` |
| `is.syntaxError` | `value` |
| `is.typeError` | `value` |
| `is.URIError` | `value` |

| 缓冲/拖动阵列类型方法 | 参数 |
| --- | --- |
| `is.arrayBuffer` | `value` |
| `is.dataView` | `value` |
| `is.sharedArrayBuffer` | `value` |
| `is.bigInt64Array` | `value` |
| `is.bigUint64Array` | `value` |
| `is.float32Array` | `value` |
| `is.float64Array` | `value` |
| `is.int8Array` | `value` |
| `is.int16Array` | `value` |
| `is.int32Array` | `value` |
| `is.uint8Array` | `value` |
| `is.uint8ClampedArray` | `value` |
| `is.uint16Array` | `value` |
| `is.uint32Array` | `value` |

| 输入类型方法 | 参数 |
| --- | --- |
| `is.intlDateTimeFormat` | `value` |
| `is.intlCollator` | `value` |
| `is.intlDisplayNames` | `value` |
| `is.intlListFormat` | `value` |
| `is.intlLocale` | `value` |
| `is.intlNumberFormat` | `value` |
| `is.intlPluralRules` | `value` |
| `is.intlRelativeTimeFormat` | `value` |

| 垃圾收集方法 | 参数 |
| --- | --- |
| `is.finalizationRegistry` | `value` |
| `is.weakRef` | `value` |

## 核心方法

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

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

## 示例 QQ 基本类型检查

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

```javascript
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](https://raw.githubusercontent.com/achrinza/strong-type/main/docs/img/basicExampleWeb.PNG)

#### 节点

![Basic Type Checking Example Node](https://raw.githubusercontent.com/achrinza/strong-type/main/docs/img/basicExampleNode.PNG)

## 示例 QQ 生成器类型检查

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

```javascript
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](https://raw.githubusercontent.com/achrinza/strong-type/main/docs/img/generatorExampleWeb.PNG)

#### 节点

![Generator Type Checking Example Node](https://raw.githubusercontent.com/achrinza/strong-type/main/docs/img/generatorExampleNode.PNG)

## 日期示例

```javascript
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`。 。 。 .

```js
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](https://raw.githubusercontent.com/achrinza/strong-type/main/docs/img/dateExampleWeb.PNG)

#### 节点

![Date Type Checking Example Node](https://raw.githubusercontent.com/achrinza/strong-type/main/docs/img/dateExampleNode.PNG)

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

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

#### 节点示例 :

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

#### 浏览器示例和支持测试

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

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

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

#### 数字海洋静态App

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

## 为自己的类型扩展类

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

#### 自定义披萨类型

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

export {default:Pizza, Pizza}
```

#### 扩展

```javascript
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};
```

#### 测试

```javascript
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](https://raw.githubusercontent.com/achrinza/strong-type/main/docs/img/pizzaExampleWeb.PNG)

#### 节点

![Pizza Type Checking Example Node](https://raw.githubusercontent.com/achrinza/strong-type/main/docs/img/pizzaExampleNode.PNG)

## 常见问题

### @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 和提交记录判断。

