-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Smarter types for fromEvent #5512
Comments
The problem is that AFAICT, using |
Is it possible to break TypeScript apart and only use particular parts of it? What would be the benefits of doing so since it installs altogether and only exists in declaration files that take no space and do not make it into compiled sources? |
TypeScript is already broken apart into libraries that contains specific type declarations - the types for said libraries are included by adding the libraries via TypeScript's To get the In short, that has been a problem before and some effort was made to remove the dependency on the An alternative approach could be to provide an additional overload signature to declare module "rxjs/internal/observable/fromEvent" {
export function fromEvent</* whatever */>(/* whatever */): /* whatever */;
} TypeScript would then attempt to match the merged signature before is matched any of the signatures declared in the RxJS codebase for |
If there is really no way to work around this, and you are still interested in releasing it in a node module, this could go to rxjs-web. It's still in an alpha version due to the fact that I am a slacker and need to implement tests :D |
The only way that I can see it working in the RxJS codebase is if there are separate import locations for DOM and non-DOM type declarations. With the former requiring the |
FWIW: #4891, rxjs-from-emitter (type-safe |
Hi everyone, I just stumbled upon this. What do you think about using TypeScript's template strings to infer the event types? I used that technique to implement the This is how it could look when applied to type EventHandler<T, U extends Event = Event> = ThisType<T> & ((event: U) => void);
type EventTargetWithPropertyHandler<T extends EventTarget, U extends string, V extends Event> = {
[P in U as `on${P}`]: null | EventHandler<T, V>;
};
type EventType<T extends EventTarget, U extends string> = T extends EventTargetWithPropertyHandler<T, U, infer V> ? V : Event;
declare function fromEvent<T extends EventTarget, U extends string>(target: T, eventName: U): Observable<EventType<T, U>>; It basically works by looking up the event by it's type. It searches for the property handler with the same name and then uses the type definition of that handler. When using it like this ... fromEvent(messagePort, 'message'); ... TypeScript will infer the event type from the type definition of The type definition would need to be modified a bit to work without the pre-defined It will unfortunately not help to improve the types for events when writing code for Node.js. |
TypeScript 4.2 will be the minimum-supported version.
Maybe. There are some other things that need to be fixed before this could be done - see #6208. Specifically, the |
#6214 actually cause much more typing error for valid code, due to Typescript's inability to resolve function overload that contains union typed parameters. |
Would #5512 (comment) by @chrisguttandin be a reasonable idea now, that minimal TypeScript is supporting this? |
Wow, what a story stream ;), +1 for @waterplea, @chrisguttandin proposition might be a reasonable approach 📌 cc @cartant |
Feature Request
Is your feature request related to a problem? Please describe.
When you create Observable with fromEvent you have to manually define generic and in received event target type is not inferred from element we provided although to my knowledge DOM event target is always the same element you listen event on. You have to typecast.
Describe the solution you'd like
Here's a proof of concept for a better typed s solution:
https://stackblitz.com/edit/angular-typed-fromevent
It's only for traditional DOM events overload
Describe alternatives you've considered
I've been using wrapper function from example above in my project. Could be great if it was unnecessary and it was built-in in RxJS.
Additional context
I'm willing to work on a PR but I might need assistance with other overloads as I'm not that familiar with them.
The text was updated successfully, but these errors were encountered: