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
functionattachEvent(element: Element,params: Array<unknown>): undefined|(()=>void){let[selector,eventType,fn]=paramsif(typeofselector!=='string'||typeofeventType!=='string'||typeoffn!=='function'){return}// `selector` is correctly seen as `string`lettargetElem=element.querySelector(selector)if(!targetElem){return}// Cast required as type `Function` is not enough for `addEventListener`leteventHandler=fnas()=>unknown// `eventType` is correctly seen as `string`targetElem.addEventListener(eventType,eventHandler)// `targetElem` is back to `Object is possibly null`// `eventType` is now seen as `unknown` againreturn()=>targetElem.removeEventListener(eventType,eventHandler)// Solution: non-null assertion operation + cast `as string`, but why?// return () => targetElem!.removeEventListener(eventType as string, eventHandler)}
🙁 Actual behaviour
Type of variable targetElem is lost and fallback to its initial type.
Type of variable eventType is lost and fallback to its initial type.
🙂 Expected behaviour
Since the context does not change and their type has been narrowed down, the types of the variables targetElem and eventType should be kept.
The text was updated successfully, but these errors were encountered:
MrChocolatine
changed the title
Type of variable lost when returning a function that reuses this variable
Type of variables lost when returning a function that reuses them
Jan 10, 2021
Bug Report
🔎 Search Terms
type
lost
unknown
🕗 Version & Regression Information
This is a crashThis changed between versions ______ and _______However I tried with every versions in the Playground and I get the same result.
⏯ Playground Link
Playground link with relevant code - v4.1.3
Playground link with relevant code - Nightly / v4.2.0-dev.20210108
💻 Code
🙁 Actual behaviour
targetElem
is lost and fallback to its initial type.eventType
is lost and fallback to its initial type.🙂 Expected behaviour
Since the context does not change and their type has been narrowed down, the types of the variables
targetElem
andeventType
should be kept.Possible related issues
Thank you for your feedback.
The text was updated successfully, but these errors were encountered: