【问题标题】:React Native extend NativeModules TypeScript typesReact Native 扩展 NativeModules TypeScript 类型
【发布时间】:2020-10-08 12:49:01
【问题描述】:

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

这是我的模块界面的示例

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

我就是这样使用它的:

NativeModules.BBAudioPlayer.playSound('tada');

如何扩展NativeModules 来添加我的新模块的类型?

【问题讨论】:

    标签: typescript react-native


    【解决方案1】:
    // 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;
      }
    }
    

    【讨论】:

    • 啊太棒了。谢谢!我遇到的问题之一是,我在我的文件夹中调用了文件 index.d.ts 并且没有被拾取。将文件重命名为其他名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多