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
Typescript doesn't detect correct type in constant array. #13578
The problem is that we really have no idea that your array isn't mutated. Heterogeneous arrays are sometimes tuples and sometimes just arrays. In this case since you are using an array as a tuple (thus hopefully not calling sort / push / pop on it), you can add a type assertion to inform the checker:
Well, it's a temporary, so I expected Typescript to be able to auto-treat it as constant.
I guess you could whitelist forEach (and some other methods) as a const method or something, so you know it can't be mutated, but yeah, seems complex enough.
Not that it particularly matters (since we don't do it either way), but it's the possible mutation of entry that's the issue, which is definitely available to mutate. And forEach on a temporary still isn't guaranteed to be immutable because the array itself is the third argument to the forEach callback (JS: always out to get you).
Activity
RyanCavanaugh commentedon Jan 19, 2017
The problem is that we really have no idea that your array isn't mutated. Heterogeneous arrays are sometimes tuples and sometimes just arrays. In this case since you are using an array as a tuple (thus hopefully not calling
sort/push/popon it), you can add a type assertion to inform the checker:emilio commentedon Jan 19, 2017
Well, it's a temporary, so I expected Typescript to be able to auto-treat it as constant.
I guess you could whitelist
forEach(and some other methods) as aconstmethod or something, so you know it can't be mutated, but yeah, seems complex enough.Thanks for the reply! :)
RyanCavanaugh commentedon Jan 19, 2017
Not that it particularly matters (since we don't do it either way), but it's the possible mutation of
entrythat's the issue, which is definitely available to mutate. AndforEachon a temporary still isn't guaranteed to be immutable because the array itself is the third argument to theforEachcallback (JS: always out to get you).emilio commentedon Jan 19, 2017
Oh, of course, sorry :)