在打字稿中安装模块

Install module in typescript

我将在打字稿中安装和使用 victory-native 模块。我试过 npm install @types/victory-native 但它说没有模块 @types/victory-native.

我应该只在 typescript 中安装 @types/... 模块吗?

如何在打字稿中使用此模块?

就这样npm install victory-native --save
这个模块没有打字,但没有什么可以阻止你使用它。
还需要手动安装依赖,然后linknative code
react-native link react-native-svg

请参考他们的指南: https://github.com/FormidableLabs/victory-native

更新
以防万一,您不熟悉如何使用本机模块和 TypeScript。
对于这种情况,我有办法:

  • 当您看到它需要 link 时,使用弹出的应用程序比使用 expo 更容易。假设项目是从 create-react-native-app 创建的。确保项目被 运行 yarn eject
  • 弹出
  • 运行 yarn add victory-native react-native-svg 安装模块
  • 在tsconfig.json中设置"allowJs": true, "noImplicitAny": false.
  • react-native link react-native-svg 之后,打开 Android Studio,在 build.gradle(Module: app) 文件中,尝试找到 dependencies { compile project(':react-native-svg') ... }在底部。添加此行,如果它不存在。
  • android文件夹下,在命令行中运行gradlew clean
  • 然后构建您的 js 文件,并从命令行 运行 react-native run-android。该模块现在应该可以工作了。

有我的tsconfig.json:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "commonjs",
    "moduleResolution": "node",
    "jsx": "react-native",
    "outDir": "build",
    "rootDir": "src",
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "noImplicitAny": false,
    "preserveConstEnums": true,
    "allowJs": true,
    "sourceMap": false,
    "noImplicitReturns": true,
    "noUnusedParameters": true,
    "noUnusedLocals": true
  },
  "filesGlob": ["typings/index.d.ts", "src/**/*.ts", "src/**/*.tsx"],
  "types": ["react", "react-native", "jest"],
  "exclude": ["android", "ios", "build", "node_modules"],
  "compileOnSave": false,
  "include": ["src/**/*"]
}

直接复制自official documentation的简单示例:

import React, { Component } from "react";

import { VictoryBar } from "victory-native";

class App extends Component {
  render() {
    return (
      <VictoryBar />
    );
  }
}

export default App;