1

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!

1
  • In case someone got the same problem, my solution was to install the global definition dt~async Commented Oct 4, 2016 at 8:24

2 Answers 2

3

Run Two commends on project root folder

npm install async --save

npm i @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':'[email protected]'});
    }]
  }, function(err:any, results:any) {
    console.log('err = ', err);
    console.log('results = ', results);
  });
Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

Thank you, I understand why I was confused: the async v1 definition doesn't use "module" so all the interfaces are directly availables.
@Steve81 could you elaborate little more why this error happened

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.