You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Potentially, we could expect the behaviors of if (a === null) and if ((v => v !== null)(a)) (inferred as predicate since TS 5.5) should be the same, but it's actually not in specific cases.
If it's combined with typeof a === 'object' check in the same flow, first one is correctly omits the null possibility, while second one doesn't.
This changed between versions:
I confirmed that the exclusion in flow can be done since TS4.5. It's not regression, but the new feature seems to have been partially applied.
Here is important to check typeof === 'object' after the check for null, because type narrowing step is formally, unknown → object | null → object. === null alone cannot narrow anything on unknown.
🔎 Search Terms
"null", "control flow", "narrowing", "narrow", "exclusion", "object", "typeof"
🕗 Version & Regression Information
Potentially, we could expect the behaviors of
if (a === null)
andif ((v => v !== null)(a))
(inferred as predicate since TS 5.5) should be the same, but it's actually not in specific cases.If it's combined with
typeof a === 'object'
check in the same flow, first one is correctly omits the null possibility, while second one doesn't.⏯ Playground Link
https://www.typescriptlang.org/play/?ts=5.6.2#code/MYewdgzgLgBAlhAcgVwDapgXhgCgG4BcMyYA1mCAO5gCUWAfDHlptmGqgNwBQA9LzEEwAegH5uoSLABmWXAEMiJclVoMYAb25D4snAhToc8mnQBOAUyjIzYHjrh6oATwAOFkLPkwAhKxgA5CAARgBWFsBQAeZWNnbaQvL2-CLiAL483BLg0DAA5nLGSmQU1HSYjFoOet6sbBwx1rb2Qo64Lu6eMN5+2EFhEVGNcS2CSdopYtwZQA
💻 Code
🙁 Actual behavior
Last
a
inf
is inferred asobject | null
.🙂 Expected behavior
Last
a
inf
would be inferred asobject
.Additional information about the issue
Here is important to check
typeof === 'object'
after the check for null, because type narrowing step is formally,unknown
→object | null
→object
.=== null
alone cannot narrow anything onunknown
.If you swap the if-statements in above, you can see both is inferred as
object
.The text was updated successfully, but these errors were encountered: