Filter instanceof type #6997
SergeyDvornikov
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am not the first nor the last to run into
type guard
issues when using thefilter
operator withinstanceof
(especially in strict mode).A very strong example:
One solution is to use an explicit type cast. For example:
I also came across other suggestions and methods here:
#4947
#3473
https://gist.github.com/jayoungers/63bf0bce77d28e448a41f5bb4c531264
None of these discussions of suggestions or ways are (to me) simple and elegant, and I decided to try to go the other way. You can use the method (function)-factory, which would take the class parameter to which you want to apply
instanceof
and return thetype guard
function:A check was made in strict mode:
No issues found.
I also think, if necessary, you can try to expand the functionality to accept several classes in the factory
Beta Was this translation helpful? Give feedback.
All reactions