简体   繁体   English

如何将JS库导入React Native项目中的typescript

[英]How to import JS library to typescript in React Native project

How can I import js library (in my case xhook library) into my react native project written in typescript? 如何将js库(在我的情况下是xhook库)导入到用typescript编写的react本机项目中? Or How can I create typescript header file for external js library? 或者我如何为外部js库创建typescript头文件?

你可以简单地使用:

const signalrLib = require("react-native-signalr").default

TypeScript compiles to plain Javascript just like Babel or any other extended Javascript language. TypeScript编译为简单的Javascript,就像Babel或任何其他扩展的Javascript语言一样。

So when you add example xhook to your project, project owner has already compiled his/her TypeScript code into plain JS and you import it just like any other library. 因此,当您将示例xhook添加到项目中时,项目所有者已经将他/她的TypeScript代码编译为纯JS,并且您可以像任何其他库一样导入它。

eg. 例如。 import xhook from 'xhook' or so on how library author has specified. import xhook from 'xhook'或左右如何指定库作者。

You can see it yourself if you visit xhook's git page https://github.com/jpillora/xhook you can see compiled code in folder dist and in package.json -file, attribute main points to that file. 你可以自己看看,如果你访问xhook的git页面https://github.com/jpillora/xhook你可以看到文件夹distpackage.json -file中的编译代码,属性main指向该文件。

TypeScript is not itself language that is runned in browser, but that it is always compiled to plain JavaScript. TypeScript本身并不是在浏览器中运行的语言,而是始终编译为纯JavaScript。 Hopefully this helps out making a grasp how this works. 希望这有助于掌握这是如何工作的。

edit. 编辑。 seems like xhook is actually written in CoffeeScript but this same rule applies to it as well. 似乎xhook实际上是用CoffeeScript编写的,但同样的规则也适用于它。

As suggested, You can use any ES6/ES2015 notation in typescript. 如建议的那样,您可以在打字稿中使用任何ES6 / ES2015表示法。 With the new typescript, import will be 使用新的打字稿,导入将是

import xhook from 'xhook';

Older version: 旧版本:

import * as xhook from 'xhook'

Some module doesnt have type support. 某些模块没有类型支持。 You can look for type support as 您可以查找类型支持

yarn add @types/xhook

If you dont find type support you can use, node require syntax 如果您没有找到可以使用的类型支持,则节点需要语法

const xhook = require('xhook');

For that you may have to declare, require definition like: 为此您可能需要声明, 需要定义如下:

declare const require: any;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 javascript 库导入 React Native 项目? - How to import javascript library to React Native project? 如何在打字稿项目中导入外部JavaScript库 - How to import an external javascript library in a typescript project 在React项目中导入库 - Import a library in react project 如何正确地导入和使用MSAL(用于js的Microsoft身份验证库)到javascript(非打字稿)反应单页应用程序中? - How to properly import and use the MSAL (Microsoft Authentication Library for js) into a javascript (non typescript) react single page application? 如何正确导入和使用MSAL(js的Microsoft身份验证库)到打字稿反应单页面应用程序? - How to properly import and use the MSAL (Microsoft Authentication Library for js) into a typescript react single page application? 如何将 SVG.js 3.0.x 版本正确导入 Typescript Create-React-App 项目 - How to properly import SVG.js 3.0.x version into a Typescript Create-React-App project 如何使库与 React Native 项目兼容 - How to make a library compatible with a React Native project 在 React-Native Typescript 项目中包含一个 JS 文件 - Include a JS file in a React-Native Typescript project 将javascript库导入React Native - Import javascript library into React Native React Native 库的条件导入 - Conditional import of React Native library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM