Skip to content

Commit b31093e

Browse files
committed
strongly typed
1 parent 54e7e72 commit b31093e

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/ads/native-ad/NativeAd.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,14 @@ import { isFunction, isOneOf, isString } from '../../common';
2323
import NativeGoogleMobileAdsNativeModule, {
2424
NativeAdEventPayload,
2525
NativeAdImage,
26+
NativeAdPaidEventPayload,
2627
NativeAdProps,
2728
NativeMediaContent,
2829
} from '../../specs/modules/NativeGoogleMobileAdsNativeModule';
2930
import { NativeAdRequestOptions } from '../../types';
3031
import { validateNativeAdRequestOptions } from '../../validateNativeAdRequestOptions';
3132

32-
export interface AdPaidEventPayload {
33-
value: number;
34-
currencyCode: string;
35-
precision: number;
36-
}
33+
type NativeAdListenerPayload<EventType extends NativeAdEventType> = EventType extends NativeAdEventType.PAID ? NativeAdPaidEventPayload : never;
3734

3835
/**
3936
* A class for loading Native Ads.
@@ -97,7 +94,7 @@ export class NativeAd {
9794
this.eventEmitter.emit(type, data);
9895
}
9996

100-
addAdEventListener(type: NativeAdEventType, listener: (payload?: AdPaidEventPayload) => void) {
97+
addAdEventListener<EventType extends NativeAdEventType>(type: EventType, listener: (payload: NativeAdListenerPayload<EventType>) => void) {
10198
if (!isOneOf(type, Object.values(NativeAdEventType))) {
10299
throw new Error(`NativeAd.addAdEventListener(*) 'type' expected a valid event type value.`);
103100
}

src/specs/modules/NativeGoogleMobileAdsNativeModule.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ export type NativeAdEventPayload = {
5555
type: string;
5656
};
5757

58+
export type NativeAdPaidEventPayload = {
59+
value: number;
60+
currencyCode: string;
61+
precision: number;
62+
}
63+
5864
export interface Spec extends TurboModule {
5965
load(adUnitId: string, requestOptions: UnsafeObject): Promise<NativeAdProps>;
6066
destroy(responseId: string): void;

0 commit comments

Comments
 (0)