Z
Z
zeuss562017-05-22 16:44:46
JavaScript
zeuss56, 2017-05-22 16:44:46

TypeScript behaves in a non-obvious way. How to fix error TS2339?

Compiles and runs fine, but the editor highlights errors.

Code and errors
// Imports
import * as fs from 'fs';

// Prepare arguments
let args = [];
process.argv.forEach(arg => {
  let {0: key, 1: val = null} = arg.split('=', 2);
  if (val) args[key] = val.replace(/^"(.*)"$/, '$1');
});

// Check required args
if (!args['path']) {
  console.error('Use "path" argument!');
  process.exit(0);
}
let filePath = args['path'];


// fs.stat promise wrapper
function fsStat(filePath) {
  return new Promise((resolve, reject) => {
    fs.stat(filePath, (err, stats) => {
      if (err) reject(stats);
      else resolve(stats);
    });
  });
}

(async () => {
  try {
    // Check file exists
    const stats = await fsStat(filePath);
    if (!stats.isFile()) throw new Error('Is dir.');
    console.log('File exists.');
  } catch (e) {
    console.log('File does not exists.');
  }
  console.log(fs.ReadStream(filePath));
})();

4998df83d0e74045be5b1817756ced00.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
zeuss56, 2017-05-24
@zeuss56

async returns the wrong type. You need to fix it:
There is an error in the last expression:

let fstream: fs.ReadStream = fs.createReadStream(filePath);

T
tyzberd, 2017-05-22
@tyzberd

probably asking you to specify the type of variables

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question