A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://typescript-eslint.io/rules/no-wrapper-object-types below:

no-wrapper-object-types | typescript-eslint

no-wrapper-object-types

Disallow using confusing built-in primitive class wrappers.

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 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.

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".

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).

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
let myBigint: bigint;
let myBoolean: boolean;
let myNumber: number;
let myString: string;
let mySymbol: symbol;

let myObject: object = "Type 'string' is not assignable to type 'object'.";
Open in Playground

eslint.config.mjs

export default tseslint.config({
rules: {
"@typescript-eslint/no-wrapper-object-types": "error"
}
});

.eslintrc.cjs

module.exports = {
"rules": {
"@typescript-eslint/no-wrapper-object-types": "error"
}
};

Try this rule in the playground ↗

Options

This rule is not configurable.

When Not To Use It

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 Resources

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4