ypeScript 2.2 introduced the object
, a type that represents any non-primitive type. It can be used to more accurately type methods such as Object.create
. Don‘t confuse it with the Object
type or {}
, the empty object type, though!
Type ‘Object‘ gives autocomplete. It includes all the methods which an empty object can have.
But empty object without Object type has not.
If we try to assign a value to empty object, we get error:
In order to do it, we can do:
const object: { [key: string]: any } = {}; object.foo = ‘bar‘;
时间: 2024-09-27 04:41:00