Skip to content

Type of ([].length > 1 && {}) can be false but Typescript cannot detect itΒ #51120

@omidh14

Description

@omidh14

Bug Report

πŸ”Ž Search Terms

object type condition parenthesis

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

// This throws an error as expected because "Spread types may only be created from object types"
const invalid = { ...(false) }

// This does not throw even though the value in parenthesis would also be resolved to `false` which
// is a side effect of wrong type detection. This assignment is allowed because Typescript cannot detect
// that there is a possibility that the value inside the parenthesis would be `false`.  
const valid = { ...([].length > 1 && {}) }

const wrongTypeDetection = ([].length > 1 && {}) // type of wrongTypeDetection is {} instead of false | {} 

πŸ™ Actual behavior

Type of const wrongTypeDetection = ([].length > 1 && {}) is {}

πŸ™‚ Expected behavior

Type of const wrongTypeDetection = ([].length > 1 && {}) should be false | {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions