4

I am still rather new to TypeScript and trying to work on my knowledge and intuition about when to use which types.

When would you use unknown vs. object?

From https://www.typescriptlang.org/docs/handbook/basic-types.html:

About object:

object is a type that represents the non-primitive type, i.e. anything that is not number, string, boolean, symbol, null, or undefined.

About unknown:

We may need to describe the type of variables that we do not know when we are writing an application. These values may come from dynamic content – e.g. from the user – or we may want to intentionally accept all values in our API. In these cases, we want to provide a type that tells the compiler and future readers that this variable could be anything, so we give it the unknown type.

Is unknown a strict superset of object?

Is unknown maybe precisely object + number + string + boolean + symbol + null + undefined? If not: what's missing -- precisely, or conceptually?

If the TypeScript version matters for answering this: let's assume 3.9 :-).

Dr. Jan-Philip Gehrcke
  • 33,287
  • 14
  • 85
  • 130
  • I don't know if I had ever a use for `object`. I'd rather be either more or less specific. Plus, `object` includes functions, which I usually want to treat differently. About `unknown`, I've not had much experience with that one either, The cases from your description, there I usually use `any` but that may be just an old habit. Have to look into that. – Thomas Aug 14 '20 at 12:15
  • 1
    ESLint says: "Don't use `object` as a type. The `object` type is currently hard to use. See https://github.com/microsoft/TypeScript/issues/21732. Consider using `Record` instead, as it allows you to more easily inspect and use the keys". – Dr. Jan-Philip Gehrcke Aug 14 '20 at 12:15
  • By the way, somewhat related discussion about any vs Object (capital O): https://stackoverflow.com/questions/18961203/any-vs-object -- but that somehow seems to be pretty out of date. – Dr. Jan-Philip Gehrcke Aug 14 '20 at 12:20
  • Consider: `const x: unknown = {}` vs `const y: object = {}` where `x.bla()` would be denied by the compiler as it's unknown. `y.bla()` would only be denied as bla is not defined on type object. In general you should avoid to denote a variable/parameter etc with type object, as it can cause confusion. – r3dst0rm Aug 17 '20 at 11:22

1 Answers1

3

Don't mix together these two types. The object is kinda "real" type, it exists in runtime, it allows you to deal with instances of the Object. I would say it's just regular, normal type.

But unknown is a kinda different thing. It was created to guard operations with types that don't have a type at all. It denies any direct operations with this type:

const x:unknown = 5;
x+= 1; // Object is of type 'unknown'.

It also not exists in runtime, so you can't create an instance of unknown or something like this.

So, I would say that unknown is the opposite type to any defined type.

Drag13
  • 5,859
  • 1
  • 18
  • 42
  • Typescript types don't exist in runtime. – Sander Dec 30 '22 at 08:42
  • @Sander did I state something else? If you talking about the "object" - I meant that `object` type represents `Object` which, of course exists in runtume – Drag13 Jan 01 '23 at 10:31