How to import nano (couchdb) - typescript

本秂侑毒 提交于 2019-12-11 07:32:14

问题


I have trouble importing and using nano in my node application.

The js way (from the doc) is :

var nano = require('nano')('http://localhost:5984');

How do I do that with typescript ?

I tried

import * as Nano from "nano";
let nano = new Nano('http://localhost:5984');

But then I get : Nano is not an object.


回答1:


By installing @types/nano we can look at :

node_modules/@types/nano/index.d.ts

where we see the lines :

declare function nano(config: nano.Configuration | string):
 nano.ServerScope | nano.DocumentScope;

Meaning Nano is a function not an object, so the answer :

import * as Nano from "nano";
let nano = Nano('http://localhost:5984');



回答2:


you have to typecast that like below:

import * as Nano from "nano";

let nano: Nano.ServerScope = <Nano.ServerScope>Nano('http://localhost:5984');
let db = nano.use(database);



回答3:


Since version 7.x nano has built-in TS type information for IDEs, and there's no need import additional typings.

Following should be enough:

import Nano from "nano";
let n = Nano('localhost:5984');


来源:https://stackoverflow.com/questions/45814385/how-to-import-nano-couchdb-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!