[英]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你可以看到文件夹
dist
和package.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.