Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I get: [ts] 'Promise' only refers to a type, but is being used as a value here

Tags:

angular

I'm taking an Angular2 course and following along. Everything I have been doing thus far for all the other exercise has worked fine until I got to creating this custom validator and this Promise code. Makes no sense to me why this would occur now.

enter image description here

I get: [ts] 'Promise' only refers to a type, but is being used as a value here.

Can't seem to resolve this. I get my error even without adding in the setTimeout function.

Per a suggestion from someone elsewhere. I added to the tsconfig.json: "compilerOptions": { ... "types" : [ "core-js" ] }

But I still get the error.

Using Visual Studio code: Version 1.10.1. OS: Windows 10 Pro.

Here's my code:

import {Control} from 'angular2/common';

export class UsernameValidators 
{
    static shouldBeUnique(control: Control) 
    {
       return new Promise((resolve, reject) =>
       {            
          setTimeout(function()
          {
             if (control.value == "Dan")
                resolve({ shouldBeUnique: true });
             else
                resolve(null);
          }, 1000);
       });
   }
}

Here's the course that I am taking and a screen shot of what I am being instructed to do. The intelliSense is different from mine. It works fine and they do not get the error I get.

enter image description here

Here's the courses end result.

enter image description here

like image 860
user3020047 Avatar asked Dec 11 '25 05:12

user3020047


1 Answers

You need to target ES6, or else use a polyfill library like bluebird. If you are targeting ES5 in the compiler output, then it will fail like this because ES5 has no Promises.

like image 147
Josh Wulf Avatar answered Dec 12 '25 18:12

Josh Wulf