-
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
Support React Native EventEmitter type with fromEvent #3809
Comments
I would like to see the Node-style declarations relaxed, too. But rather than Did you want to submit a PR for this, Andrew, or do you want me to do it? |
I'll be happy to -- other than changing the return type to |
I don't think interface NodeStyleEventEmitter {
addListener: (eventName: string | symbol, handler: NodeEventHandler) => this;
removeListener: (eventName: string | symbol, handler: NodeEventHandler) => this;
}
declare type NodeEventHandler = (...args: any[]) => void;
interface RelaxedNodeStyleEventEmitter {
addListener: (eventName: string, handler: NodeEventHandler) => void | {};
removeListener: (eventName: string, handler: NodeEventHandler) => void | {};
}
interface EmitterSubscription {
context: any;
}
interface EventEmitterListener {
addListener(eventType: string, listener: (...args: any[]) => any, context?: any): EmitterSubscription;
}
interface EventEmitter extends EventEmitterListener {
removeListener(eventType: string, listener: (...args: any[]) => any): void;
}
declare const nodeEmitter: NodeStyleEventEmitter;
declare const reactNativeEmitter: EventEmitter;
const e1: NodeStyleEventEmitter = reactNativeEmitter; // Error
const e2: RelaxedNodeStyleEventEmitter = reactNativeEmitter; // OK
const e3: RelaxedNodeStyleEventEmitter = nodeEmitter; // OK When you submit a PR, could you please add some See here for an example. |
Feature Request
React Native defines its own EventEmitter type that it claims is a subset of the Node.js EventEmitter type, but it is a bit different: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react-native/index.d.ts#L143
Specifically,
removeListener
returnsvoid
. The return type ofaddListener
and parameters are also different, but this doesn't cause a typing problem.Is your feature request related to a problem? Please describe.
If you attempt to use
fromEvent
with a React Native EventEmitter implementation such as https://github.com/urbanairship/react-native-module you will get a type error:Describe the solution you'd like
There are at least two solutions:
ReactNativeEventEmitter
type.addListener
andremoveListener
forNodeStyleEventEmitter
to returnany
.The
isNodeStyleEventEmitter
check can be used for both, but unfortunately I don't think there is a way to know whether the provided EventEmitter is Node.js style or React Native except based on type. However, the return types foraddListener
andremoveListener
are not used, so being able to distinguish between them at runtime won't matter at this time.Describe alternatives you've considered
You can of course do
fromEvent(UrbanAirship as any...
or usefromEventPattern
, but these are more verbose and you lose type safety you could get from the former.The text was updated successfully, but these errors were encountered: