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
{{ message }}
Repository navigation
Loosen strictness of Array.prototype.includes() and Set.prototype.has() #60559
constvalues=['foo','bar','baz']asconst;typeValidValue=(typeofvalues)[number];functionisValueValid(value: string): value is ValidValue{returnvalues.includes(value);// ^^ ts error: Argument of type 'string' is not assignable to parameter of type '"foo" | "bar" | "baz"'}
This error seems counter intuitive as the entire point of includes is to check if value in foo.includes(value) is one of foo.
A Set and mySet.has(...) has the same issue.
changed the title [-]Loosen strictness of `Array.prototype.includes()`[/-][+]Loosen strictness of `Array.prototype.includes()` and `Set.prototype.has()`[/+]on Nov 22, 2024
Duplicate of #60073 and many many more. Your search terms seem excessive, no wonder you didn't find any of them. I suggest limiting your search terms to a few related word, often with the in:title modifier.
IMO a type assertion is an appropriate workaround if this is intentional code.
And I have no clue what "strict property access" (from your use case) is about, that's completely unrelated.
Yes, strict property access made no sense. Removed that now from the description. This issue appeared when flicking "suppressImplicitAnyIndexErrors": true to false. A setting change that became mandatory with typescript@5.6.
Sadly I can't show it in the playground, because "suppressImplicitAnyIndexErrors": true isn't available anymore, even if you select an older version.
Activity
[-]Loosen strictness of `Array.prototype.includes()`[/-][+]Loosen strictness of `Array.prototype.includes()` and `Set.prototype.has()`[/+]MartinJohns commentedon Nov 22, 2024
Duplicate of #60073 and many many more. Your search terms seem excessive, no wonder you didn't find any of them. I suggest limiting your search terms to a few related word, often with the
in:titlemodifier.IMO a type assertion is an appropriate workaround if this is intentional code.
sod commentedon Nov 22, 2024
sorry. Thank you for your time.
I didn't see that improving this was already discussed and/or rejected.
sod commentedon Nov 22, 2024
reopen because it's not a duplicate of #60073.
This ticket is not about making
Array.prototype.includes()smarter. It's about not throwing ants-errorfor 100% valid and deterministic usecase.MartinJohns commentedon Nov 22, 2024
This is a duplicate of #60073, #26255 and many others, ending up with #14520.
Your issue is the accepted type for
includes(), same as the other.And I have no clue what "strict property access" (from your use case) is about, that's completely unrelated.
sod commentedon Nov 22, 2024
I aggree that this is a duplicate to #26255. A 6 year old, with 27 upvotes closed duplicate. But a duplicate non the less.
Then I guess i have to write:
I hope nobody follows the recommendation on https://www.typescriptlang.org/tsconfig/#suppressImplicitAnyIndexErrors to use
@ts-ignore, as that IMO is even worse.Yes,
strict property accessmade no sense. Removed that now from the description. This issue appeared when flicking"suppressImplicitAnyIndexErrors": truetofalse. A setting change that became mandatory withtypescript@5.6.Sadly I can't show it in the playground, because
"suppressImplicitAnyIndexErrors": trueisn't available anymore, even if you select an older version.Fell free to close this issue.
MartinJohns commentedon Nov 22, 2024
Again, you can just use a type assertion, IMO the cleanest approach:
values.includes(value as ValidValue)sod commentedon Nov 22, 2024
Indeed that works. Not sure why. I internalized that whenever I had to restort to typecasting, that my underlying types or generics are incorrect.
Thank you.