Skip to content

Inconsistent Errors on conditional assignment with optional object propertiesΒ #46276

Closed
@mkaraula

Description

@mkaraula

Bug Report

πŸ”Ž Search Terms

  • object
  • optional properties
  • undefined
  • conditional assignment

πŸ•— Version & Regression Information

  • This is the behavior in every version I have tried

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Foo = { one: boolean | undefined }
type Bar = {one: boolean}

const foo: Foo = {one: undefined};


const barOne: Bar = foo.one !== undefined ? foo : { one: true };

const barTwo: Bar = foo.one !== undefined ? {one: foo.one} : { one: true }

πŸ™ Actual behavior

The assignment for barOne throws an error but the assignment for barTwo does not.

πŸ™‚ Expected behavior

I was expecting that either both of them fail or that none of them is failing.

πŸ‘‹

Apologies if this is not a Bug but I dont get it and I could not find any explanation for this online.

I got stuck for quite a while with this Issue. My problem is that I dont understand why an conditional assignmnet like the one for barOne is failing. Then I discovered that when I do the assignment like I did for barTwo, Typescript isnt complaining.

I would expect that eiter both of them throw an error or both of them dont throw one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions