ワンクリックで
avoid-wrapper-types
Use when typing primitives. Use when tempted to use String, Number, Boolean. Use when wrapper types appear in errors.
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
メニュー
Use when typing primitives. Use when tempted to use String, Number, Boolean. Use when wrapper types appear in errors.
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
SOC 職業分類に基づく
Use when defining global types. Use when augmenting window. Use when typing environment variables. Use when working with build-time constants. Use when configuring type definitions.
Use when migrating JavaScript to TypeScript. Use when gradually adopting TypeScript. Use when working with mixed codebases. Use when converting large projects. Use when teams are learning TypeScript.
Use when writing asynchronous code. Use when tempted to use callbacks. Use when composing multiple async operations.
Use when creating types from example data. Use when types don't match all cases. Use when API responses vary.
Use when writing type annotations on variables. Use when TypeScript can infer the type. Use when code feels cluttered with types.
Use when defining array-like types. Use when tempted to use number as index type. Use when understanding array keys.
| name | avoid-wrapper-types |
| description | Use when typing primitives. Use when tempted to use String, Number, Boolean. Use when wrapper types appear in errors. |
Use lowercase primitive types, never uppercase wrapper types.
JavaScript has primitive types (string, number, boolean) and object wrapper types (String, Number, Boolean). TypeScript has types for both, but you should almost never use the wrapper types.
String !== stringALWAYS use lowercase: string, number, boolean, symbol, bigint
NEVER use uppercase: String, Number, Boolean, Symbol, BigInt
Remember:
// These look similar but behave differently
const primitive: string = "hello";
const wrapper: String = new String("hello");
// Equality fails
primitive === wrapper; // false (different types)
wrapper === wrapper; // Wait, this is also comparing to itself...
const s1 = new String("hello");
const s2 = new String("hello");
s1 === s2; // false! Each wrapper is a unique object
Primitives don't have methods, but this works:
'primitive'.charAt(3); // 'm' - how?
JavaScript auto-boxes:
String wrapperYou get convenience without the wrapper's drawbacks.
// Primitive assignable to wrapper (OK but don't do this)
const s: String = "primitive"; // Works
// Wrapper NOT assignable to primitive (Error)
function takesString(s: string) { }
takesString(new String("hello"));
// ~~~~~~~~~~~~~~~~~~~
// Argument of type 'String' is not assignable to parameter of type 'string'.
// 'string' is a primitive, but 'String' is a wrapper object.
| Primitive (USE) | Wrapper (AVOID) |
|---|---|
string | String |
number | Number |
boolean | Boolean |
symbol | Symbol |
bigint | BigInt |
Note: null and undefined don't have wrapper types.
const s1 = new String("hello");
const s2 = new String("hello");
s1 == s2; // false
s1 === s2; // false
// Each is a distinct object
typeof "hello"; // "string"
typeof new String("hello"); // "object"
const falsy = new Boolean(false);
if (falsy) {
console.log("This runs!"); // Wrapper objects are always truthy
}
You can use wrappers for their static methods:
// These are fine - not creating wrapper objects
String.fromCharCode(65); // "A"
Number.isNaN(x);
BigInt(123); // Creates primitive, not wrapper
But never use them as types or create instances.
Pressure: "I want to use String methods, so I'll type it as String"
Response: Primitive string has all the same methods via auto-boxing.
Action: Use string type. Methods just work.
Pressure: "TypeScript gave me String, so it must be right"
Response: Check your code - you might be creating a wrapper accidentally.
Action: Find and fix the source. Use lowercase.
String, Number, Boolean in type annotationsnew String(), new Number(), new Boolean() anywheretypeof x === "object" when expecting a primitive| Excuse | Reality |
|---|---|
| "String has more methods" | No, both have identical methods |
| "I need an object" | You rarely do; primitives are simpler |
| "It's the same thing" | No, wrappers have identity and truthiness issues |
// ALWAYS use these
const s: string = "hello";
const n: number = 42;
const b: boolean = true;
const sym: symbol = Symbol();
const big: bigint = 9007199254740991n;
// NEVER use these
const s: String = "hello"; // Wrong
const n: Number = 42; // Wrong
const b: Boolean = true; // Wrong
Always use lowercase primitive types.
There's never a good reason to use String, Number, or Boolean as types. The uppercase versions cause assignment errors, have identity issues, and provide no benefits over the lowercase primitives.
Based on "Effective TypeScript" by Dan Vanderkam, Item 10: Avoid Object Wrapper Types (String, Number, Boolean, Symbol, BigInt).