【问题标题】:toggle-switch-react-native for typescript用于打字稿的切换开关反应本机
【发布时间】:2021-04-16 12:58:54
【问题描述】:

我想在我的 Typescript 应用程序中使用 toggle-switch-react-native 组件。我尝试像这样安装它:

yarn add toggle-switch-react-native --save
@types/toggle-switch-react-native

但是,我仍然收到一个错误:

Could not find a declaration file for module 'toggle-switch-react-native'. '/Users/grannyandsmith/insta/app/node_modules/toggle-switch-react-native/ToggleSwitch.js' implicitly has an 'any' type.
  Try `npm install @types/toggle-switch-react-native` if it exists or add a new declaration (.d.ts) file containing `declare module 'toggle-switch-react-native';`

如何定义它的类型?有替代品吗?我安装正确吗?

【问题讨论】:

    标签: javascript reactjs typescript react-native toggle


    【解决方案1】:

    这个库没有类型,所以你需要创建一个“自定义类型”:

    创建一个名为 types/@custom_types 的文件夹。

    在该文件夹中,创建一个名为 declarations.d.ts 的文件。
    在该文件的第一行,添加

    declare module "toggle-switch-react-native" 
    

    【讨论】:

      【解决方案2】:

      在 npm 中,您应该使用 npm i toggle-switch-react-native --save 或 npm i toggle-switch-react-native --save-dev。这些已被 yarn add 和 yarn add --dev 取代。试试这个方法,可能有用

      【讨论】:

      • 这不是问题。问题在于 Typescript 声明。
      猜你喜欢
      • 2019-07-06
      • 2020-11-18
      • 2018-10-20
      • 2021-10-04
      • 1970-01-01
      • 2020-03-28
      • 2019-04-16
      • 2022-08-10
      • 2021-11-08
      相关资源
      最近更新 更多