最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to import JS library to typescript in React Native project - Stack Overflow

programmeradmin1浏览0评论

How can I import js library (in my case xhook library) into my react native project written in typescript? Or How can I create typescript header file for external js library?

How can I import js library (in my case xhook library) into my react native project written in typescript? Or How can I create typescript header file for external js library?

Share Improve this question asked May 3, 2018 at 8:32 JaSHinJaSHin 2772 gold badges17 silver badges45 bronze badges 1
  • See if this helps? github.com/philikon/ReactNativify – Tarun Lalwani Commented May 7, 2018 at 16:46
Add a comment  | 

3 Answers 3

Reset to default 9

You can simply use:

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

TypeScript compiles to plain Javascript just like Babel or any other extended Javascript language.

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.

eg. import xhook from 'xhook' or so on how library author has specified.

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.

TypeScript is not itself language that is runned in browser, but that it is always compiled to plain JavaScript. Hopefully this helps out making a grasp how this works.

edit. seems like xhookis actually written in CoffeeScript but this same rule applies to it as well.

As suggested, You can use any ES6/ES2015 notation in typescript. 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;
发布评论

评论列表(0)

  1. 暂无评论