Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native extend NativeModules TypeScript types

I have a native module and I would like to type it.

Here is an example of my module's interface

export interface BBAudioPlayer {
  playSound: (sound: 'click' | 'tada') => Promise<void>;
  pause: () => Promise<void>;
}

and that is how I use it:

NativeModules.BBAudioPlayer.playSound('tada');

How can extend NativeModules to add the types of my new module?

like image 429
Kevin Amiranoff Avatar asked Jun 18 '20 12:06

Kevin Amiranoff


1 Answers

// extendNativeModules.d.ts
// import original module declarations
import 'react-native';

export interface BBAudioPlayerInterface {
  playSound: (sound: 'click' | 'tada') => Promise<void>;
  pause: () => Promise<void>;
}

// and extend them!
declare module 'react-native' {

  interface NativeModulesStatic {
    BBAudioPlayer: BBAudioPlayerInterface;
  }
}

enter image description here

like image 50
strdr4605 Avatar answered Sep 27 '22 21:09

strdr4605