React Native 扩展 NativeModules TypeScript 类型

问题描述 投票:0回答:2

我有一个本机模块,我想输入它。

这是我的模块界面的示例

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

这就是我使用它的方式:

NativeModules.BBAudioPlayer.playSound('tada');

如何扩展

NativeModules
来添加我的新模块的类型?

typescript react-native
2个回答
17
投票
// 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


0
投票

扩展@strdr4605response,如果你想使用,还需要

& NativeModule
extends NativeModule
到你现有的界面

new NativeEventEmitter(NativeModules.BBAudioPlayer)
© www.soinside.com 2019 - 2024. All rights reserved.