Skip to content

Non-null assertion operator ignored in sub context #44575

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

Closed
KyleJune opened this issue Jun 13, 2021 · 2 comments
Closed

Non-null assertion operator ignored in sub context #44575

KyleJune opened this issue Jun 13, 2021 · 2 comments

Comments

@KyleJune
Copy link

Bug Report

πŸ”Ž Search Terms

non-null assertion, deno, Property 'x' does not exist on type 'never'.

πŸ•— Version & Regression Information

This regression happened between typescript 4.2.2 and 4.3.2.

⏯ Playground Link

Playground link showing no errors in 4.2.2

Playground link showing errors in 4.3.2

πŸ’» Code

interface Point {
  x: number;
  y: number;
}
let p: Point | null = null;
function assignP(value: Point) {
  p = value;
}
assignP({ x: 2, y: 3 });
let z: number = p!.x * p!.y;
if (p) {
  z = p!.x * p!.y;
}
console.table({ x: p!.x, y: p!.y, z });

πŸ™ Actual behavior

Errors inside the if block because p is inferred as never.

πŸ™‚ Expected behavior

No errors.

I also believe the non-null assertion operator in the if block should not be required since the context would only be reached if p was not null. The non-null assertion in the if block was required in 4.2.2.

@MartinJohns
Copy link
Contributor

MartinJohns commented Jun 14, 2021

You see this behavior due to a fixed bug. See #44025.
In the end it's due to #9998.

This is a duplicate of #44025.

@MartinJohns
Copy link
Contributor

You can get the behavior you want by changing your code to this:

let p = null as Point | null;

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

2 participants