Skip to content

Suggestion: The object primitive type #1809

Closed

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 {} and any

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions