https://typescript-eslint.io/rules/ban-types
DescriptionThe {}
has long been a source of confusion in TypeScript. It means "any non-nullish value, but with no known fields". That includes objects like {}
and { value: 1 }
, primitives like true
and ""
, arrays like []
, and general class instances like new Error()
.
Because users so often get confused by {}
allowing primitives like true
and ""
, the ban-types
rule suggests users use something more restrictive. Specifically its default options include:
`{}` actually means "any non-nullish value".', - If you want a type meaning "any object", you probably want `object` instead.', - If you want a type meaning "any value", you probably want `unknown` instead.', - If you want a type meaning "empty object", you probably want `Record<string, never>` instead.', - If you really want a type meaning "any non-nullish value", you probably want `NonNullable<unknown>` instead.',
That's pretty strict... at the cost of user convenience.
Now that #8364 is in, we have the ability to provide more lenient default options for the recommended*
configs vs. the strict*
configs.
Proposal: let's remove the restriction on {}
from the default ban-types
options in recommended
configs, and leave them in for strict
configs?
@RyanCavanaugh, the dev lead for TypeScript, is not a fan of the way it is now 😄: https://bsky.app/profile/searyanc.dev/post/3knqtqcjhpn2d
See also #5947 for past discussion around objects and ban-types
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