Skip to content

Truthiness narrowing does not work as expected with && #52822

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
Juneezee opened this issue Feb 17, 2023 · 3 comments
Closed

Truthiness narrowing does not work as expected with && #52822

Juneezee opened this issue Feb 17, 2023 · 3 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@Juneezee
Copy link

Bug Report

πŸ”Ž Search Terms

  • Truthiness narrowing
  • Truthiness narrowing with &&
  • Truthiness narrowing with other conditional expressions
  • Narrowing
  • Control flow analysis

πŸ•— Version & Regression Information

Possibly related to #52272

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

interface Circle {
  kind: "circle";
  x: number;
}
 
interface Square {
  kind: "square";
  x: number | null;
}

type Shape = Circle | Square;

function fn1(shape: Shape) {
  if (shape.kind === "square" && shape.x === null) {
    const { kind, x } = shape // { kind: "square", x: number } as expected

    return
  }

  // After the conditional branch above, we know that `shape.x` can no longer
  // be `null`.

  if (shape.kind === "square") {
    const { kind, x } = shape // { kind: "square", x: number | null }
                              // type of `x` should be `number` only
  }

  const newKind = shape.kind // type is `"circle" | "square"` as expected
  const newX = shape.x       // type of `shape.x` should be narrowed to `number` only
}

function fn2(shape: Shape) {
  // it works when we narrow the type without other conditonal expressions
  if (shape.x === null) {
    const { kind, x } = shape // { kind: "square", x: number } as expected

    return
  }

  if (shape.kind === "square") {
    const { kind, x } = shape // { kind: "square", x: number } as expected
  }

  const newKind = shape.kind // type is `"circle" | "square"` as expected
  const newX = shape.x // type is `number` only as expected
}

πŸ™ Actual behavior

TypeScript fails to narrow the type of shape.x to number only in fn1 after the first conditional branch if (shape.kind === "square" && shape.x === null) πŸ˜•

πŸ™‚ Expected behavior

After the first conditional branch if (shape.kind === "square" && shape.x === null) in fn1, shape.x can not be null anymore.

However, doing just if (shape.x === null) in fn2 works fine

@fatcerberus
Copy link

fatcerberus commented Feb 17, 2023

After the first check, shape.x might still be null, if shape.kind is something other than square. While that possibility is precluded by the second check for shape.kind === "square", the compiler isn't capable of counterfactual reasoning like "X or Y; X is false, therefore Y". At any given point, type narrowing either happens or it doesn't and subsequent checks can only work from there.

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Feb 17, 2023
@RyanCavanaugh
Copy link
Member

the compiler isn't capable of counterfactual reasoning like "X or Y; X is false, therefore Y"

I've been trying to state this in a reasonable way for years and you finally provided a phrasing that makes sense πŸ’–

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

4 participants