Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
icecream17 opened this issue May 19, 2021 · 4 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@icecream17
Copy link

icecream17 commented May 19, 2021

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

@icecream17 icecream17 changed the title Object destructuring for parameters don't narrow Object destructuring type doesn't narrow parameter B when parameter A is narrowed May 19, 2021
@RyanCavanaugh RyanCavanaugh added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels May 19, 2021
@RyanCavanaugh
Copy link
Member

This is a duplicate but I want to reraise the idea of CFA-linking variables sourced from the same destructuring expression

@dhythm
Copy link

dhythm commented May 21, 2021

I agree with this suggestion.
Type inference doesn't work well when I use destructuring.
I'm happy if this issue is solved. 🙏

image

@remcovaes
Copy link

remcovaes commented Aug 9, 2021

Is there any update on this? I am running into the exact same issue...

type F = ({ foo, bar }: { foo: number, bar?: never } | { foo?: never, bar: true } ) => number;

const f: F = ({ foo, bar }) => {
	if (bar === true) {
		return 2;
	}

	return foo;
};

https://www.typescriptlang.org/play?ts=4.3.5#code/C4TwDgpgBAYlC8UAUBvKAzA9pgNFARgIYBOUAvgFxRpaZUB2ArgLb4TF5HED8DEAbu3JQAPtQzZeUegPacSVYMUbQyUAJQIAfNJZtiAbgBQRgMaZ6AZ2AYqcRKgm4CJcpvg6URgJABLdMhcCPCISiqaXt7exBDAjMT0UABMxt5kJtGx8Ym0xmTGQA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants