Skip to content

Object destructuring type doesn't narrow parameter B when parameter A is narrowed #44169

Closed
@icecream17

Description

@icecream17

Bug Report

🔎 Search Terms

destructuring
narrowing
is not assignable to type

🕗 Version & Regression Information

(todo)

  • This is a crash
  • This changed between versions ______ and _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

💻 Code

This works

type Dastructure = 
   {b: false, c: null} |
   {b: true, c: string}

class Example {
   prop?: string;
   constructor (options: Dastructure) {
      if (options.b) {
         this.prop = options.c
      }
   }
}

This gets an error

type Dastructure = 
   {b: false, c: null} |
   {b: true, c: string}

class Example {
   prop?: string;
   constructor ({b, c}: Dastructure) {
      if (b) {
         this.prop = c
//       ^^^^^^^^^
// Type 'string | null' is not assignable to type 'string | undefined'.
//   Type 'null' is not assignable to type 'string | undefined'.
      }
   }
}

🙁 Actual behavior

error

🙂 Expected behavior

not error?

I think it's because in the second code the context of {b, c}: A isn't considered when b is narrowed

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions