Description
So I've gone through the pull request that introduces it and the discussion therein -- and apologies in advance if I'm just whipping a dead horse everybody would prefer to stay dead -- but the recent 3.0.0 ban-types
update's banning of object
seems particularly ill-advised to me for a couple of reasons. We here at TouchBistro have our own custom lint ruleset so we can always just disable it, but I wanted to at least broach the subject in the general community about the specific usecases where object
is useful because, well, there's always the possibility that I'm wrong.
In short, the recommended replacement of Record<string, unknown>
doesn't, I think, generally replace what we use object
for -- or, I suspect, what the vast majority of developers use it for. If there's another type that can be used as an extension base for generics to hard-lock that specific type as an object
, I'd love to hear it, but Record<string, unknown>
can't be used in this case because casting any ole' object type to that requires an index property, which adds a lot of complexity to the typing just to make lint rules pass. There's a larger discussion here about how perhaps TypeScript should make index properties default for keys of objects or provide greater flexibility for type refinement when interacting with object
, but at the end of the day I don't think creating types with properties that are objects is all that rare -- take, for example, a database model with a jsonb
column, where the format of what's in that column can change, So Model<T extends object> { name: string, id: number, blob: T }
allows for sub-typing of that model. The argument is made that this is a niche case, but is it really that niche? If it is, then I'll totally take the L on this one and acknowledge that we just have different needs in the ruleset, but unlike Object
or Function
, there are perfectly cromulent uses of object
that, while maybe a bit advanced, certainly don't take it out of the realm of acceptable idiomatic TypeScript -- and moreso, I'd argue that even MORE niche are the cases where it can be acceptably replaced with an unsealed indexed type such as Record<string, unknown>
.
Look, I love Record
-- there are tons of situations where it's a wonderful tool, especially when performing diffs or other in-the-guts object-level wizardry or whatever -- I'm not knocking Record
. And I recognize that the goal of the lint ruleset is to keep dangerous tools out of the hands of developers who might not realize their implications. But object
isn't a handgun, it's at best a Swiss Army pen knife, and I'm not sure putting rubber on it and suggesting a nail file instead is the proper risk/reward calculus.