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
// @ts-check/** * Removes all listeners from a specified event. * * @param {String|RegExp} [evt] Optional name of the event to remove all listeners for. Will remove from every event if not passed. * @return {Object} Current instance of EventEmitter for chaining. * @license MIT * @see {@link https://github.com/Olical/EventEmitter|EventEmitter by Oliver Caldwell} */functionremoveEvent(evt){consttype=typeofevtconstevents=this._getEvents()// Remove different things depending on the state of evtif(type==='string'){// Remove all listeners for the specified eventdeleteevents[evt]}returnthis}
Expected behavior:
On line 18, the type checker should know that evt is a string, from the if (type === 'string) in line 16, and it should allow the use of evt as an object key.
Actual behavior:
Error on line 18, around the variable evt: "[js] Type 'RegExp' cannot be used as an index type." This error is unhelpful since we already checked that evt is a string in line 16.
Additional notes:
This works fine:
// ...if(typeofevt==='string'){// Remove all listeners for the specified eventdeleteevents[evt]}// ...
But this doesn't:
// ...lettypeif((type=typeofevt)==='string'){// Remove all listeners for the specified eventdeleteevents[evt]}// ...
And neither does this:
// ...lettype=typeofevtif(type==='string'){// Remove all listeners for the specified eventdeleteevents[evt]}// ...
The text was updated successfully, but these errors were encountered:
This is the same underlying cause as #10976. The compiler can not reason about relationship between values in such a fashion.
the fix is to do something like if (typeof evt === 'string') {
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
TypeScript Version: 2.3.2
Code
test.js
Expected behavior:
On line 18, the type checker should know that
evt
is a string, from theif (type === 'string)
in line 16, and it should allow the use ofevt
as an object key.Actual behavior:
Error on line 18, around the variable
evt
: "[js] Type 'RegExp' cannot be used as an index type." This error is unhelpful since we already checked thatevt
is a string in line 16.Additional notes:
This works fine:
But this doesn't:
And neither does this:
The text was updated successfully, but these errors were encountered: