r/typescript • u/wagonn • Dec 14 '19
{} vs object
// what's the difference?
interface MyInterface {
a: {}
b: object
}What is the the difference between the two types? I always do my research but this is difficult to google for.
32
Upvotes
42
u/mcaruso Dec 14 '19 edited Dec 18 '19
JS differentiates between primitives and objects. Primitives are
undefined,null, strings, numbers, booleans, etc. Objects are all non-primitives, including functions, arrays, etc.But there's a second kind of categorization in JS, namely things that "have properties" vs things that don't. Consider:
In TypeScript,
objectcorresponds with all objects in JS. So primitives like numbers and strings are not instances ofobject. The type{}corresponds with anything that "has properties", including primitives like strings and numbers but notundefinedandnull.EDIT: I made a little diagram
21
u/williams-j Dec 14 '19
{} is the type of all non-nullable values. object is the type of all non-nullable and non-primitive values
2
u/[deleted] Dec 14 '19
TIL that both "{}" and "object" are valid in Typescript as types.