Steve81 Steve81 - 2 months ago 8
TypeScript Question

Nodejs, Typescript, typings: how to use async library

I'm trying to use the async library in Typescript; I've installed the definition file that was provide by typings but I cannot use AsyncFunction:

///<reference path='typings/index.d.ts' />
'use strict';

import async = require( 'async' );

let functions : AsyncFunction<any>[] = [];


If I compile this extract I got this error:

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'.


The async definition file that I use is this: https://raw.githubusercontent.com/types/npm-async/ff63908a70ec51b775d9a6b8afac9945b12fbe08/2/index.d.ts

What I'm doing wrong?

Thank you!

Answer

The interface AsyncFunction is not exported from that file. The only export is async as you can see from export = async; on the last line.

Fix

Feel free to copy interface AsyncFunction<T> { (callback: (err?: Error, result?: T) => void): void; } to a global.d.ts if you must use that annotation.