问题
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