@typescript-eslint/ban-types
Disallow String, Number, Object, Function, {} and similar types
Also called typescript-eslint/ban-types, typescript/ban-types
Some built-in types don't mean what they look like:
String,Number,Boolean,Symbol, andBigIntare the wrapper objects. You almost always want the lowercasestring,number,boolean,symbol, orbigint.Functionaccepts any function, and calling it returnsany, so arguments and results aren't checked.Objectand{}accept every value exceptnullandundefined, including0and"".
Names that a class implements or an interface extends aren't reported.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"@typescript-eslint/ban-types" = "error" # or "warn"Examples#
example.ts2 problems
function greet(name: StringDo not use String as a type. Use string instead, onDone: FunctionDon't use Function as a type) {
onDone(`Hello, ${name}`);
}example.ts2 problems
let settings: {}Prefer explicitly define the object shape = 0;
let extra: Object'The Object type actually means any non-nullish value = "";example.tsNo problems
function greet(name: string, onDone: (message: string) => void) {
onDone(`Hello, ${name}`);
}
let settings: Record<string, unknown> = {};
let extra: object = {};
interface Handler extends Function {
priority: number;
}When not to use it#
Three newer rules split up the same checks, so you can turn each one on separately: @typescript-eslint/no-wrapper-object-types, @typescript-eslint/no-unsafe-function-type, and @typescript-eslint/no-empty-object-type.