# type-detect 中文文档

规范地址：https://npmcn.com/package/type-detect/

版本：4\.1\.0
npm 元数据更新时间：2024-07-25T10:50:20.663Z
中文译文更新时间：2026-11-05T16:07:19.917Z
软件包许可：MIT

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

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

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

- 包体积（解压后）：38.7 kB，7 个文件
- 模块格式：CommonJS
- TypeScript 类型：未声明
- Node.js 要求：\>=4
- 运行依赖：0 个
- 首次发布：2013-08-14T12:07:53.439Z
- 最新版本发布：2024-07-25T10:50:20.475Z
- 近 30 天下载量：280760342（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：18（近 12 个月 0 个）

## 中文简介

改进了节点.js和浏览器的检测类型.

## 安装

```sh
npm install type-detect
```

也可以使用：`pnpm add type-detect`、`yarn add type-detect`、`bun add type-detect`

## README

# [![type-detect](https://raw.githubusercontent.com/chaijs/type-detect/master/type-detect-logo.svg)](http://chaijs.com/ "Chai Documentation")

  

改进检测类型 [节点](https://nodejs.org/), (中文(简体) ), [德诺](https://deno.land/),还有浏览器.

 [![license:mit](https://img.shields.io/badge/license-mit-green.svg?style=flat-square)](https://npmcn.com/package/type-detect/LICENSE)[![npm:?](https://img.shields.io/npm/v/type-detect.svg?style=flat-square) ](https://www.npmjs.com/packages/type-detect)[![build:?](https://github.com/chaijs/type-detect/workflows/Build/badge.svg) ](https://github.com/chaijs/type-detect)[![coverage:?](https://img.shields.io/coveralls/chaijs/type-detect/master.svg?style=flat-square) ](https://coveralls.io/r/chaijs/type-detect)[![dependencies:?](https://img.shields.io/npm/dm/type-detect.svg?style=flat-square) ](https://www.npmjs.com/packages/type-detect)![devDependencies:?](https://img.shields.io/david/chaijs/type-detect.svg?style=flat-square)  
 [![Join the Slack chat](https://img.shields.io/badge/slack-join%20chat-E2206F.svg?style=flat-square)](https://chai-slack.herokuapp.com/)[![Join the Gitter chat](https://img.shields.io/badge/gitter-join%20chat-D0104D.svg?style=flat-square)](https://gitter.im/chaijs/chai)

| 支持的浏览器 |
| --- |
| ![](https://camo.githubusercontent.com/ab586f11dfcb49bf5f2c2fa9adadc5e857de122a/687474703a2f2f73766773686172652e636f6d2f692f3278532e737667) 颜色 | ![](https://camo.githubusercontent.com/98cca3108c18dcfaa62667b42046540c6822cdac/687474703a2f2f73766773686172652e636f6d2f692f3279352e737667) 边缘 | ![](https://camo.githubusercontent.com/acdcb09840a9e1442cbaf1b684f95ab3c3f41cf4/687474703a2f2f73766773686172652e636f6d2f692f3279462e737667) 火花 | ![](https://camo.githubusercontent.com/728f8cb0bee9ed58ab85e39266f1152c53e0dffd/687474703a2f2f73766773686172652e636f6d2f692f3278342e737667) 萨法里 | ![](https://camo.githubusercontent.com/96a2317034dee0040d0a762e7a30c3c650c45aac/687474703a2f2f73766773686172652e636f6d2f692f3279532e737667) 独立 |
| ✅ | ✅ | ✅ | ✅ | 第9、10、11条 |

## 什么是类型检测?

类型检测(Type Detection)是一个模块,可以用来检测给定对象的类型. 它返回对象类型的字符串表示,或者使用 [`typeof`](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-typeof-operator) 或者说 [`@@toStringTag`](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-symbol.tostringtag)。它还使一些对象名称正常化,以便浏览器之间保持一致.

## 为什么?

这个 `typeof` 运算符将只指定原始值; 其他都是 `"object"` (包括 `null`, 数组, regexps等). 许多开发者使用 `Object.prototype.toString()` - 这是一个精细的选项,并返回更多类型(不返回) `[object Null]`,矩阵为 `[object Array]`,正则表示为 `[object RegExp]` (见e/cn.4/sub.2/2000/l.31).

很遗憾, `Object.prototype.toString` 慢了点 慢了点 慢一点,我们的意思是慢于 `typeof`。通过buggy - 我们指的是一些值(比如承诺,全局对象,延展者,数据视图,一串HTML元素)都在不同浏览器中报告不同的东西.

`type-detect` 纠正所有缺点 `Object.prototype.toString`我们有额外的代码来加快JS和DOM对象的检查,对于一些值来说,速度高达20-30x. `type-detect` 并修正这些物体的任何成分.

## 安装

### 节点.js

`type-detect` 可在 [无](http://npmjs.org/)。要安装,请输入:

```
$ npm install type-detect
```

### 德诺

`type-detect` 可用下列一行导入:

```js
import type from 'https://deno.land/x/type_detect@v4.1.0/index.ts'
```

### 浏览器

您也可以在浏览器内使用;通过 npm 安装并使用 `type-detect.js` 在下载中找到文件。 例如:

```html
<script src="./node_modules/type-detect/type-detect.js"></script>
```

## 使用量

二. 主要出口 `type-detect` 函数可以替代 `typeof`。此功能的结果将比本地的更具体 `typeof`。 。 。 .

```js
var type = require('type-detect');
```

或者,在浏览器使用的情况下

## 常见问题

### type-detect 是什么？

改进了节点\.js和浏览器的检测类型\.

### 如何安装 type-detect？

在项目目录运行 npm install type-detect，或使用 pnpm add / yarn add / bun add。

### type-detect 支持 TypeScript 吗？

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

### type-detect 支持 ESM 还是 CommonJS？

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

### type-detect 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=4。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### type-detect 可以商用吗？

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

### type-detect 的下载量有多少？

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

### type-detect 还在维护吗？

最新版本 4\.1\.0 发布于 2024/07/25（2 年前）；至今共发布 18 个版本，其中过去 12 个月发布了 0 个；首次发布于 2013/08/14。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

