4 ms·
Pattern Matching in TypeScript
- zenosmosis 5y agoLooks like you've done some homework here. Just a quick note: I followed through to the article on https://fatih-erikli.com/pattern-matching-in-typescript.html https://fatih-erikli.com/pattern-matching-in-typescript.html and noticed the useEffect call doesn't define a dependency array (not even an empty one), which I believe will make the addEventListener add new listeners on every render. Having a removeEventListener on the return of the useEffect would also ensure it gets cleaned up, if pointed to the same event name / handler. I assume it was just for illustrative purposes, but felt like I should point it out, regardless.
- fatih-erikli 5y agoYes it was for illustrative purposes, thanks for reading the article :)
- fatih-erikli 5y agoTo be honest this is more like a Typescript project and example rather than React. I'm learning Typescript and this is how I try to improve my understanding of Generic types.
- fatih-erikli 5y agoeventEmitter.addEventListener returns a unsubscribe function to remove the event listeners. useEffect hook calls the passed function's return value if it is not a void. https://reactjs.org/docs/hooks-effect.html#example-using-hooks-1 https://reactjs.org/docs/hooks-effect.html#example-using-hoo... So my eventEmitter.addEventListener method in the blog post returns an ubsubscriber when you call it :)
- devit 5y ago[flagged]
- fatih-erikli 5y agoYes but I left it intentionally. I don't think you will try to match a pattern with a function in practice. This library is intended to handle a piece of an object or array such as an application state in frontend. About the Dunning-Kruger effect, Wow wow wow :D It has nothing to do with the language preference, THUS you're not in a position to evaluate a programmers incompetence by seeing a piece of code shared on github.
- fatih-erikli 5y agoThanks for reading the code by the way. I wasn't even thinking that somebody will take a look at that :D I will make the predicate as same as I do in placeholder. Even though it is not practical, it's important to keep the function's behavior consistent. You're right :)
- cameronfraser 5y agohow do you sleep at night?