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

Type narrowing does not work on boolean variables #55586

Closed
LeonardYam opened this issue Aug 31, 2023 · 4 comments
Closed

Type narrowing does not work on boolean variables #55586

LeonardYam opened this issue Aug 31, 2023 · 4 comments

Comments

@LeonardYam
Copy link

πŸ”Ž Search Terms

"type narrowing", "type guards", "switch statement", "switch", "possibly null"

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about type narrowing

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.2.2#code/PTAEHUFMBsGMHsC2lQBd5oBYoCoE8AHSAZVgCcBLA1UABWgEM8BzM+AVwDsATAGiwoBnUENANQAd0gAjQRVSQAUCEmYKsTKGYUAbpGF4OY0BoadYKdJMoL+gzAzIoz3UNEiPOofEVKVqAHSKymAAmkYI7NCuqGqcANag8ABmIjQUXrFOKBJMggBcISGgoAC0oACCbvCwDKgU8JkY7p7ehCTkVDQS2E6gnPCxGcwmZqDSTgzxxWWVoASMFmgYkAAeRJTInN3ymj4d-jSCeNsMq-wuoPaOltigAKoASgAywhK7SbGQZIIz5VWCFzSeCrZagNYbChbHaxUDcCjJZLfSDbExIAgUdxkUBIursJzCFJtXydajBVDtHHwDAAXlAAG9FCUSpx2IgAPJkAByUWg+X6bOk31AAB8BdBoIoAL7BBCcQQ0ZLU6SOUD8pW0hlM5msjnc3n8gCsvGlwUUcoVDIFep5EtAUtAdI1KrI5salqEPMQjutnNt0FAAEIaXTWRKANzBFQSeBkeLCBjCCGQWAKbjFBGgAAUnrZAEo0fL4O4AtB4Mws40UAAeUAARgADHmzYJ3qgNNnc4gC4zmaNBJYyOxIIU+32VAR4II5NJoHhxQHvmxXWPmRbi5BS+Ws7q-bzQLXG83VyUJh5pjKo2AcM0PGQvIhY85gewaF83GZmOwGMxIPxYNA6iJAwwJ6CIXgAEQAKKrAwiALPoEFJNiEHgA4qAAOTCFykASBBQQqOyXxkO8A78L+RyoI4abjPOTiPjowxYPoKAIIg0IJjwzGSLG0QiAYHDYgsTCsBwPBBCUQA

πŸ’» Code

type foo = {
    numOrNull: number | null
}

const foobar : foo = {
    numOrNull: 5,
}

const { numOrNull } = foobar
const isNum = numOrNull !== null;

// works as expected
// if (isNum) console.log(one < 10)

switch (isNum) {
    case true:
        // possibly null error
        console.log(numOrNull < 10)
        break
}

πŸ™ Actual behavior

numOrNull is possibly null inside the switch statement

πŸ™‚ Expected behavior

numOrNull should be narrowed down to type number and there should not be any errors

Additional information about the issue

No response

@fatcerberus
Copy link

fatcerberus commented Aug 31, 2023

For the record, this doesn't work either, so it's nothing to do with the variable:

switch (numOrNull !== null) {
    case true:
        // possibly null error
        console.log(numOrNull < 10)
        break
}

or this:

if (isNum === true) {
    // possibly null error
    console.log(numOrNull < 10)
}

Direct comparison of a type guard result to true/false is not recognized as a narrowing operation.

@MartinJohns
Copy link
Contributor

Related: #37178

This is probably just a pattern that is not supported. And I honestly see no reason why you'd write code like this.

@LeonardYam
Copy link
Author

Ah I see! Of course, I understand this pattern is very uncommon and there are simple alternatives with if-statements. I just wanted to understand why it did not work - whether the control flow analysis does not support this pattern etc.

And upon further digging, I found the exact reason why this is not supported. Thanks for the quick replies @fatcerberus @MartinJohns!

@mykohsu
Copy link

mykohsu commented Apr 17, 2024

Unlike #37178, this also doesn't work with if statements? Extracting certain logical expressions into local variables to make a complex if more parsable seems like a reasonable use case.

declare function equals(a: { [key: string]: any; }, b: { [key: string]: any; }): boolean;

let current: undefined | {} | null = null;

function updateValue() {
  const value = Math.random() > 0.5 ? {} : undefined;
  const check = current && value;
  if (check && equals(current, value)) { // error
    return;
  }
  if (current && value && equals(current, value)) {
    return;
  }
  current = value;
}

https://www.typescriptlang.org/play?#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXxAEdkoIBnACigC54BveAbQGsQBPWsjGLVAcwC6tKKjYBueAF8ANPABGtBi3aduvQcNETJASlpycOCCBFiAUGeMZ4YZDDioMtNKES8QweAB96k7-FRkCAh4AF4AoIhzMxR0bDx4ZAAHYCgMEAA1UmQQCh16M3gbPC54ADdshHCAWTSACwA6GBFgHABbPPgAPngABgaAVngAfl94Z1RXd2BzIrAS6zA68GYwmzsHawAyLfLK2fgsRHgKJZX4HYJiUkpbexBHWQqIHJ18ukKi+DgMO1QDySfI4nO6bC67Z45cFXEjkU4bB4YJ6VN4FL7fEC-GD-T6AuYIxxrSEgcySIA

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

4 participants