Answer the question
In order to leave comments, you need to log in
TypeScript behaves in a non-obvious way. How to fix error TS2339?
Compiles and runs fine, but the editor highlights 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));
})();
Answer the question
In order to leave comments, you need to log in
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);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question