There are numerous ways to represent 'objects', with examples like Object
, object
, {}
, { [k: T]: U }
, and Record<K, T>
. This is because almost everything in JavaScript (and TypeScript) is considered an object.*
Let's dive straight into the answers with some clear examples:
Examples
Take into account the following regarding Object
:
class C {}
const a: Object = 0
const b: Object = ""
const c: Object = []
const d: Object = {}
const e: Object = C
const f: Object = null // <-- error!
const g: Object = undefined // <-- error!
const h: Object = { foo: "bar" }
h.bar // Property 'foo' does not exist on type 'Object'.
Consider these points about object
:
class C {}
const a: object = 0 // <-- error!
const b: object = "" // <-- error!
const c: object = []
const d: object = {}
const e: object = C
const f: object = undefined // <-- error!
const g: object = null // <-- error!
const h: object = { foo: "bar" }
h.bar // Property 'foo' does not exist on type 'object'.
Helpful References
Learn more about Record<K, T>.
Explore index signatures.
Dive into mapped types