Skip to content

"{}" type issues when comapred to "Object"/"object"Β #48988

@dblachut-adb

Description

@dblachut-adb

Bug Report

πŸ”Ž Search Terms

"object" | "{}"

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about object & {}.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type A = {}
const a0: A = undefined // error
const a1: A = null // error
const a2: A = 2 // ok
const a3: A = 'hello world' //ok
const a4: A = { foo: 'bar' } //ok
const a5: A = () => undefined //ok
const X = {} // X: {}
type D = typeof X // D = {}
const Y = new Object() // Y: Object
type E = typeof X // E = {}
interface F<T extends object = {}> {
    prop: T;
}

πŸ™ Actual behavior

I have found at least few issues with {} type, all presented in playground and code samples above:

  1. Is undocumented (or I couldn't find it in docs) and confusing - looks like object literal but can have primitives assigned.
  2. Object literal has a {} type assumed while it should rather be Object/object.
  3. new Object has a {} type assumed while it should rather be Object/object.
  4. {} type can be assigned to more narrow object type, which should not be possible.

πŸ™‚ Expected behavior

{} type is documented, cannot be assigned to more narrow object type and is not assumed when used with typeof <some_object>.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions