Description
The object
primitive type
This proposal describe a new primitive type for typescript object
.
Use case
JavaScript core api contains some functions that takes object
as parameter :
Object.getPrototypeOf
Object.getOwnPropertyDescriptor
Object.create
- ...etc
Currently there is no way in typescript to prevent passing other primitive type (string
, number
, etc ...) to those functions.
For example Object.create('string')
is a perfectly valid typescript statement, even if it will ends up with an error.
Creating a new object
primitive type would allows to model more correctly the signature of those function, and every function that share similar constraint.
Type Checking
Assignability
The object
type is the equivalent of {}
minus the assignability of other basic type, that means that :
- any other basic types are not assignable to
object
- any non-basic type is assignable to
object
- object is only assignable to
{}
andany
This behavior is coherant to how javascript works, the type represent every value that respect the constraint typeof value === 'object'
plus undefined
.
Type guard
A new type guard has to be introduced for object
: typeof value === 'object'
.
Optionally the compiler could also accept comparaison with the Object
function cast : Object(value) === value
.