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
I got a 2322 on the return B; line, saying that B is of type number | null which is not compatible with the return type number.
π Expected behavior
It's obvious that B cannot be null: Depending on the ternary branch, either B !== null or B === A && A !== null must be true. Therefore, the B in return B; should have number type and the statement should compile without error.
Additional information about the issue
Replacing the if condition with (B !== null) || (B === A && A !== null) fails with the same error. In fact
is the weakest form that still fails to compile, and might be a good starting point. (it reveals that ternaries are under-checked; at the very least, exprA ? exprB : exprC should imply exprA && exprB || !exprA && exprC)
The text was updated successfully, but these errors were encountered:
00001H
changed the title
Ternary
Ternary branches not checked
Apr 14, 2025
It's obvious that B cannot be null: Depending on the ternary branch, either B !== null or B === A && A !== null must be true. Therefore, the B in return B; should have number type and the statement should compile without error.
Type narrowing is not implemented as a constraint solver or theorem prover; it would be much too slow to try to analyze it this way. Cases where "real" code would benefit from this are few and far between.
π Search Terms
"ternary" "ts2322"
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.9.0-dev.20250414#code/GYVwdgxgLglg9mABAZwBZxAGwCYH0BGAprsDGIQBQCCAXImCALZEBOiAPvVpgDSIBCdBs0JtODTJgCUQpqwDeAKEQrEMYBUTVEAXj1dJUxAH4t-RAEJ9E6YjoVzenYipGpS1Z8QtCUECyR+AG5lVQBfUJUfPwDEAAYQsKA
π» Code
π Actual behavior
I got a 2322 on the
return B;
line, saying thatB
is of typenumber | null
which is not compatible with the return typenumber
.π Expected behavior
It's obvious that B cannot be
null
: Depending on the ternary branch, eitherB !== null
orB === A && A !== null
must be true. Therefore, the B inreturn B;
should havenumber
type and the statement should compile without error.Additional information about the issue
Replacing the
if
condition with(B !== null) || (B === A && A !== null)
fails with the same error. In factis the weakest form that still fails to compile, and might be a good starting point. (it reveals that ternaries are under-checked; at the very least,
exprA ? exprB : exprC
should implyexprA && exprB || !exprA && exprC
)The text was updated successfully, but these errors were encountered: