-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Narrowed type not used when indexing #55922
Comments
There's a weird subtlety here where a type predicate returning true can guarantee something is true, but its false case can't guarantee something isn't false; but I always forget how much we do (or don't) abide by that. So you'll need to check in with @RyanCavanaugh. |
Within the truthy branch, the compiler is able to create an intersection of the input type and the one checked by the predicate. That's not possible within the falsy branch because there is no way to express a negated type. But why the function call works you might ask. That's because in that case we also have a contextual type and In the element access expression case, there is no contextual type and thus all of this doesn't happen. The type stays as |
@Andarist covered most of it, but since I was already writing this up... When narrowing by a type predicate, when neither the current nor the candidate type is a subtype of the other (which typically is the case when the current type is generic), we create an intersection in the true case and leave the type unchanged in the false case. For example, for a variable In #43183 we somewhat alleviate the lack of narrowing in the false case. However, in the expression So, all up, this issue is a design limitation. |
Ok, I understand what's happening now, I think. I know we can't express the intersection for the negative/false case, but couldn't we do something like filtering the constraints of |
For a type variable |
π Search Terms
"cannot be used to index type"
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?ts=5.3.0-dev.20230929#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXywGcAlEAcwFEAPABwAoqAueKVATwEpmqDD5TKtANwAoUJFgIweQhniEcAWxAB5AEYArZgG94AbULNZMLKjIBdZmpw4IIVvAC+okSnTY8SGwB4AkvBAqDBBUYD5jUzJ4AB9+cmoaAD4GZl8OeG0ReAJEeDoiAQSGDnTM7OyMKABrEBJ42mLRcvg4DGQYVCanLPhKmsIAZQwTM0b4AHpx+AB1BCrUHAB3eCZ5Ycj4AAsQOB6FZXUNPSpzIQmpgCFkOVksCAh4RYRgPAByOSh7pfg2HGRenDwZCEBA8RA4GAEUKBSIiRwiMTgaBwJBoTC4fBEIYjMgpFjsLgrXhrHEuNzozzgnAAJj8ASCITCJI2sUKtGSqzSGR6WFy+UG61GVBK3OafVq2MijR62X2qk0x1O53gABVNkRHhCqoQZS0QG0Ol14RVqrU2fRhaJ4YiJCjyR58OKBTi6IZmWZCQA3HBYYCicTIhD2jG9U11QT0N3mr0+v1AA
π» Code
π Actual behavior
Error on
someObj[x]
: "Type 'I' cannot be used to index type '{ [s: string]: boolean; }'."π Expected behavior
No error: from CFA we know the type of
x
is actually constrained tostring
.Additional information about the issue
If we reverse the order of the checks, i.e. check if the type of
x
isstring
in theif
, the indexing works:The text was updated successfully, but these errors were encountered: