Repository navigation
Type guards in Array.prototype.every #14963
Copy link
Copy link
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptThe issue relates to the different libraries shipped with TypeScriptHelp WantedYou can do thisYou can do this
Milestone
Description
Activity
vkurchatkin commented
on Apr 1, 2017 ContributorMore actionsThis issue is similar to #7657,
It's not the same, though.
filtercreates a new array,everydoesn't. Assigning(string | number)[]to anumber[]is unsafe, because a string can be pushed at any moment.Reacted by Aluan Haddad, Simon Warta and Marouane FazouaneVladimir Kurchatkin (@vkurchatkin) Would adding the above declaration for
ReadonlyArray<T>be safe, then?Reacted by Kerem Katvkurchatkin commented
on Apr 3, 2017 ContributorMore actionsKyle Lacy (@kylewlacy) yes, I think so
- addedHelp WantedYou can do thisYou can do thisBugA bug in TypeScriptA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptThe issue relates to the different libraries shipped with TypeScript
on Apr 3, 2017 - added 4 commits that reference this issue
on Sep 10, 2017 - added 2 commits that reference this issue
on Sep 18, 2017 related to #5101
#16069 now tracks inferring the type guard automatically.
Reacted by Kagami Sascha Rosylight- locked as resolved and limited conversation to collaborators
on Oct 21, 2025
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptThe issue relates to the different libraries shipped with TypeScriptHelp WantedYou can do thisYou can do this
TypeScript Version: 2.2.2
Code
Expected behavior:
When every member of
numberOfStringsmatches theisNumbertype guard, the whole array should be inferred to benumberOfStrings: number[]; so, the above code should compile without error.Actual behavior:
The above fails to compile, with the following error:
This issue is similar to #7657, and it can be solved by adding something like the following to
lib.d.ts:(or, the above can be added locally to a project as a temporary workaround)