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

Narrowing with a typeguard fails after object spread #55876

Closed
Akiyamka opened this issue Sep 26, 2023 · 5 comments
Closed

Narrowing with a typeguard fails after object spread #55876

Akiyamka opened this issue Sep 26, 2023 · 5 comments

Comments

@Akiyamka
Copy link

Akiyamka commented Sep 26, 2023

🔎 Search Terms

typeguard narrowing, object spread

🕗 Version & Regression Information

This is the behavior in every version I tried, and I recheck handbook about:

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.2.2#code/JYOwLgpgTgZghgYwgAgCIBUDyyDeAoZZGAe2IC5kBnMKUAcwG4DkAjOKAfgutpEbwC+ePKEixEKDJgDqwMAAsAQu2QQAHpBAATSmiy5mbKNxr0mQvAmIhqqmwFcoEZVACSlAMoQwyALzIACi0wcj1MAEo-AD5kAEJY4OIAOiMmPAB6dOQPAAcnOC1kBHzIXThWezo8GHsQBDBga2QAWzhQALUKKXCurFkFFwNCYBhA2LUU9kj8QkIJoz9kAHItCHh7ABswJaZCC0IIBycXdy8wDvC0wisbHwBPRfn2BmRM5AfgXR56WOZmJzAjhAQ1mSTBamYQgsb368mI9h8lDyEAKyAA7sQoABrMq6dQ5CD1CBaaq1eqNYEsBEdXoRWmwwYzZAjMZPKDTZhzSZQRYrNZwTbbXbIfZ2SiOZzsU7eC5XIrWWwPfxsl6vLIfL6mPi-Qj-bxAkGEEihCbGgA0nNY7AobMhgmEQA

💻 Code

interface DTO {
  foo: string;
  bar?: string;
}

interface DTOWithBar extends DTO {
  bar: string;
}

const ensureBarIsSet = (dto: DTO) => !!dto.bar;

function main(x: DTO): DTOWithBar {
  if (!x.bar) {
    x.bar = 'default';
  }

  ensureBarIsSet(x);
  const y = x.bar; // y is string!
  return {   ...x  } 
  //         ^^^^
  // Type 'undefined' is not assignable to type 'string'
}

🙁 Actual behavior

Typescript ignore type guard if spread used

🙂 Expected behavior

Typescript respect type guard even after spread

Additional information about the issue

No response

@fatcerberus
Copy link

More accurately, duplicate of #42384 (there’s no discriminated union here).

@Akiyamka
Copy link
Author

Akiyamka commented Sep 26, 2023

From #42384:

Type guards do not propagate type narrowings to parent objects. The narrowing is only applied upon access of the narrowed property which is why the destructing function works, but the reference function does not. Narrowing the parent would involve synthesizing new types which would be expensive.

Not sure it's my case, I already declare new type (DTOWithBar), and just want to keep it

@fatcerberus
Copy link

It’s the same thing. Your type check narrows x.bar; for this to work, it would also need to narrow x as a whole. If you change the function to return x you’ll see you get the same error.

@MartinJohns
Copy link
Contributor

Just additionally pointing out that the ensureBarIsSet function is completely useless. It's neither a type guard, nor an assertion function. Just a function returning a boolean, a result that is then discarded.

@Akiyamka
Copy link
Author

@MartinJohns you right, it's mistake in example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants