[英]How do I correctly require and declare node library typings in TypeScript?
[英]Nodejs, Typescript, typings: how to use async library
我正在嘗試在Typescript中使用異步庫; 我已經安裝了通過鍵入提供的定義文件,但是我不能使用AsyncFunction:
///<reference path='typings/index.d.ts' />
'use strict';
import async = require( 'async' );
let functions : AsyncFunction<any>[] = [];
如果我編譯此摘錄,則會出現此錯誤:
tsc test.ts --target es2015 --module system --removeComments --forceConsistentCasingInFileNames --noEmitOnError --noFallthroughCasesInSwitch --noImplicitAny --noImplicitReturns --noImplicitUseStrict --declaration --outfile a.js
test.ts(4,17): error TS2304: Cannot find name 'AsyncFunction'.
我使用的異步定義文件是這樣的: https : //raw.githubusercontent.com/types/npm-async/ff63908a70ec51b775d9a6b8afac9945b12fbe08/2/index.d.ts
我做錯了什么?
謝謝!
接口AsyncFunction
不會從該文件導出。 唯一的出口是async
的,你可以看到從export = async;
在最后一行。
隨意復制interface AsyncFunction<T> { (callback: (err?: Error, result?: T) => void): void; }
interface AsyncFunction<T> { (callback: (err?: Error, result?: T) => void): void; }
到global.d.ts
如果必須使用該注釋)。
在項目根文件夾上運行兩個命令
npm install async --save
npm我@ types / async --save
import * as async from 'async';
async.auto({
get_data: function(callback:any) {
console.log('in get_data');
// async code to get some data
callback(null, 'data', 'converted to array');
},
make_folder: function(callback:any) {
console.log('in make_folder');
// async code to create a directory to store a file in
// this is run at the same time as getting the data
callback(null, 'folder');
},
write_file: ['get_data', 'make_folder', function(results:any, callback:any) {
console.log('in write_file', JSON.stringify(results));
// once there is some data and the directory exists,
// write the data to a file in the directory
callback(null, 'filename');
}],
email_link: ['write_file', function(results:any, callback:any) {
console.log('in email_link', JSON.stringify(results));
// once the file is written let's email a link to it...
// results.write_file contains the filename returned by write_file.
callback(null, {'file':results.write_file, 'email':'user@example.com'});
}]
}, function(err:any, results:any) {
console.log('err = ', err);
console.log('results = ', results);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.