2

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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Ostn
  • 803
  • 1
  • 9
  • 27

3 Answers3

3

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');
Ostn
  • 803
  • 1
  • 9
  • 27
1

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);
xExplorer
  • 19
  • 2
1

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');
dlorych
  • 21
  • 2