Banned TypeScript Types
ID |
javascript.ts_ban_types |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Best Practice |
Language |
JavaScript |
Tags |
best-practice, type-safety, typescript |
Description
Reports TypeScript type references whose name is the capitalised boxed-primitive constructor or Function, plus the empty object type {}:
function f(value: Object): String { ... } // FLAW + FLAW
const cb: Function = () => { ... }; // FLAW
const data: {} = read(); // FLAW
Rationale
The capitalised names are the JavaScript constructor functions — boxed wrappers around primitives — not the primitive types developers usually mean:
-
Objectmatches every non-null/undefined value, including primitives. It does not enforce that the value is a "plain object". -
String,Number,Boolean,Symbol,BigIntrefer to the wrapper objects produced bynew String(…), etc. Theirtypeofis"object", not the matching primitive. -
Functionis a generic callable with no signature information. Code using it cannot tell what arguments to pass or what the return type is. -
{}matches every non-null/undefined value, just likeObject. It is rarely what the developer meant.
The fix is to switch to the matching lowercase primitive (string, number, boolean, symbol, bigint), to use a specific interface, or to write a precise function signature.
Remediation
Use the matching primitive or a specific shape:
function f(value: string): string { ... }
const cb: () => void = () => { ... };
interface UserData { id: string; name: string; }
const data: UserData = read();
For "any non-primitive value", use the lowercase object. For "any value at all", unknown is the safe choice (with explicit narrowing); avoid any if possible.