Search code examples
javascriptnode.jstypescriptbluebird

Is there any way to promisify node's functions and get intellisense with Typescript?


I'm using @types definitions and added @types/bluebird, as expected I get autocomplete when using the methods from the library. However I would like to promisify node's functions (fs). I'm able to do that with the following line:

import * as Promise from 'bluebird'
import * as fs from 'fs'

const fsPromisified = Promise.promisifyAll(fs)

The problem is that when I do fsPromisified I lose autocomplete.

Is there any way I can do this without having to wrap around node's functions myself with promises?


Solution

  • You can use the pre-promisified mz/fs instead of fs, which wraps all promisifies all async fs functions (with their original names, not the Async prefix). That package has TypeScript typings (@types/mz):

    import * as fs from 'mz/fs';
    
    // e.g., read file
    fs.readFile('somefile')
      .then((contents) => {
        // ...
      });