Skip to main content

no-wrapper-object-types

Disallow using confusing built-in primitive class wrappers.

ESLint 配置 中扩展"plugin:@typescript-eslint/recommended" 可启用此规则。

🔧

此规则报告的一些问题可通过 --fix ESLint 命令行选项自动修复

TypeScript 定义了几对令人困惑的类型,它们看起来非常相似,但实际上意味着不同的东西:boolean/Boolean, number/Number, string/String, bigint/BigInt, symbol/Symbol, object/Object.一般来说,只有小写变体才适合使用。因此,此规则强制你仅使用小写变体。

¥TypeScript defines several confusing pairs of types that look very similar to each other, but actually mean different things: boolean/Boolean, number/Number, string/String, bigint/BigInt, symbol/Symbol, object/Object. In general, only the lowercase variant is appropriate to use. Therefore, this rule enforces that you only use the lowercase variant.

JavaScript 在运行时具有 8 种数据类型,这些在 TypeScript 中由小写类型 undefinednullbooleannumberstringbigintsymbolobject 描述。

¥JavaScript has 8 data types at runtime, and these are described in TypeScript by the lowercase types undefined, null, boolean, number, string, bigint, symbol, and object.

至于大写类型,这些是描述每种数据类型的 JavaScript "wrapper" 对象的结构类型,例如 BooleanNumber。此外,由于结构类型的怪癖,相应的原语也可以分配给这些大写类型,因为它们具有相同的 "shape"。

¥As for the uppercase types, these are structural types which describe JavaScript "wrapper" objects for each of the data types, such as Boolean and Number. Additionally, due to the quirks of structural typing, the corresponding primitives are also assignable to these uppercase types, since they have the same "shape".

直接使用内置原语(如 0)而不是 "看起来像" 相应原语(如 new Number(0))的对象是一种通用的最佳实践。

¥It is a universal best practice to work directly with the built-in primitives, like 0, rather than objects that "look like" the corresponding primitive, like new Number(0).

  • 原始值具有 ===== 相等性检查的预期值语义,而它们的对象对应项则通过引用进行比较。也就是说,"str" === "str"new String("str") !== new String("str")

    ¥Primitives have the expected value semantics with == and === equality checks, whereas their object counterparts are compared by reference. That is to say, "str" === "str" but new String("str") !== new String("str").

  • 原始值具有众所周知的关于真假的行为,这是常见的依赖,而所有对象都是真实的,无论封装的值如何(例如 new Boolean(false) 是真实的)。

    ¥Primitives have well-known behavior around truthiness/falsiness which is common to rely on, whereas all objects are truthy, regardless of the wrapped value (e.g. new Boolean(false) is truthy).

  • TypeScript 仅允许对数字原语而不是对象执行算术运算(例如 x - y)。

    ¥TypeScript only allows arithmetic operations (e.g. x - y) to be performed on numeric primitives, not objects.

因此,在 TypeScript 类型中使用小写类型名称(如 number)而不是大写名称(如 Number)是一种更好的做法,可以更准确地描述代码。

¥As a result, using the lowercase type names like number in TypeScript types instead of the uppercase names like Number is a better practice that describes code more accurately.

此规则的代码示例:

¥Examples of code for this rule:

let myBigInt: BigInt;
let myBoolean: Boolean;
let myNumber: Number;
let myString: String;
let mySymbol: Symbol;

let myObject: Object = 'allowed by TypeScript';
Open in Playground
eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/no-wrapper-object-types": "error"
}
});

在线运行试试这个规则 ↗

选项

该规则不可配置。

何时不使用它

¥When Not To Use It

如果你的项目是故意处理原语的类等价物的罕见项目,则可能不值得使用此规则。你可以考虑在这些特定情况下使用 ESLint 禁用注释,而不是完全禁用此规则。

¥If your project is a rare one that intentionally deals with the class equivalents of primitives, it might not be worthwhile to use this rule. You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.

进一步阅读

¥Further Reading

相关

¥Related To

'## 资源'