type-detect

中文文档
4.1.0发布于 2024/07/25MITCommonJS零依赖近 30 天下载 2.8亿

Improved typeof detection for node.js and the browser.

原始使用文档
文档目录7 节
  1. What is Type-Detect?
  2. Why?
  3. Installation
  4. Node.js
  5. Deno
  6. Browsers
  7. Usage

type-detect


Improved typeof detection for node, Deno, and the browser.

license:mit npm:? build:? coverage:? dependencies:? devDependencies:?
Join the Slack chat Join the Gitter chat

Supported Browsers
Chrome Edge Firefox Safari IE
✅ ✅ ✅ ✅ 9, 10, 11

What is Type-Detect?

Type Detect is a module which you can use to detect the type of a given object. It returns a string representation of the object's type, either using typeof or @@toStringTag. It also normalizes some object names for consistency among browsers.

Why?

The typeof operator will only specify primitive values; everything else is "object" (including null, arrays, regexps, etc). Many developers use Object.prototype.toString() - which is a fine alternative and returns many more types (null returns [object Null], Arrays as [object Array], regexps as [object RegExp] etc).

Sadly, Object.prototype.toString is slow, and buggy. By slow - we mean it is slower than typeof. By buggy - we mean that some values (like Promises, the global object, iterators, dataviews, a bunch of HTML elements) all report different things in different browsers.

type-detect fixes all of the shortcomings with Object.prototype.toString. We have extra code to speed up checks of JS and DOM objects, as much as 20-30x faster for some values. type-detect also fixes any consistencies with these objects.

Installation

Node.js

type-detect is available on npm. To install it, type:

$ npm install type-detect

Deno

type-detect can be imported with the following line:

import type from 'https://deno.land/x/[email protected]/index.ts'

Browsers

You can also use it within the browser; install via npm and use the type-detect.js file found within the download. For example:

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

Usage

The primary export of type-detect is function that can serve as a replacement for typeof. The results of this function will be more specific than that of native typeof.

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

Or, in the browser use case, after the

版本与发布记录

type-detect 共发布 18 个版本,首次发布于 2013/08/14;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.1.0latest2024/07/252 年前
4.0.82018/02/018 年前
4.0.72018/01/198 年前
4.0.62018/01/168 年前
4.0.52017/11/098 年前
4.0.42017/11/098 年前

关于 type-detect 的常见问题

type-detect 是什么?

Improved typeof detection for node.js and the browser.

如何安装 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 和提交记录判断。