Promise resolve has error in TypeScript because missed any type

Viewed 686

I'm using gulp to compile TS files to JS and for this code:

function Hello(): Promise<string> {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('Hello, World!');
    }, 3000);
  });
}

The compile has this error:

error TS7006: Parameter 'resolve' implicitly has an 'any' type.

That means, I should use any type like this:

return new Promise((resolve: any) => {

But why I should use any in this case? when I'm using Promise<string> to define the Promise ?

dependencies:

"dependencies": {
    "gulp": "^4.0.2",
    "gulp-typescript": "^6.0.0-alpha.1",
    "typescript": "^3.9.5"
  }

Thanks

2 Answers

It looks like TS compiler doesn't recognise the Promise type. The most likely reason for this is the lack or improper configuration. I would assume TS is compiling for ES5 where promises are not defined. Try to update your tsconfig.json as described at this page. I.e. add the following to tsconfig.json

"compilerOptions": {
    "target": "ES6"
}

I've edited gulpfile.js and solved:

gulp.task('ts', function () {
  return gulp.src('*.ts')
  .pipe(ts({
    noImplicitAny: true,
    outFile: 'output.js',
    target: 'ES6'
  }))
  .pipe(gulp.dest('./'));
});

Related