The object
type represents any non-primitive value. It is used to define values that are not a primitive type (string
, number
, boolean
, symbol
, null
, or undefined
).
Example 1: Function Accepting object
or null
declare function create(o: object | null): void;
create({ prop: 0 }); // OK, object is passed
create(null); // OK, null is allowed
- The function
create
accepts an argument that is either anobject
ornull
. - Passing an object like
{ prop: 0 }
ornull
works fine.
Example 2: Invalid Types for object
create(42); // Error: 42 is a number, not an object
create("string"); // Error: "string" is a string, not an object
create(false); // Error: false is a boolean, not an object
create(undefined); // Error: undefined is not an object or null
object
only allows non-primitive values, meaning primitives likenumber
,string
,boolean
,undefined
, andsymbol
are not assignable to it.
Key Points:
object
is a type that represents any value that is not a primitive.- It can be used for objects, arrays, functions, or
null
, but not for primitive types like numbers or strings. - When you use
object
, you can’t pass primitives (likenumber
,string
,boolean
) but you can pass any non-primitive value, includingnull
.